Sin descripción
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.

TimelineUtility.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Globalization;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. #if UNITY_2021_2_OR_NEWER
  6. using UnityEditor.SceneManagement;
  7. #else
  8. using UnityEditor.Experimental.SceneManagement;
  9. #endif
  10. using UnityEngine;
  11. using UnityEngine.Timeline;
  12. using UnityEngine.Playables;
  13. using Object = UnityEngine.Object;
  14. namespace UnityEditor.Timeline
  15. {
  16. static class TimelineUtility
  17. {
  18. public static void ReorderTracks(List<ScriptableObject> allTracks, List<TrackAsset> tracks, ScriptableObject insertAfterAsset, bool up)
  19. {
  20. foreach (var i in tracks)
  21. allTracks.Remove(i);
  22. int index = allTracks.IndexOf(insertAfterAsset);
  23. index = up ? Math.Max(index, 0) : index + 1;
  24. allTracks.InsertRange(index, tracks.OfType<ScriptableObject>());
  25. }
  26. // Gets the track that holds the game object reference for this track.
  27. public static TrackAsset GetSceneReferenceTrack(TrackAsset asset)
  28. {
  29. if (asset == null)
  30. return null;
  31. if (asset.isSubTrack)
  32. return GetSceneReferenceTrack(asset.parent as TrackAsset);
  33. return asset;
  34. }
  35. public static bool TrackHasAnimationCurves(TrackAsset track)
  36. {
  37. if (track.hasCurves)
  38. return true;
  39. var animTrack = track as AnimationTrack;
  40. if (animTrack != null && animTrack.infiniteClip != null && !animTrack.infiniteClip.empty)
  41. return true;
  42. for (int i = 0; i < track.clips.Length; i++)
  43. {
  44. var curveClip = track.clips[i].curves;
  45. var animationClip = track.clips[i].animationClip;
  46. // prune out clip with zero curves
  47. if (curveClip != null && curveClip.empty)
  48. curveClip = null;
  49. if (animationClip != null && animationClip.empty)
  50. animationClip = null;
  51. // prune out clips coming from FBX
  52. if (animationClip != null && ((animationClip.hideFlags & HideFlags.NotEditable) != 0))
  53. animationClip = null;
  54. if (!track.clips[i].recordable)
  55. animationClip = null;
  56. if ((curveClip != null) || (animationClip != null))
  57. return true;
  58. }
  59. return false;
  60. }
  61. // get the game object reference associated with this
  62. public static GameObject GetSceneGameObject(PlayableDirector director, TrackAsset asset)
  63. {
  64. if (director == null || asset == null)
  65. return null;
  66. asset = GetSceneReferenceTrack(asset);
  67. var gameObject = director.GetGenericBinding(asset) as GameObject;
  68. var component = director.GetGenericBinding(asset) as Component;
  69. if (component != null)
  70. gameObject = component.gameObject;
  71. return gameObject;
  72. }
  73. public static PlayableDirector[] GetDirectorsInSceneUsingAsset(PlayableAsset asset)
  74. {
  75. const HideFlags hideFlags =
  76. HideFlags.HideInHierarchy | HideFlags.HideInInspector |
  77. HideFlags.DontSaveInEditor | HideFlags.NotEditable;
  78. var prefabMode = PrefabStageUtility.GetCurrentPrefabStage();
  79. var inScene = new List<PlayableDirector>();
  80. var allDirectors = Resources.FindObjectsOfTypeAll(typeof(PlayableDirector)) as PlayableDirector[];
  81. foreach (var director in allDirectors)
  82. {
  83. if ((director.hideFlags & hideFlags) != 0)
  84. continue;
  85. string assetPath = AssetDatabase.GetAssetPath(director.transform.root.gameObject);
  86. if (!String.IsNullOrEmpty(assetPath))
  87. continue;
  88. if (prefabMode != null && !prefabMode.IsPartOfPrefabContents(director.gameObject))
  89. continue;
  90. if (asset == null || (asset != null && director.playableAsset == asset))
  91. {
  92. inScene.Add(director);
  93. }
  94. }
  95. return inScene.ToArray();
  96. }
  97. public static PlayableDirector GetDirectorComponentForGameObject(GameObject gameObject)
  98. {
  99. return gameObject != null ? gameObject.GetComponent<PlayableDirector>() : null;
  100. }
  101. public static TimelineAsset GetTimelineAssetForDirectorComponent(PlayableDirector director)
  102. {
  103. return director != null ? director.playableAsset as TimelineAsset : null;
  104. }
  105. public static bool IsPrefabOrAsset(Object obj)
  106. {
  107. return EditorUtility.IsPersistent(obj) || (obj.hideFlags & HideFlags.NotEditable) != 0;
  108. }
  109. // TODO -- Need to add this to SerializedProperty so we can get replicate the accuracy that exists
  110. // in the undo system
  111. internal static string PropertyToString(SerializedProperty property)
  112. {
  113. switch (property.propertyType)
  114. {
  115. case SerializedPropertyType.Integer:
  116. return property.intValue.ToString(CultureInfo.InvariantCulture);
  117. case SerializedPropertyType.Float:
  118. return property.floatValue.ToString(CultureInfo.InvariantCulture);
  119. case SerializedPropertyType.String:
  120. return property.stringValue;
  121. case SerializedPropertyType.Boolean:
  122. return property.boolValue ? "1" : "0";
  123. case SerializedPropertyType.Color:
  124. return property.colorValue.ToString();
  125. case SerializedPropertyType.ArraySize:
  126. return property.intValue.ToString(CultureInfo.InvariantCulture);
  127. case SerializedPropertyType.Enum:
  128. return property.intValue.ToString(CultureInfo.InvariantCulture);
  129. case SerializedPropertyType.ObjectReference:
  130. return string.Empty;
  131. case SerializedPropertyType.LayerMask:
  132. return property.intValue.ToString(CultureInfo.InvariantCulture);
  133. case SerializedPropertyType.Character:
  134. return property.intValue.ToString(CultureInfo.InvariantCulture);
  135. case SerializedPropertyType.AnimationCurve:
  136. return property.animationCurveValue.ToString();
  137. case SerializedPropertyType.Gradient:
  138. return property.gradientValue.ToString();
  139. case SerializedPropertyType.Vector3:
  140. return property.vector3Value.ToString();
  141. case SerializedPropertyType.Vector4:
  142. return property.vector4Value.ToString();
  143. case SerializedPropertyType.Vector2:
  144. return property.vector2Value.ToString();
  145. case SerializedPropertyType.Rect:
  146. return property.rectValue.ToString();
  147. case SerializedPropertyType.Bounds:
  148. return property.boundsValue.ToString();
  149. case SerializedPropertyType.Quaternion:
  150. return property.quaternionValue.ToString();
  151. case SerializedPropertyType.Generic:
  152. return string.Empty;
  153. default:
  154. Debug.LogWarning("Unknown Property Type: " + property.propertyType);
  155. return string.Empty;
  156. }
  157. }
  158. // Is this a recordable clip on an animation track.
  159. internal static bool IsRecordableAnimationClip(TimelineClip clip)
  160. {
  161. if (!clip.recordable)
  162. return false;
  163. AnimationPlayableAsset asset = clip.asset as AnimationPlayableAsset;
  164. if (asset == null)
  165. return false;
  166. return true;
  167. }
  168. public static bool HasCustomEditor(TimelineClip clip)
  169. {
  170. var editor = CustomTimelineEditorCache.GetClipEditor(clip);
  171. return editor != CustomTimelineEditorCache.GetDefaultClipEditor();
  172. }
  173. public static IList<PlayableDirector> GetSubTimelines(TimelineClip clip, IExposedPropertyTable director)
  174. {
  175. var editor = CustomTimelineEditorCache.GetClipEditor(clip);
  176. List<PlayableDirector> directors = new List<PlayableDirector>();
  177. try
  178. {
  179. editor.GetSubTimelines(clip, director as PlayableDirector, directors);
  180. }
  181. catch (Exception e)
  182. {
  183. Debug.LogException(e);
  184. }
  185. return directors;
  186. }
  187. public static bool IsAllSubTrackMuted(TrackAsset asset)
  188. {
  189. if (asset is GroupTrack)
  190. return asset.mutedInHierarchy;
  191. foreach (TrackAsset t in asset.GetChildTracks())
  192. {
  193. if (!t.muted)
  194. return false;
  195. var childMuted = IsAllSubTrackMuted(t);
  196. if (!childMuted)
  197. return false;
  198. }
  199. return true;
  200. }
  201. public static bool IsParentMuted(TrackAsset asset)
  202. {
  203. TrackAsset p = asset.parent as TrackAsset;
  204. if (p == null) return false;
  205. return p is GroupTrack ? p.mutedInHierarchy : IsParentMuted(p);
  206. }
  207. public static IEnumerable<PlayableDirector> GetAllDirectorsInHierarchy(PlayableDirector mainDirector)
  208. {
  209. var directors = new HashSet<PlayableDirector> { mainDirector };
  210. GetAllDirectorsInHierarchy(mainDirector, directors);
  211. return directors;
  212. }
  213. static void GetAllDirectorsInHierarchy(PlayableDirector director, ISet<PlayableDirector> directors)
  214. {
  215. var timelineAsset = director.playableAsset as TimelineAsset;
  216. if (timelineAsset == null)
  217. return;
  218. foreach (var track in timelineAsset.GetOutputTracks())
  219. {
  220. foreach (var clip in track.clips)
  221. {
  222. foreach (var subDirector in GetSubTimelines(clip, director))
  223. {
  224. if (!directors.Contains(subDirector))
  225. {
  226. directors.Add(subDirector);
  227. GetAllDirectorsInHierarchy(subDirector, directors);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. public static bool IsLockedFromGroup(TrackAsset asset)
  234. {
  235. TrackAsset p = asset.parent as TrackAsset;
  236. if (p == null) return false;
  237. return p is GroupTrack ? p.lockedInHierarchy : IsLockedFromGroup(p);
  238. }
  239. internal static bool IsCurrentSequenceValid()
  240. {
  241. return TimelineWindow.instance != null
  242. && TimelineWindow.instance.state != null
  243. && TimelineWindow.instance.state.editSequence != null;
  244. }
  245. public static TimelineAsset CreateAndSaveTimelineAsset(string path)
  246. {
  247. var newAsset = ScriptableObject.CreateInstance<TimelineAsset>();
  248. newAsset.editorSettings.frameRate = TimelineProjectSettings.instance.defaultFrameRate;
  249. AssetDatabase.CreateAsset(newAsset, path);
  250. return newAsset;
  251. }
  252. }
  253. }