CachedEditor.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using UnityEditor;
  5. using Object = UnityEngine.Object;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only]
  9. /// A utility for manually drawing a <see cref="UnityEditor.Editor"/>.
  10. /// </summary>
  11. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/CachedEditor
  12. public class CachedEditor : IDisposable
  13. {
  14. /************************************************************************************************************************/
  15. [NonSerialized] private Object[] _Targets = Array.Empty<Object>();
  16. [NonSerialized] private UnityEditor.Editor _Editor;
  17. [NonSerialized] private bool _WillCleanup;
  18. /************************************************************************************************************************/
  19. /// <summary>
  20. /// Creates a <see cref="UnityEditor.Editor"/> for the `target`
  21. /// and caches it to be returned by subsequent calls with the same `target`.
  22. /// </summary>
  23. public UnityEditor.Editor GetEditor(Object target)
  24. {
  25. if (_Targets.Length == 1 &&
  26. _Targets[0] == target &&
  27. _Editor != null)
  28. return _Editor;
  29. Dispose();
  30. AnimancerUtilities.SetLength(ref _Targets, 1);
  31. _Targets[0] = target;
  32. _Editor = UnityEditor.Editor.CreateEditor(target);
  33. EnsureCleanup();
  34. return _Editor;
  35. }
  36. /// <summary>
  37. /// Creates a <see cref="UnityEditor.Editor"/> for the `targets`
  38. /// and caches it to be returned by subsequent calls with the same `targets`.
  39. /// </summary>
  40. public UnityEditor.Editor GetEditor(Object[] targets)
  41. {
  42. if (AnimancerUtilities.ContentsAreEqual(targets, _Targets) &&
  43. _Editor != null)
  44. return _Editor;
  45. Dispose();
  46. _Targets = targets;
  47. _Editor = UnityEditor.Editor.CreateEditor(targets);
  48. EnsureCleanup();
  49. return _Editor;
  50. }
  51. /************************************************************************************************************************/
  52. /// <summary>Destroys the cached <see cref="UnityEditor.Editor"/>.</summary>
  53. public void Dispose()
  54. => Object.DestroyImmediate(_Editor);
  55. /************************************************************************************************************************/
  56. /// <summary>Ensures that <see cref="Dispose"/> will be called before assemblies are reloaded.</summary>
  57. private void EnsureCleanup()
  58. {
  59. if (_WillCleanup)
  60. return;
  61. _WillCleanup = true;
  62. AssemblyReloadEvents.beforeAssemblyReload += Dispose;
  63. }
  64. /************************************************************************************************************************/
  65. }
  66. }
  67. #endif