// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;
namespace Animancer.Editor
{
/// [Editor-Only] A custom Inspector for s.
/// https://kybernetik.com.au/animancer/api/Animancer.Editor/BaseAnimancerComponentEditor
public abstract class BaseAnimancerComponentEditor : UnityEditor.Editor
{
/************************************************************************************************************************/
[NonSerialized]
private IAnimancerComponent[] _Targets;
/// casted to .
public IAnimancerComponent[] Targets
=> _Targets;
/************************************************************************************************************************/
/// Initializes this .
protected virtual void OnEnable()
{
var targets = this.targets;
_Targets = new IAnimancerComponent[targets.Length];
GatherTargets();
}
/************************************************************************************************************************/
///
/// Copies the into the array.
///
private void GatherTargets()
{
for (int i = 0; i < _Targets.Length; i++)
_Targets[i] = (IAnimancerComponent)targets[i];
}
/************************************************************************************************************************/
/// Called by the Unity editor to draw the custom Inspector GUI elements.
public override void OnInspectorGUI()
{
// Normally the targets wouldn't change after OnEnable, but the trick AnimancerComponent.Reset uses to
// swap the type of an existing component when a new one is added causes the old target to be destroyed.
GatherTargets();
serializedObject.Update();
DoSerializedFieldsGUI();
serializedObject.ApplyModifiedProperties();
}
/************************************************************************************************************************/
/// Draws the rest of the Inspector fields after the Animator field.
protected void DoSerializedFieldsGUI()
{
var property = serializedObject.GetIterator();
if (!property.NextVisible(true))
return;
do
{
var path = property.propertyPath;
if (path == "m_Script")
continue;
using (var label = PooledGUIContent.Acquire(property))
{
// Let the target try to override.
if (DoOverridePropertyGUI(path, property, label))
continue;
// Otherwise draw the property normally.
EditorGUILayout.PropertyField(property, label, true);
}
}
while (property.NextVisible(false));
}
/************************************************************************************************************************/
/// [Editor-Only]
/// Draws any custom GUI for the `property`.
/// The return value indicates whether the GUI should replace the regular call to
/// .
/// True = GUI was drawn, so don't draw the regular GUI.
/// False = Draw the regular GUI.
///
protected virtual bool DoOverridePropertyGUI(string path, SerializedProperty property, GUIContent label)
=> false;
/************************************************************************************************************************/
}
}
#endif