summaryrefslogtreecommitdiff
path: root/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-14 22:50:43 +0800
committerchai <chaifix@163.com>2019-08-14 22:50:43 +0800
commit15740faf9fe9fe4be08965098bbf2947e096aeeb (patch)
treea730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs
+Unity Runtime codeHEADmaster
Diffstat (limited to 'Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs')
-rw-r--r--Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs51
1 files changed, 51 insertions, 0 deletions
diff --git a/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs
new file mode 100644
index 0000000..66bf531
--- /dev/null
+++ b/Runtime/Graphs/UnityEngine.Graphs/MonoBehaviourEventDummies/OnAnimationEventDummy.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace UnityEngine.Graphs.LogicGraph
+{
+ // This component gets attached to a GameObject with Animation component and handles special LogicGraphEvent.
+ public class OnAnimationEventDummy : MonoBehaviour
+ {
+ private Dictionary<string, Action> m_Events = new Dictionary<string, Action> ();
+
+ public static void AttachToGameObject(Animation component, string eventName, Action delegateToCall)
+ {
+ var animEventDummy = component.gameObject.GetComponent (typeof (OnAnimationEventDummy)) as OnAnimationEventDummy ??
+ component.gameObject.AddComponent(typeof(OnAnimationEventDummy)) as OnAnimationEventDummy;
+
+ if (animEventDummy == null)
+ throw new ArgumentException("Failed to attach Logic Graph Animation Event handler to a game object of component '" + component + "'.");
+
+ animEventDummy.AddNewEvent(eventName, delegateToCall);
+ }
+
+ private void AddNewEvent(string eventName, Action delegateToCall)
+ {
+ if (!m_Events.ContainsKey(eventName))
+ m_Events.Add(eventName, delegateToCall);
+ else
+ m_Events[eventName] += delegateToCall;
+ }
+
+ public void LogicGraphEvent(string eventName)
+ {
+ Action delegateToCall;
+
+ if (!m_Events.TryGetValue(eventName, out delegateToCall))
+ {
+ Debug.LogError("Logic Graph failed to handle Animation Event '" + eventName + "'. Receiver was not found.");
+ return;
+ }
+
+ if (delegateToCall == null)
+ {
+ Debug.LogError("Logic Graph failed to handle Animation Event '" + eventName + "'. Receiver was null.");
+ return;
+ }
+
+ delegateToCall ();
+ }
+ }
+}
+