DisplayName of a serialized property does not change (Scriptable Object - Editor Script)

47 views Asked by At

I am writing an Editor Script for a scriptable object. This is my requirement.

  1. I have a few options defined by an Enum. (E_Joint -> HIP, KNEE, SHOULDER)
  2. In the scriptable object, I change the enum to Knee.
  3. There is a string variable "panelName". The string value needs to change (which works) and the display name needs to change as well (Which does not seem to work).

Here is the code.

public class ApplicationSetup : ScriptableObject
{
    public E_BuildOption ebuildOption;
    public E_Joint eJoint;

    [SerializeReference]
    public string panelName;
}

Here is the Editor Script:

[CustomEditor(typeof(ApplicationSetup))]
    public class ApplicationSetupEditor : Editor
    {
        SerializedProperty mJoint;
        SerializedProperty mPanel;

        ApplicationSetup appSetup;
        private void OnEnable()
        {
            mJoint = serializedObject.FindProperty("eJoint");
            mPanel = serializedObject.FindProperty("panelName");
            appSetup = (ApplicationSetup)target;
        }
        public override void OnInspectorGUI()
        {
            //DrawDefaultInspector()
            serializedObject.Update();

            DrawPropertiesExcluding(serializedObject,
            new string[] {"m_Script", "eJoint", "panelName"});

            GUIContent content = new GUIContent("Panel Name");

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(mJoint);
            if(EditorGUI.EndChangeCheck())
            {
                E_Joint joint = (E_Joint)mJoint.enumValueIndex;
                
                // Check joint type
                switch(joint)
                {
                    // If knee joint
                    case E_Joint.KNEE:
                        // Set panel name to KneeOption
                        mPanel.stringValue = "KneeOption";
                        // Set content text to Knee Option
                        content.text = "Knee Option";
                        break;

                    // If hip joint  
                    case E_Joint.HIP:
                        // Set panel name to HipOption
                        mPanel.stringValue = "HipOption";
                        // Set content text to Hip Option
                        content.text = "Hip Option";
                        break;

                    // If shoulder joint
                    case E_Joint.SHOULDER:
                        // Set panel name to ShoulderOption
                        mPanel.stringValue = "ShoulderOption";
                        // Set content text to Shoulder Option
                        content.text = "Shoulder Option";
                        break;

                    // Default case
                    default:
                        // Set panel name to DefaultOption
                        mPanel.stringValue = "DefaultOption";
                        // Set content text to Default Option
                        content.text = "Default Option";
                        break;
                }
            }
            EditorGUILayout.PropertyField(mPanel, content);
            
            serializedObject.ApplyModifiedProperties();
        }
    }

Attaching a screenshot of what is happening

enter image description here

Am I missing something here? Any help is much appreciated.

Thanks!


Update
Found the fix.
It was silly.

Since this is running every frame, it was setting the name to PanelName.
I just saved the GUIContent onEnable.

Updated Editor script.

public class ApplicationSetupEditor : Editor
    {
        SerializedProperty mJoint;
        SerializedProperty mPanel;

        ApplicationSetup appSetup;
        
        GUIContent panelContent;
        
        private void OnEnable()
        {
            mJoint = serializedObject.FindProperty("eJoint");
            mPanel = serializedObject.FindProperty("panelName");
            appSetup = (ApplicationSetup)target;
            panelContent = new GUIContent(appSetup.panelName);
        }
        public override void OnInspectorGUI()
        {
            //DrawDefaultInspector()
            serializedObject.Update();

            DrawPropertiesExcluding(serializedObject,
            new string[] {"m_Script", "eJoint", "panelName"});

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(mJoint);
            if(EditorGUI.EndChangeCheck())
            {
                E_Joint joint = (E_Joint)mJoint.enumValueIndex;
                // Check joint type
                switch(joint)
                {
                    // If knee joint
                    case E_Joint.KNEE:
                        // Set panel name to KneeOption
                        mPanel.stringValue = "KneeOption";
                        // Set content text to Knee Option
                        panelContent.text = "Knee Option";
                        break;

                    // If hip joint  
                    case E_Joint.HIP:
                        // Set panel name to HipOption
                        mPanel.stringValue = "HipOption";
                        // Set content text to Hip Option
                        panelContent.text = "Hip Option";
                        break;

                    // If shoulder joint
                    case E_Joint.SHOULDER:
                        // Set panel name to ShoulderOption
                        mPanel.stringValue = "ShoulderOption";
                        // Set content text to Shoulder Option
                        panelContent.text = "Shoulder Option";
                        break;

                    // Default case
                    default:
                        // Set panel name to DefaultOption
                        mPanel.stringValue = "DefaultOption";
                        // Set content text to Default Option
                        panelContent.text = "Default Option";
                        break;
                }
                
            }
            
            EditorGUILayout.PropertyField(mPanel, panelContent);
            serializedObject.ApplyModifiedProperties();
        }
    }

This worked.

0

There are 0 answers