Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TimelineCreateUtilities.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 void RemoveAssetFromObject(Object childAsset, Object masterAsset)
  61. {
  62. if (childAsset == null || masterAsset == null)
  63. return;
  64. #if UNITY_EDITOR
  65. if (AssetDatabase.Contains(childAsset) && AssetDatabase.Contains(masterAsset))
  66. AssetDatabase.RemoveObjectFromAsset(childAsset);
  67. #endif
  68. }
  69. public static AnimationClip CreateAnimationClipForTrack(string name, TrackAsset track, bool isLegacy)
  70. {
  71. var timelineAsset = track != null ? track.timelineAsset : null;
  72. var trackFlags = track != null ? track.hideFlags : HideFlags.None;
  73. var curves = new AnimationClip
  74. {
  75. legacy = isLegacy,
  76. name = name,
  77. frameRate = timelineAsset == null
  78. ? (float)TimelineAsset.EditorSettings.kDefaultFrameRate
  79. : (float)timelineAsset.editorSettings.frameRate
  80. };
  81. SaveAssetIntoObject(curves, timelineAsset);
  82. curves.hideFlags = trackFlags & ~HideFlags.HideInHierarchy; // Never hide in hierarchy
  83. TimelineUndo.RegisterCreatedObjectUndo(curves, "Create Curves");
  84. return curves;
  85. }
  86. public static bool ValidateParentTrack(TrackAsset parent, Type childType)
  87. {
  88. if (childType == null || !typeof(TrackAsset).IsAssignableFrom(childType))
  89. return false;
  90. // no parent is valid for any type
  91. if (parent == null)
  92. return true;
  93. // A track supports layers if it implements ILayerable. Only supported for parents that are
  94. // the same exact type as the child class, and 1 level of nesting only
  95. if (parent is ILayerable && !parent.isSubTrack && parent.GetType() == childType)
  96. return true;
  97. var attr = Attribute.GetCustomAttribute(parent.GetType(), typeof(SupportsChildTracksAttribute)) as SupportsChildTracksAttribute;
  98. if (attr == null)
  99. return false;
  100. // group track case, accepts all
  101. if (attr.childType == null)
  102. return true;
  103. // specific case. Specifies nesting level
  104. if (childType == attr.childType)
  105. {
  106. int nestCount = 0;
  107. var p = parent;
  108. while (p != null && p.isSubTrack)
  109. {
  110. nestCount++;
  111. p = p.parent as TrackAsset;
  112. }
  113. return nestCount < attr.levels;
  114. }
  115. return false;
  116. }
  117. }
  118. }