Custom Editor on Serialized Objects which are not derived from Monobehaviour
Hi... I cannot manage to have the desired behaviour on my custom editor when using objects which are not extending anything... Below is an oversimplified version of what I would like to achieve.
What I have :
a serialized class MyClass holding an enum and a string
a script object MyClassHolder, extending MonoBehaviour, holding various instances of MyClass
a custom Editor MyClassEditor which should create a dynamic behaviour on "MyClass" properties
Behaviour wanted :
I inspect MyClassHolder using the inspector
I see the list of instances of MyClass
I select one instance
I select one of the value from the enum dropdown list
I would like the string value to update using some calculation.
However the string value does not update... Below is the actual code I try to fix... Can anyone help me ??
// file Assets/Scripts/MyClass.cs
using UnityEngine;
public class MyClassHolder : MonoBehaviour
{
public MyClass example1;
public MyClass example2;
}
[System.Serializable]
public class MyClass
{
public Dropdown dropdown;
public string textFromDropdown;
public enum Dropdown {ResponseOne, ResponseTwo }
}
.
// file Assets/Scripts/Editor/MyClassEditor.cs
using UnityEditor;
[CustomEditor(typeof(MyClass)), CanEditMultipleObjects]
public class MyClassEditor : Editor
{
public SerializedProperty dropdown_prop;
public SerializedProperty textFromDropdown_prop;
void OnEnable()
{
dropdown_prop = serializedObject.FindProperty("dropdown");
textFromDropdown_prop = serializedObject.FindProperty("textFromDropdown");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(dropdown_prop);
MyClass.Dropdown dropdownIndex = (MyClass.Dropdown)dropdown_prop.enumValueIndex;
switch (dropdownIndex)
{
case MyClass.Dropdown.ResponseOne:
textFromDropdown_prop.stringValue = "I am Response One";
break;
case MyClass.Dropdown.ResponseTwo:
textFromDropdown_prop.stringValue = "I am Response Two";
break;
}
serializedObject.ApplyModifiedProperties();
}
}
Answer by Adam-Mechtley · Mar 09, 2017 at 11:42 AM
For a custom serializable type, you want to use PropertyDrawer (and CustomPropertyDrawer), not Editor (and CustomEditor).
Your answer
Follow this Question
Related Questions
How to access a Custom Editor components 1 Answer
Unity3D ScriptableObject with UnityEvent not saving event parameter 0 Answers
Custom editor for a global list of variables, similar to Tags and Layers. 0 Answers
Custom Editor: Array of Structs with elements hidden by toggles 0 Answers
Detect Mouse Click Over BoxCollider2D in Scene View (Custom Editor) 1 Answer