3

Hello people i'm trying to save a field of a custom editor value is set in a combo box, i get a code from internet but i realized that the changes maded in the combo box doesn't save at all, i tried use the "Save" button, i tried to make serializedfield, but i'm new in C# and unity. Thanks for visiting have a nice day.

    using UnityEditor;
    using UnityEngine;


    [CustomEditor(typeof(DoorScript))]
    [System.Serializable]
    public class ButtonDropDownMenu : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            DoorScript script = (DoorScript)target;

            GUIContent arrayLabel = new GUIContent("Color");
            script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
            /*serializedObject.FindProperty("index").intValue = script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
            script.index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);

            var properlyToSave = serializedObject.FindProperty("Index");
            EditorGUILayout.PropertyField(properlyToSave);
            serializedObject.ApplyModifiedProperties();¨*/

        }
    }



using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;

public class DoorScript : MonoBehaviour
{
    private SpriteRenderer myColor;
    public Light2D doorLight;
    public Light2D doorPLight;

    [HideInInspector][SerializeField]
    public int index = 0;
    [HideInInspector][SerializeField]
    public string[] options = new string[]
    {
        "White",
        "Red",
        "Blue",
        "Cyan"

    };

    private void Awake()
    {
        myColor = GetComponent<SpriteRenderer>();
        ColorChanger();
    }

    void Start()
    {

    }

    private void ColorChanger()
    {
        if (index <= 0)
        {
            DoorsLights(Color.grey);
        }
        else if (index == 1)
        {
            DoorsLights(Color.red);
        }
        else if (index == 2)
        {
            DoorsLights(Color.blue);
        }
        else if (index >= 3)
        {
            DoorsLights(Color.green);
        }
    }

    private void DoorsLights(Color color)
    {
        myColor.color = color;
        doorLight.color = color;
        doorPLight.color = color;
    }
}

Edit:

I worked Perfectly i made a change instead of

index = EditorGUILayout.Popup(arrayLabel, script.index, script.options); 

i used

 index.intValue = EditorGUILayout.Popup(arrayLabel, script.index, script.options); 
2
  • Does it still not save when you uncomment serializedObject.ApplyModifiedProperties();... that line should be necessary Commented Apr 15, 2020 at 21:22
  • i'll try again, give a minute Edit: isn't working yet Commented Apr 15, 2020 at 21:48

1 Answer 1

2

If you want to update properties from a custom editor you'll want to use SerializedProperty. Like this:

[CustomEditor(typeof(DoorScript))]
public class ButtonDropDownMenu : Editor
{
    SerializedProperty index;
    void OnEnable()
    {
        index = serializedObject.FindProperty("index");
    }
...

Then make sure you're calling serializedObject.ApplyModifiedProperties(); when you're done:

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();
    ...
    index = EditorGUILayout.Popup(arrayLabel, script.index, script.options);
    ...
    serializedObject.ApplyModifiedProperties();
}

Hope this helps! Let me know if this doesn't work

Sign up to request clarification or add additional context in comments.

2 Comments

I worked Perfectly i made a change instead of 'code' index = EditorGUILayout.Popup(arrayLabel, script.index, script.options); 'code'
Glad! BTW you can use a ` to get the 'code' to format properly, my button for the symbol is on the top left of my keyboard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.