NotificationUtilities.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Playables;
  4. namespace UnityEngine.Timeline
  5. {
  6. static class NotificationUtilities
  7. {
  8. public static ScriptPlayable<TimeNotificationBehaviour> CreateNotificationsPlayable(PlayableGraph graph, IEnumerable<IMarker> markers, GameObject go)
  9. {
  10. var notificationPlayable = ScriptPlayable<TimeNotificationBehaviour>.Null;
  11. var director = go.GetComponent<PlayableDirector>();
  12. foreach (var e in markers)
  13. {
  14. var notif = e as INotification;
  15. if (notif == null)
  16. continue;
  17. if (notificationPlayable.Equals(ScriptPlayable<TimeNotificationBehaviour>.Null))
  18. {
  19. notificationPlayable = TimeNotificationBehaviour.Create(graph,
  20. director.playableAsset.duration, director.extrapolationMode);
  21. }
  22. var time = (DiscreteTime)e.time;
  23. var tlDuration = (DiscreteTime)director.playableAsset.duration;
  24. if (time >= tlDuration && time <= tlDuration.OneTickAfter() && tlDuration != 0)
  25. {
  26. time = tlDuration.OneTickBefore();
  27. }
  28. var notificationOptionProvider = e as INotificationOptionProvider;
  29. if (notificationOptionProvider != null)
  30. {
  31. notificationPlayable.GetBehaviour().AddNotification((double)time, notif, notificationOptionProvider.flags);
  32. }
  33. else
  34. {
  35. notificationPlayable.GetBehaviour().AddNotification((double)time, notif);
  36. }
  37. }
  38. return notificationPlayable;
  39. }
  40. public static bool TrackTypeSupportsNotifications(Type type)
  41. {
  42. var binding = (TrackBindingTypeAttribute)Attribute.GetCustomAttribute(type, typeof(TrackBindingTypeAttribute));
  43. return binding != null &&
  44. (typeof(Component).IsAssignableFrom(binding.type) ||
  45. typeof(GameObject).IsAssignableFrom(binding.type));
  46. }
  47. }
  48. }