-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnchorReferenceBase.cs
More file actions
52 lines (43 loc) · 1.37 KB
/
AnchorReferenceBase.cs
File metadata and controls
52 lines (43 loc) · 1.37 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
42
43
44
45
46
47
48
49
50
51
52
using System;
using UnityEngine.Events;
using Object = UnityEngine.Object;
namespace Xprees.RuntimeAnchors.Base.Reference
{
[Serializable]
public class AnchorReferenceBase<T> where T : Object
{
public bool useInlined = true;
public T inlinedValue;
public RuntimeAnchorBase<T> anchor;
public event UnityAction<T> OnValueChanged;
public AnchorReferenceBase()
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (!useInlined && anchor != null) anchor.onAnchorProvided += OnAnchorProvided;
}
private void OnAnchorProvided() => OnValueChanged?.Invoke(anchor.Value);
public AnchorReferenceBase(T value)
{
useInlined = true;
inlinedValue = value;
}
public T Value
{
get => useInlined ? inlinedValue : anchor.Value;
set
{
if (!useInlined)
{
anchor.Provide(value);
}
else
{
inlinedValue = value;
}
OnOnValueChanged(value);
}
}
public static implicit operator T(AnchorReferenceBase<T> reference) => reference?.Value;
protected virtual void OnOnValueChanged(T value) => OnValueChanged?.Invoke(value);
}
}