Skip to content

Commit

Permalink
proper casting to unity event base
Browse files Browse the repository at this point in the history
  • Loading branch information
MephestoKhaan committed Dec 5, 2017
1 parent 70c3fdc commit 2ba033f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
43 changes: 43 additions & 0 deletions Assets/CustomEventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class CustomEventTest : MonoBehaviour
{
public UnityEvent simpleEvent;
public CustomComplexEvent complexEvent;


public void TriggerSimpleEvent()
{
if(simpleEvent != null)
{
simpleEvent.Invoke();
}
}

public void TriggerComplexEvent()
{
if (complexEvent != null)
{
complexEvent.Invoke("a", 1, 2);
}
StartCoroutine(DelayedTrigger("Test", 0, 1));
}

private IEnumerator DelayedTrigger(string message, int a, int b)
{
for(int i = 0; i < 20; i++)
{
yield return new WaitForSeconds(0.1f);
if (complexEvent != null)
{
complexEvent.Invoke(message, a, b);
}
}
}
}

[System.Serializable]
public class CustomComplexEvent : UnityEvent<string, int, int> { }
12 changes: 12 additions & 0 deletions Assets/CustomEventTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions Assets/EventVisualizer/Editor/EventsVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static List<EventCall> ExtractEvents(Component caller)
SerializedProperty persistentCalls = iterator.FindPropertyRelative("m_PersistentCalls.m_Calls");
if (persistentCalls != null)
{
UnityEvent unityEvent = FindEvent(caller, iterator);
UnityEventBase unityEvent = FindEvent(caller, iterator);
for (int i = 0; i < persistentCalls.arraySize; ++i)
{
SerializedProperty methodName = persistentCalls.GetArrayElementAtIndex(i).FindPropertyRelative("m_MethodName");
Expand Down Expand Up @@ -122,7 +122,7 @@ public static List<GameObject> GetAllObjectsInScene(bool rootOnly = false)
}


private static UnityEvent FindEvent(Component caller, SerializedProperty iterator)
private static UnityEventBase FindEvent(Component caller, SerializedProperty iterator)
{
PropertyInfo eventPropertyInfo = caller.GetType().GetProperty(iterator.propertyPath);
if (eventPropertyInfo == null)
Expand All @@ -134,8 +134,7 @@ private static UnityEvent FindEvent(Component caller, SerializedProperty iterato
}
if (eventPropertyInfo != null)
{
UnityEvent trigger = eventPropertyInfo.GetValue(caller, null) as UnityEvent;
return trigger;
return eventPropertyInfo.GetValue(caller, null) as UnityEventBase;
}

FieldInfo eventFieldInfo = caller.GetType().GetField(iterator.propertyPath);
Expand All @@ -148,8 +147,7 @@ private static UnityEvent FindEvent(Component caller, SerializedProperty iterato
}
if (eventFieldInfo != null)
{
UnityEvent trigger = eventFieldInfo.GetValue(caller) as UnityEvent;
return trigger;
return eventFieldInfo.GetValue(caller) as UnityEventBase;
}
return null;
}
Expand Down
Binary file modified Assets/_Scenes/testScene.unity
Binary file not shown.

0 comments on commit 2ba033f

Please sign in to comment.