Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 justin_iSO · Jul 22, 2013 at 04:31 PM · c#arrayvector3serializationxml

C#-XMLSerialize a Struct with Vector3 Array in it

I am rather new to serialization and have it working for some sections but I am running in to an issue.

The first struct:

 public struct RecordPt
 {
     float xP;
     float yP;
 
     public RecordPt(float x, float y){
         xP = x;
         yP = y;
     }
 }

Works just fine in my serialize code:

 public void WriteToXML (string path1,string path2)
 {
     //Serialize and write first list of RecordPt to xml
     XmlSerializer serializer = new XmlSerializer (typeof(List<RecordPt>));
     FileStream stream = new FileStream (path, FileMode.Create);
     serializer.Serialize (stream, recordingPoints);
     stream.Close ();
         
     //Serialize and write second list of LineSeg to xml
     XmlSerializer lineSer = new XmlSerializer (typeof(List<LineSeg>));
     FileStream lineStream = new FileStream (path2, FileMode.Create);
     serializer.Serialize (lineStream, recordingLines);
     lineStream.Close ();

}

The second struct has a Vector3 Array that seems to be causing the issue.

     public struct LineSeg
     {
         
         public Vector3[] pts;
         public int lnColor;
         public float timeP;
         
         public LineSeg (Vector3[] ptAr, int color, float time)
         {
             pts = ptAr;
             lnColor = color;
             timeP = time;
         }
         
     }

The error I am getting is:

InvalidOperationException: The type of the argument object 'System.Collections.Generic.List`1[[RecordMovement.SerLines, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not primitive.

Thanks in advance!

~Justin

Comment
Add comment
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

2 Replies

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

Answer by tsmitro · Jul 22, 2013 at 04:50 PM

Vector3 is not a primitive and not marked as Serializable. You can wrap it yourself and still provide access to the underlying object (note that I'm using doubles because of the JSON serializer that I use, but that's negligible):

 [Serializable]
 public class SerializableVector3
 {
     public double X;
     public double Y;
     public double Z;
     
     public Vector3 Vector3
     {
         get
         {
             return new Vector3((float)X, (float)Y, (float)Z);    
         }
     }
     
     public SerializableVector3() { }
     public SerializableVector3(Vector3 vector) 
     {
         double val;
         X = double.TryParse(vector.x.ToString(), out val) ? val : 0.0;
         Y = double.TryParse(vector.y.ToString(), out val) ? val : 0.0;
         Z = double.TryParse(vector.z.ToString(), out val) ? val : 0.0;
     }        
 }

I would advise against using an array, but rather a List of Vector3: see why here. Also, you should wrap your streams in using statements to clean up disposable objects.

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 justin_iSO · Jul 22, 2013 at 05:28 PM 0
Share

Thanks for your help!

I took your advice and made a custom struct for the Vector3's and that seemed to help. Additionally I made an "oh duh" mistake and didn't make the second serialization use the correct XmlSerializer. Copy/paste didn't rename... dough!

Thanks!

~Justin

avatar image tsmitro · Jul 22, 2013 at 05:36 PM 0
Share

Nice, I didn't catch the serializer error either, but I'm glad you got it workin.

avatar image
0

Answer by nugget70 · Jul 28, 2015 at 05:14 AM

Im trying to serilize a list of vector 3's using your class SerializableVector3

and saving like this:

if(_colliderPos.Count!=0){ //loop through every position in the list for (int i=0; i<_colliderPos.Count;i++){ //create a container for the class SerilizableVector3 and pass collider positiotion to its constructor SerializableVector3 data=new SerializableVector3(_colliderPos[i]); //Write to the file using the data we just sent to the class SerilizableVector3 bf.Serialize(file,data);//write that container to the file } }

but its only saving the first part of the list? what am i doing wrong

p.s. im using binary not XML

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Trouble updating old Unity syntax 0 Answers

Best practice to store and load a specific set of objects? 3 Answers

How do I keep references to unique NPCs between scenes? 1 Answer

Serializing an Action as XML? 0 Answers

Multiple Cars not working 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