Search Unity

Question Editor scripst, ApplyModifiedParametesr and save scene. Something doesn't work

Discussion in 'Scripting' started by Elandir, Mar 2, 2021.

  1. Elandir

    Elandir

    Joined:
    Feb 9, 2014
    Posts:
    32
    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:

    Code (CSharp):
    1. public class AnimationScript : MonoBehaviour
    2. {
    3.  
    4.     [ContextMenu(" test play animation")]
    5.     private void Update()
    6.     {
    7.         Debug.Log("---> " + m_selectedTrigger);
    8.     }
    9.  
    10.     public Animator p_Animator => m_Animator;
    11.     [SerializeField] private Animator m_Animator;
    12.      public string m_selectedTrigger = "none";
    13.     public int selectedIndex = 0;
    14. }
    Code (CSharp):
    1. [CustomEditor(typeof (AnimationScript))]
    2. public class AnimationScript_Editor : Editor
    3. {
    4.  
    5.     public override void OnInspectorGUI()
    6.     {
    7.         this.DrawDefaultInspector();
    8.         AnimationScript targetObject = (AnimationScript) this.target;
    9.      
    10.         var indexProperty =  serializedObject.FindProperty("selectedIndex") ;
    11.         var triggerNameProperty = serializedObject.FindProperty("m_selectedTrigger");
    12.      
    13.         Animator animator = targetObject.p_Animator;
    14.         if (animator == null)
    15.             return;
    16.  
    17.      
    18.         List<string> triggerOnlyNames = animator.parameters.ToList().FindAll(x => x.type == AnimatorControllerParameterType.Trigger)
    19.                                                             .Select(x => x.name).ToList();
    20.  
    21.         triggerNameProperty.stringValue = triggerOnlyNames[indexProperty.intValue];
    22.         indexProperty.intValue = EditorGUILayout.Popup(indexProperty.intValue, triggerOnlyNames.ToArray());
    23.  
    24.  
    25.         serializedObject.ApplyModifiedProperties();
    26.    
    27.     }
    28. }
    Is working as expected, the list of triggers is loaded as a list of strings in the Popup and I
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm not sure what all you're trying, but if the changes are not "sticking," you need to make sure you call Undo.RecordObject() immediately before where you change properties you want saved, passing in the field you contemplate changing. This lets Unity know it is dirty and must be saved.