-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoAnchorProviderBase.cs
More file actions
41 lines (32 loc) · 1.26 KB
/
AutoAnchorProviderBase.cs
File metadata and controls
41 lines (32 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;
namespace Xprees.RuntimeAnchors.Base
{
public abstract class AutoAnchorProviderBase<T> : MonoBehaviour where T : Object
{
[SerializeField] protected RuntimeAnchorBase<T> anchor;
[Header("Manual Reference - Optional")]
[Tooltip("This will override the automatic reference. Not required.")]
[SerializeField] protected T manualReference;
protected virtual void Start() => ProvideAnchor();
protected virtual void OnEnable() => ProvideAnchor();
protected virtual void OnDisable()
{
if (anchor == null) return;
anchor.Unset();
}
protected abstract T GetAutomaticallyAnchorComponent();
protected void ProvideAnchor()
{
if (anchor == null) return;
if (anchor.isSet) return;
var value = manualReference != null ? manualReference : GetAutomaticallyAnchorComponent();
anchor.Provide(value);
}
private void OnValidate()
{
// Check that the GameObject is present in the scene and not a prefab
if (gameObject.scene.rootCount == 0) return;
if (anchor == null) Debug.LogWarning($"Anchor on {name} has no {nameof(anchor)} set.");
}
}
}