AnimatedParameterCache.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace UnityEditor.Timeline
  5. {
  6. static class AnimatedParameterCache
  7. {
  8. static readonly Dictionary<Type, FieldInfo[]> k_ScriptPlayableFieldsCache = new Dictionary<Type, FieldInfo[]>();
  9. static readonly Dictionary<PropertyKey, FieldInfo> k_PropertyFieldInfoCache = new Dictionary<PropertyKey, FieldInfo>();
  10. static readonly Dictionary<PropertyKey, bool> k_PropertyIsAnimatableCache = new Dictionary<PropertyKey, bool>();
  11. static readonly Dictionary<PropertyKey, string> k_BindingNameCache = new Dictionary<PropertyKey, string>();
  12. public static bool TryGetScriptPlayableFields(Type type, out FieldInfo[] scriptPlayableFields)
  13. {
  14. return k_ScriptPlayableFieldsCache.TryGetValue(type, out scriptPlayableFields);
  15. }
  16. public static void SetScriptPlayableFields(Type type, FieldInfo[] scriptPlayableFields)
  17. {
  18. k_ScriptPlayableFieldsCache[type] = scriptPlayableFields;
  19. }
  20. public static bool TryGetFieldInfoForProperty(SerializedProperty property, out FieldInfo fieldInfo)
  21. {
  22. return k_PropertyFieldInfoCache.TryGetValue(new PropertyKey(property), out fieldInfo);
  23. }
  24. public static void SetFieldInfoForProperty(SerializedProperty property, FieldInfo fieldInfo)
  25. {
  26. k_PropertyFieldInfoCache[new PropertyKey(property)] = fieldInfo;
  27. }
  28. public static bool TryGetIsPropertyAnimatable(SerializedProperty property, out bool isAnimatable)
  29. {
  30. return k_PropertyIsAnimatableCache.TryGetValue(new PropertyKey(property), out isAnimatable);
  31. }
  32. public static void SetIsPropertyAnimatable(SerializedProperty property, bool isAnimatable)
  33. {
  34. k_PropertyIsAnimatableCache[new PropertyKey(property)] = isAnimatable;
  35. }
  36. public static bool TryGetBindingName(Type type, string path, out string bindingName)
  37. {
  38. return k_BindingNameCache.TryGetValue(new PropertyKey(type, path), out bindingName);
  39. }
  40. public static void SetBindingName(Type type, string path, string bindingName)
  41. {
  42. k_BindingNameCache[new PropertyKey(type, path)] = bindingName;
  43. }
  44. }
  45. struct PropertyKey : IEquatable<PropertyKey>
  46. {
  47. readonly Type m_Type;
  48. readonly string m_Path;
  49. public PropertyKey(SerializedProperty property)
  50. {
  51. m_Type = property.serializedObject.targetObject.GetType();
  52. m_Path = property.propertyPath;
  53. }
  54. public PropertyKey(Type type, string path)
  55. {
  56. m_Type = type;
  57. m_Path = path;
  58. }
  59. public bool Equals(PropertyKey other)
  60. {
  61. return m_Type == other.m_Type && string.Equals(m_Path, other.m_Path);
  62. }
  63. public override bool Equals(object obj)
  64. {
  65. if (ReferenceEquals(null, obj)) return false;
  66. return obj is PropertyKey && Equals((PropertyKey)obj);
  67. }
  68. public override int GetHashCode()
  69. {
  70. unchecked
  71. {
  72. return ((m_Type != null ? m_Type.GetHashCode() : 0) * 397) ^ (m_Path != null ? m_Path.GetHashCode() : 0);
  73. }
  74. }
  75. }
  76. }