- Home /
Still having Serialization issues in 4.5.1f3
Hey guys,
According to the following release note, it is my understanding that we should be able to serialize Vector3s and other such structs as of Unity 4.5.
"Scripting: Structs with System.Serializable attribute can now be serialized. Also, fields of AnimationCurve[] and double[] now get serialised."
and the following debug line returns True...
Debug.Log("This type is serializable: " + System.Attribute.IsDefined(typeof(Vector3), typeof(System.SerializableAttribute)));
My problem is: When I run the following Save scipt ... https://github.com/antonholmquist/easy-serializer-unity
I still get the following error, because the class i'm trying to save contains a vector3.
"SerializationException: Type UnityEngine.Vector3 is not marked as Serializable."
To test it out, I marked the Vector3 as [System.NonSerialized] and it works just fine.
Thanks!
PS: Let me know if my description of the problem is unclear. Its very easy to recreate. I'm really stumped on this one.
Answer by Loius · Jul 08, 2014 at 03:49 PM
Structs with System.Serializable attribute can now be serialized.
Type UnityEngine.Vector3 is not marked as Serializable.
Therefore Vector3 is not in the category of things that can now be serialized.
YOU can now create and use Serializable structs in Unity serialization paths. Before that was impossible.
I think I understand, so I could create a struct from scratch and call it Vector3Serializable and serialize that and have it work just like a vector 3, but I cannot straight up serialize a pre-existing Vector3?
If that's basically it, i'll mark this as correct. Just want to make sure I understand. I'm only a self-taught programmer.
You are correct. I've actually got a serializable v3 if you want to just have one:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif
[System.Serializable]
public class SeriV3: ISerializable {
public static implicit operator Vector3(SeriV3 v) {return v.v3;}
public static explicit operator SeriV3(Vector3 v) {return new SeriV3( v );}
public SeriV3(Vector3 v) { v3 = v; }
public static Vector3 operator*(SeriV3 v,float f) {return v.v3*f;}
public Vector3 v3;
public float x { get { return v3.x; } set { v3 = v3.WithX( value ); } }
public float y { get { return v3.y; } set { v3 = v3.WithY( value ); } }
public float z { get { return v3.z; } set { v3 = v3.WithZ( value ); } }
public void GetObjectData(SerializationInfo info, Strea$$anonymous$$gContext context) {
// Use the AddValue method to specify serialized values.
info.AddValue("x", v3.x, typeof(float));
info.AddValue("y", v3.y, typeof(float));
info.AddValue("z", v3.z, typeof(float));
}
// The special constructor is used to deserialize values.
public SeriV3(SerializationInfo info, Strea$$anonymous$$gContext context)
{
// Reset the property value using the GetValue method.
v3 = new Vector3(
(float)info.GetValue( "x", typeof(float) ),
(float)info.GetValue( "y", typeof(float) ),
(float)info.GetValue( "z", typeof(float) )
);
}
public override string ToString ()
{
return string.Format ("[x={0}, y={1}, z={2}]", x, y, z);
}
}
// my real version of this class has a thousand things in it, these are just the necessary ones for seriv3
public static class Utex {
public static Vector3 WithX(this Vector3 v, float x) {
return new Vector3(x,v.y,v.z);
}
public static Vector3 WithY(this Vector3 v, float y) {
return new Vector3(v.x,y,v.z);
}
public static Vector3 WithZ(this Vector3 v, float z) {
return new Vector3(v.x,v.y,z);
}
}
Your answer
Follow this Question
Related Questions
Saving game data: BinaryFormatter vs SoapFormatter 0 Answers
Binaryformatter android files not reading properly 1 Answer
Can we merge multiple save files for easier management? 1 Answer
Saving gameobjects to .NET binary files 1 Answer
Binary Serialization (In Editor) - Path Access denied / File not found 0 Answers