SignalReceiver.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.Playables;
  6. namespace UnityEngine.Timeline
  7. {
  8. /// <summary>
  9. /// Listens for emitted signals and reacts depending on its defined reactions.
  10. /// </summary>
  11. /// A SignalReceiver contains a list of reactions. Each reaction is bound to a SignalAsset.
  12. /// When a SignalEmitter emits a signal, the SignalReceiver invokes the corresponding reaction.
  13. /// <seealso cref="UnityEngine.Timeline.SignalEmitter"/>
  14. /// <seealso cref="UnityEngine.Timeline.SignalAsset"/>
  15. public class SignalReceiver : MonoBehaviour, INotificationReceiver
  16. {
  17. [SerializeField]
  18. EventKeyValue m_Events = new EventKeyValue();
  19. /// <summary>
  20. /// Called when a notification is sent.
  21. /// </summary>
  22. /// <param name="origin">The playable that sent the notification.</param>
  23. /// <param name="notification">The received notification. Only notifications of type <see cref="SignalEmitter"/> will be processed.</param>
  24. /// <param name="context">User defined data that depends on the type of notification. Uses this to pass necessary information that can change with each invocation.</param>
  25. public void OnNotify(Playable origin, INotification notification, object context)
  26. {
  27. var signal = notification as SignalEmitter;
  28. if (signal != null && signal.asset != null)
  29. {
  30. UnityEvent evt;
  31. if (m_Events.TryGetValue(signal.asset, out evt) && evt != null)
  32. {
  33. evt.Invoke();
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Defines a new reaction for a SignalAsset.
  39. /// </summary>
  40. /// <param name="asset">The SignalAsset for which the reaction is being defined.</param>
  41. /// <param name="reaction">The UnityEvent that describes the reaction.</param>
  42. /// <exception cref="ArgumentNullException">Thrown when the asset is null.</exception>
  43. /// <exception cref="ArgumentException">Thrown when the SignalAsset is already registered with this receiver.</exception>
  44. public void AddReaction(SignalAsset asset, UnityEvent reaction)
  45. {
  46. if (asset == null)
  47. throw new ArgumentNullException("asset");
  48. if (m_Events.signals.Contains(asset))
  49. throw new ArgumentException("SignalAsset already used.");
  50. m_Events.Append(asset, reaction);
  51. }
  52. /// <summary>
  53. /// Appends a null SignalAsset with a reaction specified by the UnityEvent.
  54. /// </summary>
  55. /// <param name="reaction">The new reaction to be appended.</param>
  56. /// <returns>The index of the appended reaction.</returns>
  57. /// <remarks>Multiple null assets are valid.</remarks>
  58. public int AddEmptyReaction(UnityEvent reaction)
  59. {
  60. m_Events.Append(null, reaction);
  61. return m_Events.events.Count - 1;
  62. }
  63. /// <summary>
  64. /// Removes the first occurrence of a SignalAsset.
  65. /// </summary>
  66. /// <param name="asset">The SignalAsset to be removed.</param>
  67. public void Remove(SignalAsset asset)
  68. {
  69. if (!m_Events.signals.Contains(asset))
  70. {
  71. throw new ArgumentException("The SignalAsset is not registered with this receiver.");
  72. }
  73. m_Events.Remove(asset);
  74. }
  75. /// <summary>
  76. /// Gets a list of all registered SignalAssets.
  77. /// </summary>
  78. /// <returns>Returns a list of SignalAssets.</returns>
  79. public IEnumerable<SignalAsset> GetRegisteredSignals()
  80. {
  81. return m_Events.signals;
  82. }
  83. /// <summary>
  84. /// Gets the first UnityEvent associated with a SignalAsset.
  85. /// </summary>
  86. /// <param name="key">A SignalAsset defining the signal.</param>
  87. /// <returns>Returns the reaction associated with a SignalAsset. Returns null if the signal asset does not exist.</returns>
  88. public UnityEvent GetReaction(SignalAsset key)
  89. {
  90. UnityEvent ret;
  91. if (m_Events.TryGetValue(key, out ret))
  92. {
  93. return ret;
  94. }
  95. return null;
  96. }
  97. /// <summary>
  98. /// Returns the count of registered SignalAssets.
  99. /// </summary>
  100. /// <returns></returns>
  101. public int Count()
  102. {
  103. return m_Events.signals.Count;
  104. }
  105. /// <summary>
  106. /// Replaces the SignalAsset associated with a reaction at a specific index.
  107. /// </summary>
  108. /// <param name="idx">The index of the reaction.</param>
  109. /// <param name="newKey">The replacement SignalAsset.</param>
  110. /// <exception cref="ArgumentException">Thrown when the replacement SignalAsset is already registered to this SignalReceiver.</exception>
  111. /// <remarks>The new SignalAsset can be null.</remarks>
  112. public void ChangeSignalAtIndex(int idx, SignalAsset newKey)
  113. {
  114. if (idx < 0 || idx > m_Events.signals.Count - 1)
  115. throw new IndexOutOfRangeException();
  116. if (m_Events.signals[idx] == newKey)
  117. return;
  118. var alreadyUsed = m_Events.signals.Contains(newKey);
  119. if (newKey == null || m_Events.signals[idx] == null || !alreadyUsed)
  120. m_Events.signals[idx] = newKey;
  121. if (newKey != null && alreadyUsed)
  122. throw new ArgumentException("SignalAsset already used.");
  123. }
  124. /// <summary>
  125. /// Removes the SignalAsset and reaction at a specific index.
  126. /// </summary>
  127. /// <param name="idx">The index of the SignalAsset to be removed.</param>
  128. public void RemoveAtIndex(int idx)
  129. {
  130. if (idx < 0 || idx > m_Events.signals.Count - 1)
  131. throw new IndexOutOfRangeException();
  132. m_Events.Remove(idx);
  133. }
  134. /// <summary>
  135. /// Replaces the reaction at a specific index with a new UnityEvent.
  136. /// </summary>
  137. /// <param name="idx">The index of the reaction to be replaced.</param>
  138. /// <param name="reaction">The replacement reaction.</param>
  139. /// <exception cref="ArgumentNullException">Thrown when the replacement reaction is null.</exception>
  140. public void ChangeReactionAtIndex(int idx, UnityEvent reaction)
  141. {
  142. if (idx < 0 || idx > m_Events.events.Count - 1)
  143. throw new IndexOutOfRangeException();
  144. m_Events.events[idx] = reaction;
  145. }
  146. /// <summary>
  147. /// Gets the reaction at a specific index.
  148. /// </summary>
  149. /// <param name="idx">The index of the reaction.</param>
  150. /// <returns>Returns a reaction.</returns>
  151. public UnityEvent GetReactionAtIndex(int idx)
  152. {
  153. if (idx < 0 || idx > m_Events.events.Count - 1)
  154. throw new IndexOutOfRangeException();
  155. return m_Events.events[idx];
  156. }
  157. /// <summary>
  158. /// Gets the SignalAsset at a specific index
  159. /// </summary>
  160. /// <param name="idx">The index of the SignalAsset.</param>
  161. /// <returns>Returns a SignalAsset.</returns>
  162. public SignalAsset GetSignalAssetAtIndex(int idx)
  163. {
  164. if (idx < 0 || idx > m_Events.signals.Count - 1)
  165. throw new IndexOutOfRangeException();
  166. return m_Events.signals[idx];
  167. }
  168. // Required by Unity for the MonoBehaviour to have an enabled state
  169. private void OnEnable()
  170. {
  171. }
  172. [Serializable]
  173. class EventKeyValue
  174. {
  175. [SerializeField]
  176. List<SignalAsset> m_Signals = new List<SignalAsset>();
  177. [SerializeField, CustomSignalEventDrawer]
  178. List<UnityEvent> m_Events = new List<UnityEvent>();
  179. public bool TryGetValue(SignalAsset key, out UnityEvent value)
  180. {
  181. var index = m_Signals.IndexOf(key);
  182. if (index != -1)
  183. {
  184. value = m_Events[index];
  185. return true;
  186. }
  187. value = null;
  188. return false;
  189. }
  190. public void Append(SignalAsset key, UnityEvent value)
  191. {
  192. m_Signals.Add(key);
  193. m_Events.Add(value);
  194. }
  195. public void Remove(int idx)
  196. {
  197. if (idx != -1)
  198. {
  199. m_Signals.RemoveAt(idx);
  200. m_Events.RemoveAt(idx);
  201. }
  202. }
  203. public void Remove(SignalAsset key)
  204. {
  205. var idx = m_Signals.IndexOf(key);
  206. if (idx != -1)
  207. {
  208. m_Signals.RemoveAt(idx);
  209. m_Events.RemoveAt(idx);
  210. }
  211. }
  212. public List<SignalAsset> signals
  213. {
  214. get { return m_Signals; }
  215. }
  216. public List<UnityEvent> events
  217. {
  218. get { return m_Events; }
  219. }
  220. }
  221. }
  222. }