-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleSignalListenerBehaviour.cs
88 lines (69 loc) · 2.82 KB
/
SimpleSignalListenerBehaviour.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine.Serialization;
namespace AltSalt.Maestro
{
[ExecuteInEditMode]
public class SimpleSignalListenerBehaviour : MonoBehaviour, ISimpleSignalListener, ISkipRegistration
{
[SerializeField]
[ListDrawerSettings(AlwaysAddDefaultValue = true)]
private List<SimpleSignalReference> _simpleSignalReferences = new List<SimpleSignalReference>();
private List<SimpleSignalReference> simpleSignalReferences => _simpleSignalReferences;
[SerializeField]
[ValidateInput(nameof(IsPopulated))]
protected GameObjectGenericAction _action;
private GameObjectGenericAction action => _action;
[SerializeField]
[InfoBox("Specifies whether this dependency should be recorded when the RegisterDependencies tool is used.")]
[FormerlySerializedAs("doNotRecord")]
private bool _skipRegistration;
public bool skipRegistration => _skipRegistration;
public UnityEngine.Object parentObject => gameObject;
public string sceneName => gameObject.scene.name;
private void OnEnable()
{
string simpleSignalsListPath = nameof(_simpleSignalReferences);
for (int i = 0; i < simpleSignalReferences.Count; i++) {
#if UNITY_EDITOR
simpleSignalReferences[i].PopulateVariable(this,
$"{simpleSignalsListPath}.{i.ToString()}");
#endif
var simpleSignal = simpleSignalReferences[i].GetVariable() as SimpleSignal;
if(simpleSignal != null) {
simpleSignal.RegisterListener(this);
} else {
Debug.LogWarning("Please set an event for SimpleEventListenerBehaviour on " + this.name, this.gameObject);
}
}
}
private void OnDisable()
{
for (int i = 0; i < simpleSignalReferences.Count; i++) {
var simpleSignal = simpleSignalReferences[i].GetVariable() as SimpleSignal;
if(simpleSignal != null) {
simpleSignal.UnregisterListener(this);
}
}
}
public void OnEventRaised()
{
action.Invoke(this.gameObject);
}
public void LogName(string callingInfo)
{
Debug.Log(callingInfo + gameObject, gameObject);
}
private static bool IsPopulated(UnityEvent attribute)
{
return Utils.IsPopulated(attribute);
}
private static bool IsPopulated(GameObjectGenericAction attribute)
{
return Utils.IsPopulated(attribute);
}
}
}