IPropertyKeyDataSource.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using UnityEditorInternal;
  5. using UnityEngine;
  6. namespace UnityEditor.Timeline
  7. {
  8. interface IPropertyKeyDataSource
  9. {
  10. float[] GetKeys(); // Get the keys
  11. Dictionary<float, string> GetDescriptions(); // Caches for descriptions
  12. }
  13. abstract class BasePropertyKeyDataSource : IPropertyKeyDataSource
  14. {
  15. static readonly StringBuilder k_StringBuilder = new StringBuilder();
  16. protected abstract AnimationClip animationClip { get; }
  17. public virtual float[] GetKeys()
  18. {
  19. if (animationClip == null)
  20. return null;
  21. var info = AnimationClipCurveCache.Instance.GetCurveInfo(animationClip);
  22. return info.keyTimes.Select(TransformKeyTime).ToArray();
  23. }
  24. public virtual Dictionary<float, string> GetDescriptions()
  25. {
  26. var map = new Dictionary<float, string>();
  27. var info = AnimationClipCurveCache.Instance.GetCurveInfo(animationClip);
  28. var processed = new HashSet<string>();
  29. foreach (var b in info.bindings)
  30. {
  31. var groupID = b.GetGroupID();
  32. if (processed.Contains(groupID))
  33. continue;
  34. var group = info.GetGroupBinding(groupID);
  35. var prefix = AnimationWindowUtility.GetNicePropertyGroupDisplayName(b.type, b.propertyName);
  36. foreach (var t in info.keyTimes)
  37. {
  38. k_StringBuilder.Length = 0;
  39. var key = TransformKeyTime(t);
  40. if (map.ContainsKey(key))
  41. k_StringBuilder.Append(map[key])
  42. .Append('\n');
  43. k_StringBuilder.Append(prefix)
  44. .Append(" : ")
  45. .Append(group.GetDescription(key));
  46. map[key] = k_StringBuilder.ToString();
  47. }
  48. processed.Add(groupID);
  49. }
  50. return map;
  51. }
  52. protected virtual float TransformKeyTime(float keyTime)
  53. {
  54. return keyTime;
  55. }
  56. }
  57. }