TimeNotificationBehaviour.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Playables;
  4. namespace UnityEngine.Timeline
  5. {
  6. /// <summary>
  7. /// Use this PlayableBehaviour to send notifications at a given time.
  8. /// </summary>
  9. /// <seealso cref="UnityEngine.Timeline.NotificationFlags"/>
  10. public class TimeNotificationBehaviour : PlayableBehaviour
  11. {
  12. struct NotificationEntry
  13. {
  14. public double time;
  15. public INotification payload;
  16. public bool notificationFired;
  17. public NotificationFlags flags;
  18. public bool triggerInEditor
  19. {
  20. get { return (flags & NotificationFlags.TriggerInEditMode) != 0; }
  21. }
  22. public bool prewarm
  23. {
  24. get { return (flags & NotificationFlags.Retroactive) != 0; }
  25. }
  26. public bool triggerOnce
  27. {
  28. get { return (flags & NotificationFlags.TriggerOnce) != 0; }
  29. }
  30. }
  31. readonly List<NotificationEntry> m_Notifications = new List<NotificationEntry>();
  32. double m_PreviousTime;
  33. bool m_NeedSortNotifications;
  34. Playable m_TimeSource;
  35. /// <summary>
  36. /// Sets an optional Playable that provides duration and Wrap mode information.
  37. /// </summary>
  38. /// <remarks>
  39. /// timeSource is optional. By default, the duration and Wrap mode will come from the current Playable.
  40. /// </remarks>
  41. public Playable timeSource
  42. {
  43. set { m_TimeSource = value; }
  44. }
  45. /// <summary>
  46. /// Creates and initializes a ScriptPlayable with a TimeNotificationBehaviour.
  47. /// </summary>
  48. /// <param name="graph">The playable graph.</param>
  49. /// <param name="duration">The duration of the playable.</param>
  50. /// <param name="loopMode">The loop mode of the playable.</param>
  51. /// <returns>A new TimeNotificationBehaviour linked to the PlayableGraph.</returns>
  52. public static ScriptPlayable<TimeNotificationBehaviour> Create(PlayableGraph graph, double duration, DirectorWrapMode loopMode)
  53. {
  54. var notificationsPlayable = ScriptPlayable<TimeNotificationBehaviour>.Create(graph);
  55. notificationsPlayable.SetDuration(duration);
  56. notificationsPlayable.SetTimeWrapMode(loopMode);
  57. notificationsPlayable.SetPropagateSetTime(true);
  58. return notificationsPlayable;
  59. }
  60. /// <summary>
  61. /// Adds a notification to be sent with flags, at a specific time.
  62. /// </summary>
  63. /// <param name="time">The time to send the notification.</param>
  64. /// <param name="payload">The notification.</param>
  65. /// <param name="flags">The notification flags that determine the notification behaviour. This parameter is set to Retroactive by default.</param>
  66. /// <seealso cref="UnityEngine.Timeline.NotificationFlags"/>
  67. public void AddNotification(double time, INotification payload, NotificationFlags flags = NotificationFlags.Retroactive)
  68. {
  69. m_Notifications.Add(new NotificationEntry
  70. {
  71. time = time,
  72. payload = payload,
  73. flags = flags
  74. });
  75. m_NeedSortNotifications = true;
  76. }
  77. /// <summary>
  78. /// This method is called when the PlayableGraph that owns this PlayableBehaviour starts.
  79. /// </summary>
  80. /// <param name="playable">The reference to the playable associated with this PlayableBehaviour.</param>
  81. public override void OnGraphStart(Playable playable)
  82. {
  83. SortNotifications();
  84. for (var i = 0; i < m_Notifications.Count; i++)
  85. {
  86. var notification = m_Notifications[i];
  87. notification.notificationFired = false;
  88. m_Notifications[i] = notification;
  89. }
  90. m_PreviousTime = playable.GetTime();
  91. }
  92. /// <summary>
  93. /// This method is called when the Playable play state is changed to PlayState.Paused
  94. /// </summary>
  95. /// <param name="playable">The reference to the playable associated with this PlayableBehaviour.</param>
  96. /// <param name="info">Playable context information such as weight, evaluationType, and so on.</param>
  97. public override void OnBehaviourPause(Playable playable, FrameData info)
  98. {
  99. if (playable.IsDone())
  100. {
  101. SortNotifications();
  102. for (var i = 0; i < m_Notifications.Count; i++)
  103. {
  104. var e = m_Notifications[i];
  105. if (!e.notificationFired)
  106. {
  107. var duration = playable.GetDuration();
  108. var canTrigger = m_PreviousTime <= e.time && e.time <= duration;
  109. if (canTrigger)
  110. {
  111. Trigger_internal(playable, info.output, ref e);
  112. m_Notifications[i] = e;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// This method is called during the PrepareFrame phase of the PlayableGraph.
  120. /// </summary>
  121. /// <remarks>
  122. /// Called once before processing starts.
  123. /// </remarks>
  124. /// <param name="playable">The reference to the playable associated with this PlayableBehaviour.</param>
  125. /// <param name="info">Playable context information such as weight, evaluationType, and so on.</param>
  126. public override void PrepareFrame(Playable playable, FrameData info)
  127. {
  128. // Never trigger on scrub
  129. if (info.evaluationType == FrameData.EvaluationType.Evaluate)
  130. {
  131. return;
  132. }
  133. SyncDurationWithExternalSource(playable);
  134. SortNotifications();
  135. var currentTime = playable.GetTime();
  136. // Fire notifications from previousTime till the end
  137. if (info.timeLooped)
  138. {
  139. var duration = playable.GetDuration();
  140. TriggerNotificationsInRange(m_PreviousTime, duration, info, playable, true);
  141. var dx = playable.GetDuration() - m_PreviousTime;
  142. var nFullTimelines = (int)((info.deltaTime * info.effectiveSpeed - dx) / playable.GetDuration());
  143. for (var i = 0; i < nFullTimelines; i++)
  144. {
  145. TriggerNotificationsInRange(0, duration, info, playable, false);
  146. }
  147. TriggerNotificationsInRange(0, currentTime, info, playable, false);
  148. }
  149. else
  150. {
  151. var pt = playable.GetTime();
  152. TriggerNotificationsInRange(m_PreviousTime, pt, info,
  153. playable, true);
  154. }
  155. for (var i = 0; i < m_Notifications.Count; ++i)
  156. {
  157. var e = m_Notifications[i];
  158. if (e.notificationFired && CanRestoreNotification(e, info, currentTime, m_PreviousTime))
  159. {
  160. Restore_internal(ref e);
  161. m_Notifications[i] = e;
  162. }
  163. }
  164. m_PreviousTime = playable.GetTime();
  165. }
  166. void SortNotifications()
  167. {
  168. if (m_NeedSortNotifications)
  169. {
  170. m_Notifications.Sort((x, y) => x.time.CompareTo(y.time));
  171. m_NeedSortNotifications = false;
  172. }
  173. }
  174. static bool CanRestoreNotification(NotificationEntry e, FrameData info, double currentTime, double previousTime)
  175. {
  176. if (e.triggerOnce)
  177. return false;
  178. if (info.timeLooped)
  179. return true;
  180. //case 1111595: restore the notification if the time is manually set before it
  181. return previousTime > currentTime && currentTime <= e.time;
  182. }
  183. void TriggerNotificationsInRange(double start, double end, FrameData info, Playable playable, bool checkState)
  184. {
  185. if (start <= end)
  186. {
  187. var playMode = Application.isPlaying;
  188. for (var i = 0; i < m_Notifications.Count; i++)
  189. {
  190. var e = m_Notifications[i];
  191. if (e.notificationFired && (checkState || e.triggerOnce))
  192. continue;
  193. var notificationTime = e.time;
  194. if (e.prewarm && notificationTime < end && (e.triggerInEditor || playMode))
  195. {
  196. Trigger_internal(playable, info.output, ref e);
  197. m_Notifications[i] = e;
  198. }
  199. else
  200. {
  201. if (notificationTime < start || notificationTime > end)
  202. continue;
  203. if (e.triggerInEditor || playMode)
  204. {
  205. Trigger_internal(playable, info.output, ref e);
  206. m_Notifications[i] = e;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. void SyncDurationWithExternalSource(Playable playable)
  213. {
  214. if (m_TimeSource.IsValid())
  215. {
  216. playable.SetDuration(m_TimeSource.GetDuration());
  217. playable.SetTimeWrapMode(m_TimeSource.GetTimeWrapMode());
  218. }
  219. }
  220. static void Trigger_internal(Playable playable, PlayableOutput output, ref NotificationEntry e)
  221. {
  222. output.PushNotification(playable, e.payload);
  223. e.notificationFired = true;
  224. }
  225. static void Restore_internal(ref NotificationEntry e)
  226. {
  227. e.notificationFired = false;
  228. }
  229. }
  230. }