AnimancerGraphCleanup.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace Animancer.Editor
  8. {
  9. /// <summary>[Editor-Only]
  10. /// Keeps track of <see cref="AnimancerGraph"/> instances
  11. /// to ensure that they're properly cleaned up.
  12. /// </summary>
  13. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/AnimancerGraphCleanup
  14. public static class AnimancerGraphCleanup
  15. {
  16. /************************************************************************************************************************/
  17. private static List<AnimancerGraph> _AllGraphs;
  18. /// <summary>[Editor-Only] Registers a `graph` to make sure it gets cleaned up properly.</summary>
  19. public static void AddGraph(AnimancerGraph graph)
  20. {
  21. if (_AllGraphs == null)
  22. {
  23. _AllGraphs = new();
  24. AssemblyReloadEvents.beforeAssemblyReload +=
  25. () => DestroyAll(EditorApplication.isPlaying);
  26. EditorApplication.playModeStateChanged += change =>
  27. {
  28. switch (change)
  29. {
  30. case PlayModeStateChange.EnteredEditMode:
  31. DestroyAll(true);
  32. break;
  33. case PlayModeStateChange.ExitingEditMode:
  34. DestroyAll(false);
  35. break;
  36. }
  37. };
  38. }
  39. else
  40. {
  41. EditModeDestroyOldInstances();
  42. }
  43. _AllGraphs.Add(graph);
  44. }
  45. /// <summary>[Editor-Only] Removes the `graph` from the list of instances.</summary>
  46. public static void RemoveGraph(AnimancerGraph graph)
  47. {
  48. _AllGraphs?.Remove(graph);
  49. AnimancerGraph.ClearInactiveInitializationStackTrace(graph);
  50. }
  51. /************************************************************************************************************************/
  52. private static void DestroyAll(bool isPlaying)
  53. {
  54. for (int i = _AllGraphs.Count - 1; i >= 0; i--)
  55. {
  56. var graph = _AllGraphs[i];
  57. if (graph.IsValidOrDispose())
  58. {
  59. if (isPlaying && graph.InactiveInitializationStackTrace != null)
  60. {
  61. Debug.LogWarning(
  62. $"{graph} was not properly destroyed." +
  63. $" Its {nameof(GameObject)} was inactive and never activated," +
  64. $" meaning that Unity didn't call its AnimancerComponent.OnDestroy." +
  65. $"\n\nIf you need to use Animancer on an object that never gets activated," +
  66. $" you must call animancerComponent.Graph.Destroy() on it manually." +
  67. $"\n\nThis graph was created:\n{graph.InactiveInitializationStackTrace}\n",
  68. graph.Component as Object);
  69. AnimancerGraph.ClearInactiveInitializationStackTrace(graph);
  70. }
  71. graph.Destroy();
  72. }
  73. }
  74. _AllGraphs.Clear();
  75. }
  76. /************************************************************************************************************************/
  77. private static void EditModeDestroyOldInstances()
  78. {
  79. if (EditorApplication.isPlayingOrWillChangePlaymode)
  80. return;
  81. for (int i = _AllGraphs.Count - 1; i >= 0; i--)
  82. {
  83. var graph = _AllGraphs[i];
  84. if (!ShouldStayAlive(graph))
  85. {
  86. if (graph.IsValidOrDispose())
  87. graph.Destroy();// This will remove it.
  88. else
  89. _AllGraphs.RemoveAt(i);
  90. }
  91. }
  92. }
  93. /************************************************************************************************************************/
  94. /// <summary>Should this graph should stay alive instead of being destroyed?</summary>
  95. private static bool ShouldStayAlive(AnimancerGraph graph)
  96. {
  97. if (!graph.IsValidOrDispose())
  98. return false;
  99. if (graph.Component == null)
  100. return true;
  101. if (graph.Component is Object obj && obj == null)
  102. return false;
  103. if (graph.Component.Animator == null)
  104. return false;
  105. return true;
  106. }
  107. /************************************************************************************************************************/
  108. /// <summary>[Editor-Only]
  109. /// Returns true if the `initial` mode was <see cref="AnimatorUpdateMode.AnimatePhysics"/>
  110. /// and the `current` has changed to another mode or if the `initial` mode was something else
  111. /// and the `current` has changed to <see cref="AnimatorUpdateMode.AnimatePhysics"/>.
  112. /// </summary>
  113. public static bool HasChangedToOrFromAnimatePhysics(AnimatorUpdateMode? initial, AnimatorUpdateMode current)
  114. {
  115. if (initial == null)
  116. return false;
  117. #if UNITY_2023_1_OR_NEWER
  118. var wasAnimatePhysics = initial.Value == AnimatorUpdateMode.Fixed;
  119. var isAnimatePhysics = current == AnimatorUpdateMode.Fixed;
  120. #else
  121. var wasAnimatePhysics = initial.Value == AnimatorUpdateMode.AnimatePhysics;
  122. var isAnimatePhysics = current == AnimatorUpdateMode.AnimatePhysics;
  123. #endif
  124. return wasAnimatePhysics != isAnimatePhysics;
  125. }
  126. /************************************************************************************************************************/
  127. }
  128. }
  129. namespace Animancer
  130. {
  131. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerGraph
  132. public partial class AnimancerGraph
  133. {
  134. /************************************************************************************************************************/
  135. /// <summary>[Editor-Only] [Internal]
  136. /// A stack trace captured in <see cref="CreateOutput(Animator, IAnimancerComponent)"/>
  137. /// if the <see cref="GameObject.activeInHierarchy"/> is false.
  138. /// </summary>
  139. /// <remarks>
  140. /// This is used to warn if the graph isn't destroyed
  141. /// because Unity won't call <c>OnDestroy</c> if the object is never activated.
  142. /// </remarks>
  143. internal System.Diagnostics.StackTrace InactiveInitializationStackTrace { get; private set; }
  144. /************************************************************************************************************************/
  145. /// <summary>[Editor-Only] Captures the <see cref="InactiveInitializationStackTrace"/>.</summary>
  146. private void CaptureInactiveInitializationStackTrace(IAnimancerComponent animancer)
  147. {
  148. if (!animancer.gameObject.activeInHierarchy &&
  149. EditorApplication.isPlayingOrWillChangePlaymode)
  150. InactiveInitializationStackTrace = new(1, true);
  151. }
  152. /************************************************************************************************************************/
  153. /// <summary>[Editor-Only] [Internal] Discards the <see cref="InactiveInitializationStackTrace"/>.</summary>
  154. public static void ClearInactiveInitializationStackTrace(AnimancerGraph graph)
  155. {
  156. graph.InactiveInitializationStackTrace = null;
  157. }
  158. /************************************************************************************************************************/
  159. }
  160. }
  161. #endif