AnimatorBindingCache.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if UNITY_EDITOR
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. using UnityEngine.Timeline;
  6. using UnityEditor;
  7. namespace UnityEngine.Timeline
  8. {
  9. /// <summary>
  10. /// Animator to Editor Curve Binding cache. Used to prevent frequent calls to GetAnimatorBindings which can be costly
  11. /// </summary>
  12. class AnimatorBindingCache
  13. {
  14. public const string TRPlaceHolder = "TransformTR";
  15. public const string ScalePlaceholder = "TransformScale";
  16. struct AnimatorEntry
  17. {
  18. public int animatorID;
  19. public bool applyRootMotion;
  20. public bool humanoid;
  21. }
  22. class AnimatorEntryComparer : IEqualityComparer<AnimatorEntry>
  23. {
  24. public bool Equals(AnimatorEntry x, AnimatorEntry y) { return x.animatorID == y.animatorID && x.applyRootMotion == y.applyRootMotion && x.humanoid == y.humanoid; }
  25. public int GetHashCode(AnimatorEntry obj) { return HashUtility.CombineHash(obj.animatorID, obj.applyRootMotion.GetHashCode(), obj.humanoid.GetHashCode()); }
  26. public static readonly AnimatorEntryComparer Instance = new AnimatorEntryComparer();
  27. }
  28. readonly Dictionary<AnimatorEntry, EditorCurveBinding[]> m_AnimatorCache = new Dictionary<AnimatorEntry, EditorCurveBinding[]>(AnimatorEntryComparer.Instance);
  29. readonly Dictionary<AnimationClip, EditorCurveBinding[]> m_ClipCache = new Dictionary<AnimationClip, EditorCurveBinding[]>();
  30. private static readonly EditorCurveBinding[] kEmptyArray = new EditorCurveBinding[0];
  31. private static readonly List<EditorCurveBinding> s_BindingScratchPad = new List<EditorCurveBinding>(1000);
  32. public AnimatorBindingCache()
  33. {
  34. AnimationUtility.onCurveWasModified += OnCurveWasModified;
  35. }
  36. public EditorCurveBinding[] GetAnimatorBindings(GameObject gameObject)
  37. {
  38. if (gameObject == null)
  39. return kEmptyArray;
  40. Animator animator = gameObject.GetComponent<Animator>();
  41. if (animator == null)
  42. return kEmptyArray;
  43. AnimatorEntry entry = new AnimatorEntry()
  44. {
  45. animatorID = animator.GetInstanceID(),
  46. applyRootMotion = animator.applyRootMotion,
  47. humanoid = animator.isHuman
  48. };
  49. EditorCurveBinding[] result = null;
  50. if (m_AnimatorCache.TryGetValue(entry, out result))
  51. return result;
  52. s_BindingScratchPad.Clear();
  53. // Replacement for AnimationMode.GetAnimatorBinding - this is faster and allocates kB instead of MB
  54. var transforms = animator.GetComponentsInChildren<Transform>();
  55. foreach (var t in transforms)
  56. {
  57. if (animator.IsBoneTransform(t))
  58. s_BindingScratchPad.Add(EditorCurveBinding.FloatCurve(AnimationUtility.CalculateTransformPath(t, animator.transform), typeof(Transform), TRPlaceHolder));
  59. }
  60. var streamBindings = AnimationUtility.GetAnimationStreamBindings(animator.gameObject);
  61. UpdateTransformBindings(streamBindings);
  62. s_BindingScratchPad.AddRange(streamBindings);
  63. result = new EditorCurveBinding[s_BindingScratchPad.Count];
  64. s_BindingScratchPad.CopyTo(result);
  65. m_AnimatorCache[entry] = result;
  66. return result;
  67. }
  68. public EditorCurveBinding[] GetCurveBindings(AnimationClip clip)
  69. {
  70. if (clip == null)
  71. return kEmptyArray;
  72. EditorCurveBinding[] result;
  73. if (!m_ClipCache.TryGetValue(clip, out result))
  74. {
  75. result = AnimationMode.GetCurveBindings(clip);
  76. UpdateTransformBindings(result);
  77. m_ClipCache[clip] = result;
  78. }
  79. return result;
  80. }
  81. private static void UpdateTransformBindings(EditorCurveBinding[] bindings)
  82. {
  83. for (int i = 0; i < bindings.Length; i++)
  84. {
  85. var binding = bindings[i];
  86. if (AnimationPreviewUtilities.IsRootMotion(binding))
  87. {
  88. binding.type = typeof(Transform);
  89. binding.propertyName = TRPlaceHolder;
  90. }
  91. else if (typeof(Transform).IsAssignableFrom(binding.type) && (binding.propertyName.StartsWith("m_LocalRotation.") || binding.propertyName.StartsWith("m_LocalPosition.")))
  92. {
  93. binding.propertyName = TRPlaceHolder;
  94. }
  95. else if (typeof(Transform).IsAssignableFrom(binding.type) && binding.propertyName.StartsWith("m_LocalScale."))
  96. {
  97. binding.propertyName = ScalePlaceholder;
  98. }
  99. bindings[i] = binding;
  100. }
  101. }
  102. public void Clear()
  103. {
  104. m_AnimatorCache.Clear();
  105. m_ClipCache.Clear();
  106. }
  107. void OnCurveWasModified(AnimationClip clip, EditorCurveBinding binding, AnimationUtility.CurveModifiedType modification)
  108. {
  109. m_ClipCache.Remove(clip);
  110. }
  111. }
  112. }
  113. #endif