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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With