No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SignalReceiver.cs 9.1KB

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