// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #if UNITY_EDITOR using UnityEditor; using UnityEngine; namespace Animancer.Editor { /// [Editor-Only] [Internal] /// A utility for drawing empty [] fields. /// /// /// Used to draw empty slots in /// which don't actually have a of their own because /// the array is compacted to trim any null items from the end. /// /// https://kybernetik.com.au/animancer/api/Animancer.Editor/DummyInvokableDrawer internal class DummyInvokableDrawer : ScriptableObject { /************************************************************************************************************************/ [SerializeReference, Polymorphic] private IInvokable[] _Invokable; /************************************************************************************************************************/ private static SerializedProperty _InvokableProperty; /// [Editor-Only] A static dummy . private static SerializedProperty InvokableProperty { get { if (_InvokableProperty == null) { var instance = CreateInstance(); instance.hideFlags = HideFlags.HideInHierarchy | HideFlags.DontSave; var serializedObject = new SerializedObject(instance); _InvokableProperty = serializedObject.FindProperty(nameof(_Invokable)); AssemblyReloadEvents.beforeAssemblyReload += () => { serializedObject.Dispose(); DestroyImmediate(instance); }; } return _InvokableProperty; } } /************************************************************************************************************************/ /// [Editor-Only] The GUI height required by . public static float Height => AnimancerGUI.LineHeight; /************************************************************************************************************************/ private static int _LastControlID; private static int _PropertyIndex; /// [Editor-Only] Draws the GUI. public static bool DoGUI( ref Rect area, GUIContent label, SerializedProperty property, out object invokable) { var controlID = GUIUtility.GetControlID(FocusType.Passive); if (_LastControlID >= controlID) _PropertyIndex = 0; _LastControlID = controlID; var invokablesProperty = InvokableProperty; if (invokablesProperty.arraySize <= _PropertyIndex) invokablesProperty.arraySize = _PropertyIndex + 1; var invokableProperty = invokablesProperty.GetArrayElementAtIndex(_PropertyIndex); invokableProperty.prefabOverride = property.prefabOverride; _PropertyIndex++; label = EditorGUI.BeginProperty(area, label, property); EditorGUI.PropertyField(area, invokableProperty, label, false); EditorGUI.EndProperty(); invokable = invokableProperty.managedReferenceValue; if (invokable == null) return false; invokableProperty.managedReferenceValue = null; return true; } /************************************************************************************************************************/ } } #endif