Hi there!
I have not very much experience regarding editor scripts so there is probably something very obvious I’m missing. Hopefully someone can give me a hand with this.
I’m trying to write a very simple editor script for a MonoBehaviour which includes an animator. The idea is to show in the inspector tab a Popup window with all the triggers in the Animator so anyone can be selected and then saved in a string variable in the MonoBehaviour script. The final goal for this is to create a prefab with any given animator and this MonoBehaviour script that can be instantiated multiple times in any scene, then select specific triggers in the Popup window to make the animator go through different transitions.
My classes are like this:
public class AnimationScript : MonoBehaviour
{
[ContextMenu(" test play animation")]
private void Update()
{
Debug.Log("---> " + m_selectedTrigger);
}
public Animator p_Animator => m_Animator;
[SerializeField] private Animator m_Animator;
public string m_selectedTrigger = "none";
public int selectedIndex = 0;
}
[CustomEditor(typeof (AnimationScript))]
public class AnimationScript_Editor : Editor
{
public override void OnInspectorGUI()
{
this.DrawDefaultInspector();
AnimationScript targetObject = (AnimationScript) this.target;
var indexProperty = serializedObject.FindProperty("selectedIndex") ;
var triggerNameProperty = serializedObject.FindProperty("m_selectedTrigger");
Animator animator = targetObject.p_Animator;
if (animator == null)
return;
List<string> triggerOnlyNames = animator.parameters.ToList().FindAll(x => x.type == AnimatorControllerParameterType.Trigger)
.Select(x => x.name).ToList();
triggerNameProperty.stringValue = triggerOnlyNames[indexProperty.intValue];
indexProperty.intValue = EditorGUILayout.Popup(indexProperty.intValue, triggerOnlyNames.ToArray());
serializedObject.ApplyModifiedProperties();
}
}
Is working as expected, the list of triggers is loaded as a list of strings in the Popup and I