Skip to content

Commit 82ad196

Browse files
committed
Implemented support to up to three arguments
1 parent 470b3b4 commit 82ad196

12 files changed

Lines changed: 274 additions & 10 deletions

Editor/EditorUI/BaseEventListenerCustomInspector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Editor.EditorUI
66
{
7-
public class BaseEventListenerCustomInspector : UnityEditor.Editor
7+
public abstract class BaseEventListenerCustomInspector : UnityEditor.Editor
88
{
99
protected const string EventSOVariableName = "_baseEvent";
1010

@@ -18,8 +18,6 @@ protected virtual void OnEnable()
1818
protected void DrawEventInspector(string label, string tooltip, Func<UnityEngine.Object, bool> validationLogic)
1919
{
2020
serializedObject.Update();
21-
// EditorGUILayout.Space();
22-
// EditorGUILayout.LabelField("Event Configuration", EditorStyles.boldLabel);
2321

2422
EditorGUI.BeginChangeCheck();
2523
var newObj = EditorGUILayout.ObjectField(new GUIContent(label, tooltip), _baseEventProp.objectReferenceValue, typeof(ScriptableObject), false);
@@ -40,5 +38,7 @@ protected void DrawEventInspector(string label, string tooltip, Func<UnityEngine
4038
DrawPropertiesExcluding(serializedObject, "m_Script", EventSOVariableName);
4139
serializedObject.ApplyModifiedProperties();
4240
}
41+
42+
protected abstract bool ValidationLogic(System.Object obj);
4343
}
4444
}

Editor/EditorUI/EventListenerWithArgCustomInspector.cs renamed to Editor/EditorUI/EventListener1ArgCustomInspector.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22
using SOBaseEvents.Refactor;
33
using UnityEditor;
4-
using Object = UnityEngine.Object;
54

65
namespace Editor.EditorUI
76
{
87
[CustomEditor(typeof(EventListener<,>), true)]
98
[CanEditMultipleObjects]
10-
public class EventListenerWithArgCustomInspector : BaseEventListenerCustomInspector
9+
public class EventListener1ArgCustomInspector : BaseEventListenerCustomInspector
1110
{
1211
Type _tArg;
1312

@@ -34,7 +33,7 @@ public override void OnInspectorGUI()
3433
obj => ValidationLogic(obj));
3534
}
3635

37-
private bool ValidationLogic(Object obj)
36+
protected override bool ValidationLogic(Object obj)
3837
{
3938
var registryInterface = typeof(ISOEventRegistry<>).MakeGenericType(_tArg);
4039
return obj is ISOEventBase && registryInterface.IsAssignableFrom(obj.GetType());

Editor/EditorUI/EventListenerWithArgCustomInspector.cs.meta renamed to Editor/EditorUI/EventListener1ArgCustomInspector.cs.meta

File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using SOBaseEvents.Refactor;
2+
using UnityEditor;
3+
using System;
4+
5+
namespace Editor.EditorUI
6+
{
7+
[CustomEditor(typeof(EventListener<,,>), true)]
8+
[CanEditMultipleObjects]
9+
public class EventListener2ArgCustomInspector : BaseEventListenerCustomInspector
10+
{
11+
Type _tArg;
12+
13+
private void OnEnable()
14+
{
15+
base.OnEnable();
16+
var type = target.GetType();
17+
18+
while (type != null && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(EventListener<,,>)))
19+
{
20+
type = type.BaseType;
21+
}
22+
23+
if (type != null)
24+
{
25+
_tArg = type.GetGenericArguments()[1];
26+
}
27+
}
28+
29+
public override void OnInspectorGUI()
30+
{
31+
DrawEventInspector("Event Asset",
32+
$"Must implement ISOEventRegistry<{(_tArg != null ? _tArg.Name : "T")}>",
33+
obj => ValidationLogic(obj));
34+
}
35+
36+
protected override bool ValidationLogic(Object obj)
37+
{
38+
var registryInterface = typeof(ISOEventRegistry<,>).MakeGenericType(_tArg);
39+
return obj is ISOEventBase && registryInterface.IsAssignableFrom(obj.GetType());
40+
}
41+
}
42+
}

Editor/EditorUI/EventListener2ArgCustomInspector.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using SOBaseEvents.Refactor;
2+
using UnityEditor;
3+
using System;
4+
5+
namespace Editor.EditorUI
6+
{
7+
[CustomEditor(typeof(EventListener<,,,>), true)]
8+
[CanEditMultipleObjects]
9+
public class EventListener3ArgCustomInspector : BaseEventListenerCustomInspector
10+
{
11+
Type _tArg;
12+
13+
private void OnEnable()
14+
{
15+
base.OnEnable();
16+
var type = target.GetType();
17+
18+
while (type != null && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(EventListener<,,,>)))
19+
{
20+
type = type.BaseType;
21+
}
22+
23+
if (type != null)
24+
{
25+
_tArg = type.GetGenericArguments()[1];
26+
}
27+
}
28+
29+
public override void OnInspectorGUI()
30+
{
31+
DrawEventInspector("Event Asset",
32+
$"Must implement ISOEventRegistry<{(_tArg != null ? _tArg.Name : "T")}>",
33+
obj => ValidationLogic(obj));
34+
}
35+
36+
protected override bool ValidationLogic(Object obj)
37+
{
38+
var registryInterface = typeof(ISOEventRegistry<,,>).MakeGenericType(_tArg);
39+
return obj is ISOEventBase && registryInterface.IsAssignableFrom(obj.GetType());
40+
}
41+
}
42+
}

