• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Olgo · Jul 07, 2014 at 07:01 PM · serializationbinaryformatterunity 4.5

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Olgo · Jul 08, 2014 at 02:04 PM 0
Share

PS: Let me know if my description of the problem is unclear. Its very easy to recreate. I'm really stumped on this one.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Olgo · Jul 08, 2014 at 06:15 PM 0
Share

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.

avatar image Loius · Jul 09, 2014 at 06:11 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialize Data - After Update 1 Answer

Saving HashSet using BinaryFormat 1 Answer

How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers

What is the best/easiest way to save a game (or scene) at runtime? 1 Answer

IOException: Sharing violation on path 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges