Hi,
We created a custom inspector (EditorWindow) that enables editing of some of our custom fixed point value types. Editing and viewing the values work well enough; however, it seems that the values are not serialized in the same way that the regular types are.
We can view both floats and fixed point in our custom editor window, but upon running the program the fixed point reverts to whatever it was in the beginning while the floating point values retain the new edited values.
It seems Unity only serializes public types it knows about (makes sense), but is there some way to hook into the system to save our custom types so we can edit them in the same way in the editor? It would seem so natural to have the facility since it’s so easy to add custom inspectors.
Yes its possible. The requirement is that the class is serializable if it does not extend one of the classes that work out of the box
We’ve tried adding [System.Serializable], but it does not seem to work for structs.
Is there another way to specify/control serialization for structs? It’s for a fixed point data type, so changing it into a class would introduce a lot of possible bugs when people try to treat it as a regular value type.
[System.Serializable]
public struct fixed_point : ISerializable
{
public long Raw;
public fixed_point(SerializationInfo serInfo,
StreamingContext streamContext)
{
Raw = serInfo.GetInt32("Raw");
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Raw", Raw);
}
}
With the above code neither the ISerializable interface nor the serialization constructor are called by Unity. The Raw public variable doesn’t appear in the inspector either, even if we change fixed_point into a class.
Is there another way to tell the Inspector to save the public long Raw above after we edit it in our custom inspector?
You could make it part of the custom inspector
Hmmm, offhand to make that work I think we would need a separate data store from what the Inspector uses, or declare companion strings for each fixed_point member variable just so that Unity would have somewhere to store the edited values. Then every time the game starts we convert from the string into the long value.
public struct fixed_point
{
public long Raw;
public string Raw_storage;
public void InitFromAwake()
{
Raw = long.Parse(Raw_storage);
}
...
Then call InitFromAwake for every single instance of fixed_point that should be editable from the Inspector each time a GameObject is started.
Workable I guess, just… rather awkward =/
You can use a Property for your Raw (long) value, and then perform the string conversion (with FormatException handling) the first time it is requested.
Should make it a bit nicer to use, but it is still a pain…
-Jeremy
Ah, right, that’ll get rid of a lot of the mess of this approach.
Thanks Jeremy and dreamora!
FWIW, custom structs won’t display in the editor by default so you’re not missing anything. I just wanted to point that out so you knew for sure.
Hi HiggyB,
Yup, we built an inspector to display the custom struct… that contained a variable type that wasn’t saved by the inspector anyway. :lol:
Oh well…