Editor/EditorUI/EventListener3ArgCustomInspector.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/EditorUI/EventListenerCustomInspector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public override void OnInspectorGUI()
1515
obj => ValidationLogic(obj));
1616
}
1717

18-
private bool ValidationLogic(Object obj)
18+
protected override bool ValidationLogic(Object obj)
1919
{
2020
return obj is ISOEventBase && obj is ISOEventRegistry;
2121
}

Runtime/SOBaseEvents/Refactor/EventListener.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,54 @@ private void OnDisable()
5252
}
5353
}
5454
}
55+
56+
public abstract class EventListener<TEvent, TArg1, TArg2> : MonoBehaviour where TEvent : class, ISOEventRegistry<TArg1, TArg2>
57+
{
58+
public event Action<TArg1, TArg2> OnEventRaised;
59+
60+
[SerializeField] protected ScriptableObject _baseEvent;
61+
62+
protected TEvent _typedEvent => _baseEvent as TEvent;
63+
64+
private void OnEnable()
65+
{
66+
if (_typedEvent != null)
67+
{
68+
_typedEvent.AddListener(OnEventRaised);
69+
}
70+
}
71+
72+
private void OnDisable()
73+
{
74+
if (_typedEvent != null)
75+
{
76+
_typedEvent.RemoveListener(OnEventRaised);
77+
}
78+
}
79+
}
80+
81+
public abstract class EventListener<TEvent, TArg1, TArg2, TArg3> : MonoBehaviour where TEvent : class, ISOEventRegistry<TArg1, TArg2, TArg3>
82+
{
83+
public event Action<TArg1, TArg2, TArg3> OnEventRaised;
84+
85+
[SerializeField] protected ScriptableObject _baseEvent;
86+
87+
protected TEvent _typedEvent => _baseEvent as TEvent;
88+
89+
private void OnEnable()
90+
{
91+
if (_typedEvent != null)
92+
{
93+
_typedEvent.AddListener(OnEventRaised);
94+
}
95+
}
96+
97+
private void OnDisable()
98+
{
99+
if (_typedEvent != null)
100+
{
101+
_typedEvent.RemoveListener(OnEventRaised);
102+
}
103+
}
104+
}
55105
}

