Skip to content

Commit 2ba033f

Browse files
committed
proper casting to unity event base
1 parent 70c3fdc commit 2ba033f

4 files changed

Lines changed: 59 additions & 6 deletions

File tree

Assets/CustomEventTest.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.Events;
5+
6+
public class CustomEventTest : MonoBehaviour
7+
{
8+
public UnityEvent simpleEvent;
9+
public CustomComplexEvent complexEvent;
10+
11+
12+
public void TriggerSimpleEvent()
13+
{
14+
if(simpleEvent != null)
15+
{
16+
simpleEvent.Invoke();
17+
}
18+
}
19+
20+
public void TriggerComplexEvent()
21+
{
22+
if (complexEvent != null)
23+
{
24+
complexEvent.Invoke("a", 1, 2);
25+
}
26+
StartCoroutine(DelayedTrigger("Test", 0, 1));
27+
}
28+
29+
private IEnumerator DelayedTrigger(string message, int a, int b)
30+
{
31+
for(int i = 0; i < 20; i++)
32+
{
33+
yield return new WaitForSeconds(0.1f);
34+
if (complexEvent != null)
35+
{
36+
complexEvent.Invoke(message, a, b);
37+
}
38+
}
39+
}
40+
}
41+
42+
[System.Serializable]
43+
public class CustomComplexEvent : UnityEvent<string, int, int> { }

Assets/CustomEventTest.cs.meta

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

Assets/EventVisualizer/Editor/EventsVisualizer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static List<EventCall> ExtractEvents(Component caller)
4141
SerializedProperty persistentCalls = iterator.FindPropertyRelative("m_PersistentCalls.m_Calls");
4242
if (persistentCalls != null)
4343
{
44-
UnityEvent unityEvent = FindEvent(caller, iterator);
44+
UnityEventBase unityEvent = FindEvent(caller, iterator);
4545
for (int i = 0; i < persistentCalls.arraySize; ++i)
4646
{
4747
SerializedProperty methodName = persistentCalls.GetArrayElementAtIndex(i).FindPropertyRelative("m_MethodName");
@@ -122,7 +122,7 @@ public static List<GameObject> GetAllObjectsInScene(bool rootOnly = false)
122122
}
123123

124124

125-
private static UnityEvent FindEvent(Component caller, SerializedProperty iterator)
125+
private static UnityEventBase FindEvent(Component caller, SerializedProperty iterator)
126126
{
127127
PropertyInfo eventPropertyInfo = caller.GetType().GetProperty(iterator.propertyPath);
128128
if (eventPropertyInfo == null)
@@ -134,8 +134,7 @@ private static UnityEvent FindEvent(Component caller, SerializedProperty iterato
134134
}
135135
if (eventPropertyInfo != null)
136136
{
137-
UnityEvent trigger = eventPropertyInfo.GetValue(caller, null) as UnityEvent;
138-
return trigger;
137+
return eventPropertyInfo.GetValue(caller, null) as UnityEventBase;
139138
}
140139

141140
FieldInfo eventFieldInfo = caller.GetType().GetField(iterator.propertyPath);
@@ -148,8 +147,7 @@ private static UnityEvent FindEvent(Component caller, SerializedProperty iterato
148147
}
149148
if (eventFieldInfo != null)
150149
{
151-
UnityEvent trigger = eventFieldInfo.GetValue(caller) as UnityEvent;
152-
return trigger;
150+
return eventFieldInfo.GetValue(caller) as UnityEventBase;
153151
}
154152
return null;
155153
}

Assets/_Scenes/testScene.unity

2.41 KB
Binary file not shown.

0 commit comments

Comments
 (0)