AnimationClipUpgrader_Types.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using IMaterial = UnityEditor.Rendering.UpgradeUtility.IMaterial;
  6. using MaterialProxy = UnityEditor.Rendering.UpgradeUtility.MaterialProxy;
  7. namespace UnityEditor.Rendering
  8. {
  9. /// <summary>
  10. /// Internal type definitions for <see cref="AnimationClipUpgrader"/>.
  11. /// </summary>
  12. /// <remarks>
  13. /// This class contains two categories of internal types:
  14. /// 1. Proxies for UnityObject assets (used for test mocking and facilitating usage without requiring loading all assets of the given type at once).
  15. /// 2. Asset path wrappers (used for stronger typing and clarity in the API surface).
  16. /// </remarks>
  17. static partial class AnimationClipUpgrader
  18. {
  19. #region Proxies
  20. internal interface IAnimationClip
  21. {
  22. AnimationClip Clip { get; }
  23. EditorCurveBinding[] GetCurveBindings();
  24. void ReplaceBindings(EditorCurveBinding[] oldBindings, EditorCurveBinding[] newBindings);
  25. }
  26. internal struct AnimationClipProxy : IAnimationClip
  27. {
  28. public AnimationClip Clip { get; set; }
  29. public EditorCurveBinding[] GetCurveBindings() => AnimationUtility.GetCurveBindings(Clip);
  30. public void ReplaceBindings(EditorCurveBinding[] oldBindings, EditorCurveBinding[] newBindings)
  31. {
  32. var curves = new AnimationCurve[oldBindings.Length];
  33. for (int i = 0, count = oldBindings.Length; i < count; ++i)
  34. curves[i] = AnimationUtility.GetEditorCurve(Clip, oldBindings[i]);
  35. AnimationUtility.SetEditorCurves(Clip, oldBindings, new AnimationCurve[oldBindings.Length]);
  36. AnimationUtility.SetEditorCurves(Clip, newBindings, curves);
  37. }
  38. public static implicit operator AnimationClip(AnimationClipProxy proxy) => proxy.Clip;
  39. public static implicit operator AnimationClipProxy(AnimationClip clip) => new AnimationClipProxy { Clip = clip };
  40. public override string ToString() => Clip.ToString();
  41. }
  42. internal interface IRenderer
  43. {
  44. }
  45. internal struct RendererProxy : IRenderer
  46. {
  47. Renderer m_Renderer;
  48. public void GetSharedMaterials(List<IMaterial> materials)
  49. {
  50. materials.Clear();
  51. var m = ListPool<Material>.Get();
  52. m_Renderer.GetSharedMaterials(m);
  53. materials.AddRange(m.Select(mm => (MaterialProxy)mm).Cast<IMaterial>());
  54. ListPool<Material>.Release(m);
  55. }
  56. public static implicit operator Renderer(RendererProxy proxy) => proxy.m_Renderer;
  57. public static implicit operator RendererProxy(Renderer renderer) => new RendererProxy { m_Renderer = renderer };
  58. public override string ToString() => m_Renderer.ToString();
  59. }
  60. #endregion
  61. #region AssetPath Wrappers
  62. internal interface IAssetPath
  63. {
  64. string Path { get; }
  65. }
  66. internal struct ClipPath : IAssetPath
  67. {
  68. public string Path { get; set; }
  69. public static implicit operator string(ClipPath clip) => clip.Path;
  70. public static implicit operator ClipPath(string path) => new ClipPath { Path = path };
  71. public static implicit operator ClipPath(AnimationClip clip) => new ClipPath { Path = AssetDatabase.GetAssetPath(clip) };
  72. public override string ToString() => Path;
  73. }
  74. internal struct PrefabPath : IAssetPath
  75. {
  76. public string Path { get; set; }
  77. public static implicit operator string(PrefabPath prefab) => prefab.Path;
  78. public static implicit operator PrefabPath(string path) => new PrefabPath { Path = path };
  79. public static implicit operator PrefabPath(GameObject go) => new PrefabPath { Path = AssetDatabase.GetAssetPath(go) };
  80. public override string ToString() => Path;
  81. }
  82. internal struct ScenePath : IAssetPath
  83. {
  84. public string Path { get; set; }
  85. public static implicit operator string(ScenePath scene) => scene.Path;
  86. public static implicit operator ScenePath(string path) => new ScenePath { Path = path };
  87. public static implicit operator ScenePath(SceneAsset scene) => new ScenePath { Path = AssetDatabase.GetAssetPath(scene) };
  88. public override string ToString() => Path;
  89. }
  90. #endregion
  91. }
  92. }