- Home /
Unity3: can I serialize non-POD types? (Color, Vector3, etc.)?
The following code gives me the error: Type UnityEngine.Color is not marked as Serializable. I get the same using Vector3... if I change to float, it works? What's the deal??? Thx!
[System.Serializable ()] public class ColorPreset : ISerializable { public Color clrStageBG = new Color(0.0f, 0.0f, 0.0f, 1.0f);
public ColorPreset ()
{
}
public ColorPreset (SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
clrStageBG = (Color)info.GetValue("clrStageBG", typeof(Color));
}
//Serialization function.
public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("clrStageBG",clrStageBG);
}
}
Answer by Sumner · Jan 13, 2011 at 02:12 PM
Using Binary Formatter, I added values of POD types as so:
[System.Serializable] public class ColorPreset : ISerializable { public Color clrStageBG = new Color(0.0f, 0.0f, 0.0f, 1.0f);
public ColorPreset ()
{
}
public ColorPreset (SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
clrStageBG.r = (float)info.GetValue("clrStageBG_R", typeof(float));
clrStageBG.g = (float)info.GetValue("clrStageBG_G", typeof(float));
clrStageBG.b = (float)info.GetValue("clrStageBG_B", typeof(float));
clrStageBG.a = (float)info.GetValue("clrStageBG_A", typeof(float));
}
public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("clrStageBG_R",clrStageBG.r);
info.AddValue("clrStageBG_G",clrStageBG.g);
info.AddValue("clrStageBG_B",clrStageBG.b);
info.AddValue("clrStageBG_A",clrStageBG.a);
}
};
As a note, my string values are a bit verbose, and I do see it adds the strings in full (not hashed) to the exported file... still a bit smaller than XML, but not as small as it could be... I'll just try to keep my AddValue strings short, i guess.
Answer by MidnightStudiosInc · Jul 01, 2015 at 02:51 PM
GameObject Serializer Pro can serialize Vectors, Colors, and even complex subclasses of UnityEngine.Object like Components, Meshes, and Textures. It's much more efficient than XMLSerializer and unlike BinaryFormatter is forward-compatible.
Answer by Voxel-Busters · Jul 01, 2015 at 03:52 PM
You won't be able to serialise Vectors, Colors and Quaternions directly as they are not Serializable classes. But c# allows you to implement serialization extensions classes using ISerializationSurrogate. Check this link for more info https://msdn.microsoft.com/en-us/library/system.runtime.serialization.surrogateselector%28v=vs.110%29.aspx
But if you want to avoid all the trouble and save time, then check out Runtime Serialization for Unity fast and efficient plugin designed to handle serialization of c# class object as well Unity Objects like GameObject, Transform, Textures etc.
Answer by Paulius-Liekis · Jan 10, 2011 at 07:20 AM
Try this:
[System.Serializable] public class ColorPreset : ISerializable { public Color clrStageBG = new Color(0.0f, 0.0f, 0.0f, 1.0f);
public ColorPreset ()
{
}
// no need for serialization functions, at least I don't use them and everything works for me
}
Using a copy/paste of the above code, I get:
Assets/Plugins/SaveGame.cs(59,14): error CS0535: ColorPreset' does not implement interface member
System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.Strea$$anonymous$$gContext)'
Basically, asking for the GetObjectData fn()
I'm using Unity3 and targeting iPhone (although switching project to $$anonymous$$ac Standalone didn't change the behavior)
STILL NEED HELP, PLEASE :D
Answer by Sumner · Jan 13, 2011 at 01:40 PM
Still doing testing, but from extensive searching, I found this link:
http://forum.unity3d.com/threads/7766-Vector3-not-serializable
Which makes me think that my original code will work with the XMLSerializer, but not with BinaryFormatter, which must use POD types.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094744im_/https://answers.unity.com/themes/thub/images/avi.jpg)