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.

TimeNotificationBehaviour.cs 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. var currentTime = playable.GetTime();
  85. for (var i = 0; i < m_Notifications.Count; i++)
  86. {
  87. // case 1257208 - when a timeline is _resumed_, only reset notifications after the resumed time
  88. if (m_Notifications[i].time > currentTime && !m_Notifications[i].triggerOnce)
  89. {
  90. var notification = m_Notifications[i];
  91. notification.notificationFired = false;
  92. m_Notifications[i] = notification;
  93. }
  94. }
  95. m_PreviousTime = playable.GetTime();
  96. }
  97. /// <summary>
  98. /// This method is called when the Playable play state is changed to PlayState.Paused
  99. /// </summary>
  100. /// <param name="playable">The reference to the playable associated with this PlayableBehaviour.</param>
  101. /// <param name="info">Playable context information such as weight, evaluationType, and so on.</param>
  102. public override void OnBehaviourPause(Playable playable, FrameData info)
  103. {
  104. if (playable.IsDone())
  105. {
  106. SortNotifications();
  107. for (var i = 0; i < m_Notifications.Count; i++)
  108. {
  109. var e = m_Notifications[i];
  110. if (!e.notificationFired)
  111. {
  112. var duration = playable.GetDuration();
  113. var canTrigger = m_PreviousTime <= e.time && e.time <= duration;
  114. if (canTrigger)
  115. {
  116. Trigger_internal(playable, info.output, ref e);
  117. m_Notifications[i] = e;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// This method is called during the PrepareFrame phase of the PlayableGraph.
  125. /// </summary>
  126. /// <remarks>
  127. /// Called once before processing starts.
  128. /// </remarks>
  129. /// <param name="playable">The reference to the playable associated with this PlayableBehaviour.</param>
  130. /// <param name="info">Playable context information such as weight, evaluationType, and so on.</param>
  131. public override void PrepareFrame(Playable playable, FrameData info)
  132. {
  133. // Never trigger on scrub
  134. if (info.evaluationType == FrameData.EvaluationType.Evaluate)
  135. {
  136. return;
  137. }
  138. SyncDurationWithExternalSource(playable);
  139. SortNotifications();
  140. var currentTime = playable.GetTime();
  141. // Fire notifications from previousTime till the end
  142. if (info.timeLooped)
  143. {
  144. var duration = playable.GetDuration();
  145. TriggerNotificationsInRange(m_PreviousTime, duration, info, playable, true);
  146. var dx = playable.GetDuration() - m_PreviousTime;
  147. var nFullTimelines = (int)((info.deltaTime * info.effectiveSpeed - dx) / playable.GetDuration());
  148. for (var i = 0; i < nFullTimelines; i++)
  149. {
  150. TriggerNotificationsInRange(0, duration, info, playable, false);
  151. }
  152. TriggerNotificationsInRange(0, currentTime, info, playable, false);
  153. }
  154. else
  155. {
  156. var pt = playable.GetTime();
  157. TriggerNotificationsInRange(m_PreviousTime, pt, info,
  158. playable, true);
  159. }
  160. for (var i = 0; i < m_Notifications.Count; ++i)
  161. {
  162. var e = m_Notifications[i];
  163. if (e.notificationFired && CanRestoreNotification(e, info, currentTime, m_PreviousTime))
  164. {
  165. Restore_internal(ref e);
  166. m_Notifications[i] = e;
  167. }
  168. }
  169. m_PreviousTime = playable.GetTime();
  170. }
  171. void SortNotifications()
  172. {
  173. if (m_NeedSortNotifications)
  174. {
  175. m_Notifications.Sort((x, y) => x.time.CompareTo(y.time));
  176. m_NeedSortNotifications = false;
  177. }
  178. }
  179. static bool CanRestoreNotification(NotificationEntry e, FrameData info, double currentTime, double previousTime)
  180. {
  181. if (e.triggerOnce)
  182. return false;
  183. if (info.timeLooped)
  184. return true;
  185. //case 1111595: restore the notification if the time is manually set before it
  186. return previousTime > currentTime && currentTime <= e.time;
  187. }
  188. void TriggerNotificationsInRange(double start, double end, FrameData info, Playable playable, bool checkState)
  189. {
  190. if (start <= end)
  191. {
  192. var playMode = Application.isPlaying;
  193. for (var i = 0; i < m_Notifications.Count; i++)
  194. {
  195. var e = m_Notifications[i];
  196. if (e.notificationFired && (checkState || e.triggerOnce))
  197. continue;
  198. var notificationTime = e.time;
  199. if (e.prewarm && notificationTime < end && (e.triggerInEditor || playMode))
  200. {
  201. Trigger_internal(playable, info.output, ref e);
  202. m_Notifications[i] = e;
  203. }
  204. else
  205. {
  206. if (notificationTime < start || notificationTime > end)
  207. continue;
  208. if (e.triggerInEditor || playMode)
  209. {
  210. Trigger_internal(playable, info.output, ref e);
  211. m_Notifications[i] = e;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. void SyncDurationWithExternalSource(Playable playable)
  218. {
  219. if (m_TimeSource.IsValid())
  220. {
  221. playable.SetDuration(m_TimeSource.GetDuration());
  222. playable.SetTimeWrapMode(m_TimeSource.GetTimeWrapMode());
  223. }
  224. }
  225. static void Trigger_internal(Playable playable, PlayableOutput output, ref NotificationEntry e)
  226. {
  227. output.PushNotification(playable, e.payload);
  228. e.notificationFired = true;
  229. }
  230. static void Restore_internal(ref NotificationEntry e)
  231. {
  232. e.notificationFired = false;
  233. }
  234. }
  235. }