- Home /
Is it possible to serialize and store custom structs on the iPhone in scriptableObjects?
This is the general idea. I'd like to store a bunch of these in a scriptable object without making them classes. Unfortunately it fails to save the data when stored as a struct.
I've seen forum threads mentioning using custom inspectors, but those are not available in the iPhone IDE.
[System.Serializable] public struct Point{ public int x; public int y;
}
As a thought, if this IS possible in 2.6, how would one go about saving the data, and could that data be imported into iPhone Unity once serialized as an asset?
Answer by Duke · Jan 22, 2010 at 05:50 PM
Does it work correctly if you provide an ISerializable implementation?
using System.Runtime.Serialization;
[Serializable] public struct Point : ISerializable { public int x; public int y;
public Point(SerializationInfo info, StreamingContext context)
{
x = info.GetInt32("x");
y = info.GetInt32("y");
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("x", x);
info.AddValue("y", x);
}
}
It doesn't seem to work for the iPhone. Thanks though.
@$$anonymous$$$$anonymous$$ehrer In what way does this not work on iPhone for you? Is this working on other platforms?
Everything on iPhone seems to have a problem with structs, it's something to do with the way that $$anonymous$$ono has implemented some of the CLR. There's a problem with SetValueDirect which doesn't help much. Better off to use classes I'm afraid - unless you are really stuck talking to some native interface.
Unity states in their Serialization documentation to avoid structs, I also tried anyway :) but no go. See: http://forum.unity3d.com/threads/110247-Serialization-issues classes should work if impl'ing ISerialize
Answer by andeeee · Jan 22, 2010 at 11:02 AM
Although structs don't give an error when they are marked as serialisable, they don't have the same behaviour as serialisable classes in Unity (properties visible in the inspector, etc). There might be some other approach, though. Are you using structs for better array or GC performance or is there another reason?
Yeah, better GC performance primarily. I would agree, but somehow Unity made Vector3 serializable, I wonder if the same method is possible? I've obviously switched to objects in the mean time, and the world hasn't ended yet, but in more performance critical sections of my code, this is at best annoying.
Unity serialisation capabilities seem extremely under developed.