- Home /
Trouble setting the object reference in a property drawer
I've got a wrapper class that holds a serialzed named key and a data object. I'd like to select from static instances of the wrapper in a dropdown based on the key. My assignment of those references doesn't seem to be working correctly. What's the proper way to assign a reference to a new object in a property drawer?
Here's some example code:
[Serializable]
class Wrapper
{
public string name;
private Data data;
}
class Data
{
public string foo;
public int bar;
}
class Drawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
int index = 3; //from dropdown
Undo.RegisterCompleteObjectUndo(property.serializedObject.targetObject, "Setting wrapper");
fieldInfo.SetValue(property.serializedObject.targetObject, StaticClass.listOfWrappers[index]);
property.serializedObject.ApplyModifiedProperties();
}
}
class StaticClass
{
public static List<Wrapper> listOfWrappers = new List<Wrapper>() { new Wrapper() { name = "foo" }, new Wrapper() { name = "bar" } };
}
This seems to almost work. If I have two instances of a component with a wrapper field and I set one to foo and one to bar, save, and change scenes then come back they are both set to bar.
I tried making the wrapper a UnityEngine.Object and then setting property.objectReferenceValue. But that didn't seem to stick at all. I'd prefer for it not to be a unity engine object as I don't think I get any benefit from that.
Any ideas what I'm doing wrong?
Your answer
Follow this Question
Related Questions
SerializedProperty with children class fields 2 Answers
Custom Editor script to show non-native class in inspector 1 Answer
PropertyDrawer let's dissapear my INT 0 Answers
Help with Missing Monobehaviours and Asset Serialization? 0 Answers
Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers