SignalReceiver.cs 8.6 KB

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