I’m working on a custom editor extension that will support a palatalized color system for Unity3D UI elements (both NGUI and new Unity UI). As part of this process I’m learning how to create custom Inspectors and PropertyDrawers.
I added a struct to one of my objects that replaced a simple Int, and I needed to be able to access the struct from an inspector. Normally, to access the Int you’d create a SerializedProperty that would point to the public serialized int and then access the value with the .intValue property.
Once I updated the class to use a custom struct, the inspector no longer worked (obviously) and I needed to pull values from this struct instead of a primitive type.
The struct is defined as:
[Serializable]
public struct PBMapperIndex
{
public int intValue;
public string indexName;
}
It contains a string and an int, and most importantly, it’s marked as serializable. This means Unity will keep the struct as a serializable object.
To access it in the custom inspector, I used the following:
SerializedProperty tintProp = serializedObject.FindProperty(“tintIndex“);
Where tinitIndex is declared as a public PBMapperIndex in my class.
Then to drill down and get the intValue for this property I used:
SerializedProperty ixProp = tintProp.FindPropertyRelative(“intValue“);
int index = ixProp.intValue;
Two keys, mark the original struct as [Serializable] and then used FindPropertyRelative to pull out the ‘child’ property.