MarkerList.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Playables;
  4. namespace UnityEngine.Timeline
  5. {
  6. [Serializable]
  7. struct MarkerList : ISerializationCallbackReceiver
  8. {
  9. [SerializeField, HideInInspector] List<ScriptableObject> m_Objects;
  10. [HideInInspector, NonSerialized] List<IMarker> m_Cache;
  11. bool m_CacheDirty;
  12. bool m_HasNotifications;
  13. public List<IMarker> markers
  14. {
  15. get
  16. {
  17. BuildCache();
  18. return m_Cache;
  19. }
  20. }
  21. public MarkerList(int capacity)
  22. {
  23. m_Objects = new List<ScriptableObject>(capacity);
  24. m_Cache = new List<IMarker>(capacity);
  25. m_CacheDirty = true;
  26. m_HasNotifications = false;
  27. }
  28. public void Add(ScriptableObject item)
  29. {
  30. if (item == null)
  31. return;
  32. m_Objects.Add(item);
  33. m_CacheDirty = true;
  34. }
  35. public bool Remove(IMarker item)
  36. {
  37. if (!(item is ScriptableObject))
  38. throw new InvalidOperationException("Supplied type must be a ScriptableObject");
  39. return Remove((ScriptableObject)item, item.parent.timelineAsset, item.parent);
  40. }
  41. public bool Remove(ScriptableObject item, TimelineAsset timelineAsset, PlayableAsset thingToDirty)
  42. {
  43. if (!m_Objects.Contains(item)) return false;
  44. TimelineUndo.PushUndo(thingToDirty, "Delete Marker");
  45. m_Objects.Remove(item);
  46. m_CacheDirty = true;
  47. TimelineUndo.PushDestroyUndo(timelineAsset, thingToDirty, item, "Delete Marker");
  48. return true;
  49. }
  50. public void Clear()
  51. {
  52. m_Objects.Clear();
  53. m_CacheDirty = true;
  54. }
  55. public bool Contains(ScriptableObject item)
  56. {
  57. return m_Objects.Contains(item);
  58. }
  59. public IEnumerable<IMarker> GetMarkers()
  60. {
  61. return markers;
  62. }
  63. public int Count
  64. {
  65. get { return markers.Count; }
  66. }
  67. public IMarker this[int idx]
  68. {
  69. get
  70. {
  71. return markers[idx];
  72. }
  73. }
  74. public List<ScriptableObject> GetRawMarkerList()
  75. {
  76. return m_Objects;
  77. }
  78. public IMarker CreateMarker(Type type, double time, TrackAsset owner)
  79. {
  80. if (!typeof(ScriptableObject).IsAssignableFrom(type) || !typeof(IMarker).IsAssignableFrom(type))
  81. {
  82. throw new InvalidOperationException(
  83. "The requested type needs to inherit from ScriptableObject and implement IMarker");
  84. }
  85. if (!owner.supportsNotifications && typeof(INotification).IsAssignableFrom(type))
  86. {
  87. throw new InvalidOperationException(
  88. "Markers implementing the INotification interface cannot be added on tracks that do not support notifications");
  89. }
  90. var markerSO = ScriptableObject.CreateInstance(type);
  91. var marker = (IMarker)markerSO;
  92. marker.time = time;
  93. TimelineCreateUtilities.SaveAssetIntoObject(markerSO, owner);
  94. TimelineUndo.RegisterCreatedObjectUndo(markerSO, "Create " + type.Name);
  95. TimelineUndo.PushUndo(owner, "Create " + type.Name);
  96. Add(markerSO);
  97. marker.Initialize(owner);
  98. return marker;
  99. }
  100. public bool HasNotifications()
  101. {
  102. BuildCache();
  103. return m_HasNotifications;
  104. }
  105. void ISerializationCallbackReceiver.OnBeforeSerialize()
  106. {
  107. }
  108. void ISerializationCallbackReceiver.OnAfterDeserialize()
  109. {
  110. #if UNITY_EDITOR
  111. for (int i = m_Objects.Count - 1; i >= 0; i--)
  112. {
  113. object o = m_Objects[i];
  114. if (o == null)
  115. {
  116. Debug.LogWarning("Empty marker found while loading timeline. It will be removed.");
  117. m_Objects.RemoveAt(i);
  118. }
  119. }
  120. #endif
  121. m_CacheDirty = true;
  122. }
  123. void BuildCache()
  124. {
  125. if (m_CacheDirty)
  126. {
  127. m_Cache = new List<IMarker>(m_Objects.Count);
  128. m_HasNotifications = false;
  129. foreach (var o in m_Objects)
  130. {
  131. if (o != null)
  132. {
  133. m_Cache.Add(o as IMarker);
  134. if (o is INotification)
  135. {
  136. m_HasNotifications = true;
  137. }
  138. }
  139. }
  140. m_CacheDirty = false;
  141. }
  142. }
  143. }
  144. }