RemapSpriteAnimationTool.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. using UnityEngine;
  8. namespace Animancer.Editor.Tools
  9. {
  10. /// <summary>[Editor-Only] [Pro-Only]
  11. /// An <see cref="AnimationModifierTool"/> for changing which
  12. /// <see cref="Sprite"/>s an <see cref="AnimationClip"/> uses.
  13. /// </summary>
  14. /// <remarks>
  15. /// <strong>Documentation:</strong>
  16. /// <see href="https://kybernetik.com.au/animancer/docs/manual/tools/remap-sprite-animation">
  17. /// Remap Sprite Animation</see>
  18. /// </remarks>
  19. /// https://kybernetik.com.au/animancer/api/Animancer.Editor.Tools/RemapSpriteAnimationTool
  20. ///
  21. [Serializable]
  22. public class RemapSpriteAnimationTool : AnimationModifierTool
  23. {
  24. /************************************************************************************************************************/
  25. [SerializeField] private List<Sprite> _NewSprites;
  26. [NonSerialized] private List<Sprite> _OldSprites;
  27. [NonSerialized] private bool _OldSpritesAreDirty;
  28. [NonSerialized] private ReorderableList _OldSpriteDisplay;
  29. [NonSerialized] private ReorderableList _NewSpriteDisplay;
  30. [NonSerialized] private EditorCurveBinding _SpriteBinding;
  31. [NonSerialized] private ObjectReferenceKeyframe[] _SpriteKeyframes;
  32. /************************************************************************************************************************/
  33. /// <inheritdoc/>
  34. public override int DisplayOrder => 4;
  35. /// <inheritdoc/>
  36. public override string Name => "Remap Sprite Animation";
  37. /// <inheritdoc/>
  38. public override string HelpURL => Strings.DocsURLs.RemapSpriteAnimation;
  39. /// <inheritdoc/>
  40. public override string Instructions
  41. {
  42. get
  43. {
  44. if (Animation == null)
  45. return "Select the animation you want to remap.";
  46. if (_OldSprites.Count == 0)
  47. return "The selected animation does not use Sprites.";
  48. return "Assign the New Sprites that you want to replace the Old Sprites with then click Save As." +
  49. " You can Drag and Drop multiple Sprites onto the New Sprites list at the same time.";
  50. }
  51. }
  52. /************************************************************************************************************************/
  53. /// <inheritdoc/>
  54. public override void OnEnable(int index)
  55. {
  56. base.OnEnable(index);
  57. _NewSprites ??= new();
  58. if (Animation == null)
  59. _NewSprites.Clear();
  60. _OldSprites = new();
  61. _OldSpriteDisplay = AnimancerToolsWindow.CreateReorderableObjectList(_OldSprites, "Old Sprites");
  62. _NewSpriteDisplay = AnimancerToolsWindow.CreateReorderableObjectList(_NewSprites, "New Sprites");
  63. }
  64. /************************************************************************************************************************/
  65. /// <inheritdoc/>
  66. protected override void OnAnimationChanged()
  67. {
  68. base.OnAnimationChanged();
  69. _OldSpritesAreDirty = true;
  70. }
  71. /************************************************************************************************************************/
  72. /// <inheritdoc/>
  73. public override void DoBodyGUI()
  74. {
  75. base.DoBodyGUI();
  76. GatherOldSprites();
  77. GUILayout.BeginHorizontal();
  78. {
  79. GUILayout.BeginVertical();
  80. GUI.enabled = false;
  81. _OldSpriteDisplay.DoLayoutList();
  82. GUI.enabled = true;
  83. GUILayout.EndVertical();
  84. GUILayout.BeginVertical();
  85. _NewSpriteDisplay.DoLayoutList();
  86. GUILayout.EndVertical();
  87. HandleDragAndDropIntoList(GUILayoutUtility.GetLastRect(), _NewSprites, overwrite: true);
  88. }
  89. GUILayout.EndHorizontal();
  90. GUI.enabled = Animation != null;
  91. GUILayout.BeginHorizontal();
  92. {
  93. GUILayout.FlexibleSpace();
  94. if (GUILayout.Button("Reset"))
  95. {
  96. AnimancerGUI.Deselect();
  97. AnimancerToolsWindow.RecordUndo();
  98. _NewSprites.Clear();
  99. _OldSpritesAreDirty = true;
  100. }
  101. if (GUILayout.Button("Save As"))
  102. {
  103. if (SaveAs())
  104. {
  105. _OldSpritesAreDirty = true;
  106. }
  107. }
  108. }
  109. GUILayout.EndHorizontal();
  110. }
  111. /************************************************************************************************************************/
  112. /// <summary>Gathers the <see cref="_OldSprites"/> from the <see cref="AnimationModifierTool.Animation"/>.</summary>
  113. private void GatherOldSprites()
  114. {
  115. if (!_OldSpritesAreDirty)
  116. return;
  117. _OldSpritesAreDirty = false;
  118. _OldSprites.Clear();
  119. _NewSprites.Clear();
  120. if (Animation == null)
  121. return;
  122. var bindings = AnimationUtility.GetObjectReferenceCurveBindings(Animation);
  123. for (int iBinding = 0; iBinding < bindings.Length; iBinding++)
  124. {
  125. var binding = bindings[iBinding];
  126. if (binding.type == typeof(SpriteRenderer) && binding.propertyName == "m_Sprite")
  127. {
  128. _SpriteBinding = binding;
  129. _SpriteKeyframes = AnimationUtility.GetObjectReferenceCurve(Animation, binding);
  130. for (int iKeyframe = 0; iKeyframe < _SpriteKeyframes.Length; iKeyframe++)
  131. {
  132. var reference = _SpriteKeyframes[iKeyframe].value as Sprite;
  133. if (reference != null)
  134. _OldSprites.Add(reference);
  135. }
  136. _NewSprites.AddRange(_OldSprites);
  137. return;
  138. }
  139. }
  140. }
  141. /************************************************************************************************************************/
  142. /// <inheritdoc/>
  143. protected override void Modify(AnimationClip animation)
  144. {
  145. for (int i = 0; i < _SpriteKeyframes.Length; i++)
  146. {
  147. _SpriteKeyframes[i].value = _NewSprites[i];
  148. }
  149. AnimationUtility.SetObjectReferenceCurve(animation, _SpriteBinding, _SpriteKeyframes);
  150. }
  151. /************************************************************************************************************************/
  152. }
  153. }
  154. #endif