Runtime/SOBaseEvents/Refactor/EventSOBase.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,112 @@ public void RaiseEvent(TArg arg)
111111
return (objectName, methodName);
112112
}
113113
}
114+
115+
public abstract class EventSOBase<TArg1, TArg2> : ScriptableObject, ISOEventBase, ISOEventRegistry<TArg1, TArg2>, ISOEventRaiser<TArg1, TArg2>
116+
{
117+
private readonly List<Action<TArg1, TArg2>> _listeners = new();
118+
119+
public void AddListener(Action<TArg1, TArg2> listener)
120+
{
121+
if (_listeners.Contains(listener))
122+
{
123+
var (objectName, methodName) = GetListenerInfo(listener);
124+
Debug.LogError($"[ScriptableObjectEvents] Listener {methodName} of GameObject {objectName} already registered. Aborting registration.");
125+
return;
126+
}
127+
128+
_listeners.Add(listener);
129+
}
130+
131+
public void RemoveListener(Action<TArg1, TArg2> listener)
132+
{
133+
if (!_listeners.Contains(listener))
134+
{
135+
var (objectName, methodName) = GetListenerInfo(listener);
136+
Debug.LogError($"[ScriptableObjectEvents] Listener {methodName} of GameObject {objectName} is not registered. Aborting removal.");
137+
return;
138+
}
139+
140+
_listeners.Remove(listener);
141+
}
142+
143+
public void RaiseEvent(TArg1 arg1, TArg2 arg2)
144+
{
145+
for (int i = _listeners.Count - 1; i >= 0; i--)
146+
{
147+
_listeners[i]?.Invoke(arg1, arg2);
148+
}
149+
}
150+
151+
private (string objectName, string methodName) GetListenerInfo(Action<TArg1, TArg2> listener)
152+
{
153+
var objectName = string.Empty;
154+
var methodName = listener.Method.Name;
155+
156+
if (listener.Target is MonoBehaviour mb)
157+
{
158+
objectName = mb.gameObject.name;
159+
}
160+
else if (listener.Target != null)
161+
{
162+
objectName = listener.Target.ToString();
163+
}
164+
165+
return (objectName, methodName);
166+
}
167+
}
168+
169+
public abstract class EventSOBase<TArg1, TArg2, TArg3> : ScriptableObject, ISOEventBase, ISOEventRegistry<TArg1, TArg2, TArg3>, ISOEventRaiser<TArg1, TArg2, TArg3>
170+
{
171+
private readonly List<Action<TArg1, TArg2, TArg3>> _listeners = new();
172+
173+
public void AddListener(Action<TArg1, TArg2, TArg3> listener)
174+
{
175+
if (_listeners.Contains(listener))
176+
{
177+
var (objectName, methodName) = GetListenerInfo(listener);
178+
Debug.LogError($"[ScriptableObjectEvents] Listener {methodName} of GameObject {objectName} already registered. Aborting registration.");
179+
return;
180+
}
181+
182+
_listeners.Add(listener);
183+
}
184+
185+
public void RemoveListener(Action<TArg1, TArg2, TArg3> listener)
186+
{
187+
if (!_listeners.Contains(listener))
188+
{
189+
var (objectName, methodName) = GetListenerInfo(listener);
190+
Debug.LogError($"[ScriptableObjectEvents] Listener {methodName} of GameObject {objectName} is not registered. Aborting removal.");
191+
return;
192+
}
193+
194+
_listeners.Remove(listener);
195+
}
196+
197+
public void RaiseEvent(TArg1 arg1, TArg2 arg2, TArg3 arg3)
198+
{
199+
for (int i = _listeners.Count - 1; i >= 0; i--)
200+
{
201+
_listeners[i]?.Invoke(arg1, arg2, arg3);
202+
}
203+
}
204+
205+
private (string objectName, string methodName) GetListenerInfo(Action<TArg1, TArg2, TArg3> listener)
206+
{
207+
var objectName = string.Empty;
208+
var methodName = listener.Method.Name;
209+
210+
if (listener.Target is MonoBehaviour mb)
211+
{
212+
objectName = mb.gameObject.name;
213+
}
214+
else if (listener.Target != null)
215+
{
216+
objectName = listener.Target.ToString();
217+
}
218+
219+
return (objectName, methodName);
220+
}
221+
}
114222
}

0 commit comments

Comments
 (0)