- Home /
Modifying SerializedProperty in Inspector
I have an Editor script for a class I've been working on and I'm trying to update it to take advantage of SerializedProperty (which I'm not sure I fully understand).
The issue I'm having is that I have the inspector displaying the same data in different ways depending on how the user would prefer to manipulate the data. However, I can't find a way to have a SerializedProperty be displayed differently in the Inspector without also changing how it stores that data internally.
For example, imagine my data is var length: float, stored as "meters". However, the user has opted to work with centimeters instead.
Previously, I would have done something as follows:
if (meters)
length = EditorGUILayout.IntField("Meters:", length);
else
length = EditorGUILayout.IntField("Centimeters:", length * 100) / 100;
The best I've done to emulate that with SerializedProperty is something like this:
if (meters)
{
EditorGUILayout.PropertyField(lengthProp, new GUIContent("Meters:"));
}
else
{
var lengthVal : float = lengthProp.floatValue;
lengthVal = EditorGUILayout.IntField("Centimeters:", lengthVal * 100);
if (!lengthProp.hasMultipleDifferentValues && lengthVal != lengthProp.floatValue)
lengthProp.floatValue = lengthVal / 100;
}
But that doesn't seem like an ideal solution and misses some of the advantages of SerializedProperty. Any suggestions on a better approach?
Answer by Julien-Lynge · Jan 10, 2013 at 07:22 PM
I think you should look at custom editors. Hopefully this page will get you started:
http://answers.unity3d.com/questions/222741/how-to-make-a-custom-editor-in-c.html
In a nutshell, these wrap around your component and have a GUI event associated with them. So you can create whatever interface you'd like, and do whatever math you'd like, at the custom editor level, and then it communicates information back down to the actual component. The user never sees the actual component, just the custom editor.
That's actually what I'm doing. All of the code above is in an Editor class. $$anonymous$$y current implementation has an inspector interface that hides the actual data and uses math/conversions to display more easily understood and editable values, and then converts the input back into data the original class can use.
What I'm really looking for is how to best implement SerializedProperty in an Editor class in a way that separates "how the data is stored" from "how the data is displayed/manipulated in the Inspector/Editor".
Your answer
![](https://koobas.hobune.stream/wayback/20220613090729im_/https://answers.unity.com/themes/thub/images/avi.jpg)