// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #if UNITY_EDITOR using System; using UnityEditor; using Object = UnityEngine.Object; namespace Animancer.Editor { /// [Editor-Only] /// A utility for manually drawing a . /// /// https://kybernetik.com.au/animancer/api/Animancer.Editor/CachedEditor public class CachedEditor : IDisposable { /************************************************************************************************************************/ [NonSerialized] private Object[] _Targets = Array.Empty(); [NonSerialized] private UnityEditor.Editor _Editor; [NonSerialized] private bool _WillCleanup; /************************************************************************************************************************/ /// /// Creates a for the `target` /// and caches it to be returned by subsequent calls with the same `target`. /// public UnityEditor.Editor GetEditor(Object target) { if (_Targets.Length == 1 && _Targets[0] == target && _Editor != null) return _Editor; Dispose(); AnimancerUtilities.SetLength(ref _Targets, 1); _Targets[0] = target; _Editor = UnityEditor.Editor.CreateEditor(target); EnsureCleanup(); return _Editor; } /// /// Creates a for the `targets` /// and caches it to be returned by subsequent calls with the same `targets`. /// public UnityEditor.Editor GetEditor(Object[] targets) { if (AnimancerUtilities.ContentsAreEqual(targets, _Targets) && _Editor != null) return _Editor; Dispose(); _Targets = targets; _Editor = UnityEditor.Editor.CreateEditor(targets); EnsureCleanup(); return _Editor; } /************************************************************************************************************************/ /// Destroys the cached . public void Dispose() => Object.DestroyImmediate(_Editor); /************************************************************************************************************************/ /// Ensures that will be called before assemblies are reloaded. private void EnsureCleanup() { if (_WillCleanup) return; _WillCleanup = true; AssemblyReloadEvents.beforeAssemblyReload += Dispose; } /************************************************************************************************************************/ } } #endif