TimelineCreateUtilities.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. namespace UnityEngine.Timeline
  7. {
  8. static class TimelineCreateUtilities
  9. {
  10. // based off of ObjectNames.GetUniqueName, but can exist in runtime
  11. public static string GenerateUniqueActorName(List<ScriptableObject> tracks, string name)
  12. {
  13. if (!tracks.Exists(x => ((object)x) != null && x.name == name))
  14. return name;
  15. int numberInParentheses = 0;
  16. string baseName = name;
  17. if (!string.IsNullOrEmpty(name) && name[name.Length - 1] == ')')
  18. {
  19. int index = name.LastIndexOf('(');
  20. if (index > 0)
  21. {
  22. string numberString = name.Substring(index + 1, name.Length - index - 2);
  23. if (int.TryParse(numberString, out numberInParentheses))
  24. {
  25. numberInParentheses++;
  26. baseName = name.Substring(0, index);
  27. }
  28. }
  29. }
  30. baseName = baseName.TrimEnd();
  31. for (int i = numberInParentheses; i < numberInParentheses + 5000; i++)
  32. {
  33. if (i > 0)
  34. {
  35. string result = string.Format("{0} ({1})", baseName, i);
  36. if (!tracks.Exists(x => ((object)x) != null && x.name == result))
  37. return result;
  38. }
  39. }
  40. // Fallback
  41. return name;
  42. }
  43. public static void SaveAssetIntoObject(Object childAsset, Object masterAsset)
  44. {
  45. if (childAsset == null || masterAsset == null)
  46. return;
  47. if ((masterAsset.hideFlags & HideFlags.DontSave) != 0)
  48. {
  49. childAsset.hideFlags |= HideFlags.DontSave;
  50. }
  51. else
  52. {
  53. childAsset.hideFlags |= HideFlags.HideInHierarchy;
  54. #if UNITY_EDITOR
  55. if (!AssetDatabase.Contains(childAsset) && AssetDatabase.Contains(masterAsset))
  56. AssetDatabase.AddObjectToAsset(childAsset, masterAsset);
  57. #endif
  58. }
  59. }
  60. public static AnimationClip CreateAnimationClipForTrack(string name, TrackAsset track, bool isLegacy)
  61. {
  62. var timelineAsset = track != null ? track.timelineAsset : null;
  63. var trackFlags = track != null ? track.hideFlags : HideFlags.None;
  64. var curves = new AnimationClip
  65. {
  66. legacy = isLegacy,
  67. name = name,
  68. frameRate = timelineAsset == null
  69. ? TimelineAsset.EditorSettings.kDefaultFps
  70. : timelineAsset.editorSettings.fps
  71. };
  72. SaveAssetIntoObject(curves, timelineAsset);
  73. curves.hideFlags = trackFlags & ~HideFlags.HideInHierarchy; // Never hide in hierarchy
  74. TimelineUndo.RegisterCreatedObjectUndo(curves, "Create Curves");
  75. return curves;
  76. }
  77. public static bool ValidateParentTrack(TrackAsset parent, Type childType)
  78. {
  79. if (childType == null || !typeof(TrackAsset).IsAssignableFrom(childType))
  80. return false;
  81. // no parent is valid for any type
  82. if (parent == null)
  83. return true;
  84. // A track supports layers if it implements ILayerable. Only supported for parents that are
  85. // the same exact type as the child class, and 1 level of nesting only
  86. if (parent is ILayerable && !parent.isSubTrack && parent.GetType() == childType)
  87. return true;
  88. var attr = Attribute.GetCustomAttribute(parent.GetType(), typeof(SupportsChildTracksAttribute)) as SupportsChildTracksAttribute;
  89. if (attr == null)
  90. return false;
  91. // group track case, accepts all
  92. if (attr.childType == null)
  93. return true;
  94. // specific case. Specifies nesting level
  95. if (childType == attr.childType)
  96. {
  97. int nestCount = 0;
  98. var p = parent;
  99. while (p != null && p.isSubTrack)
  100. {
  101. nestCount++;
  102. p = p.parent as TrackAsset;
  103. }
  104. return nestCount < attr.levels;
  105. }
  106. return false;
  107. }
  108. }
  109. }