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);
}
}
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.
[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.StreamingContext)' Basically, asking for the GetObjectData fn() I'm using Unity3 and targeting iPhone (although switching project to Mac Standalone didn't change the behavior) STILL NEED HELP, PLEASE :D
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.
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 SurrogateSelector Class (System.Runtime.Serialization) | Microsoft Learn
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.
i use properties and implicit operators
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SerializableColor{
public float[] colorStore = new float[4]{1F,1F,1F,1F};
public Color Color{
get{ return new Color( colorStore[0], colorStore[1], colorStore[2], colorStore[3] );}
set{ colorStore = new float[4]{ value.r, value.g, value.b, value.a }; }
}
//makes this class usable as Color, Color normalColor = mySerializableColor;
public static implicit operator Color ( SerializableColor instance ){
return instance.Color;
}
//makes this class assignable by Color, SerializableColor myColor = Color.white;
public static implicit operator SerializableColor ( Color color ){
return new SerializableColor{ Color = color};
}
}
Using a copy/paste of the above code, I get: Assets/Plugins/SaveGame.cs(59,14): error CS0535:
– anon35426692ColorPreset' does not implement interface memberSystem.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)' Basically, asking for the GetObjectData fn() I'm using Unity3 and targeting iPhone (although switching project to Mac Standalone didn't change the behavior) STILL NEED HELP, PLEASE :D