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);
    }
}

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.

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
}

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.

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};
	}
}