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?

I think you should look at custom editors. Hopefully this page will get you started:

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.