PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/PackageCache/com.unity.timeline@1.2.18/Editor/TimelineUtility.cs

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