• 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 /
This question was closed Feb 17, 2017 at 04:59 PM by Ziron999 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ziron999 · Feb 17, 2017 at 02:52 AM · c#vector3convertingbytearray

Converting Vector3[] to byte[]

I have a similar question to:
http://answers.unity3d.com/questions/683693/converting-vector3-to-byte.html
it has a nice answer at the bottom:
To byte:

  byte[] buff = new byte[sizeof(float)*3];         
  Buffer.BlockCopy( BitConverter.GetBytes( vect.x ), 0, buff, 0*sizeof(float), sizeof(float) );
  Buffer.BlockCopy( BitConverter.GetBytes( vect.y ), 0, buff, 1*sizeof(float), sizeof(float) );
  Buffer.BlockCopy( BitConverter.GetBytes( vect.z ), 0, buff, 2*sizeof(float), sizeof(float) );

To vector3:

  byte[] buff = data;
  Vector3 vect = Vector3.zero;
  vect.x = BitConverter.ToSingle(buff,0*sizeof(float));
  vect.y = BitConverter.ToSingle(buff,1*sizeof(float));
  vect.z = BitConverter.ToSingle(buff,2*sizeof(float));

The main difference is...what if you want to convert 200 vector3's? how would you go about it?

Comment
Add comment · Show 8
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 RobAnthem · Feb 17, 2017 at 03:04 AM 0
Share

By using an iteration loop, assu$$anonymous$$g you don't randomly have 200 Vector3's, if they were in an array you could just say

 List<byte[]> vectorBytes = new List<byte[]>;
 Vector3[] vectors;
 
 for (int i = 0; i < vectors.Length; i++)
 {
      byte[] buff = new byte[sizeof(float)*3];         
      Buffer.BlockCopy( BitConverter.GetBytes( vectors[i].x ), 0, buff, 0*sizeof(float), sizeof(float) );
      Buffer.BlockCopy( BitConverter.GetBytes( vectors[i].y ), 0, buff, 1*sizeof(float), sizeof(float) );
      Buffer.BlockCopy( BitConverter.GetBytes( vectors[i].z ), 0, buff, 2*sizeof(float), sizeof(float) );
      vectorBytes.Add(buff);
 }


Of course if your reasoning is for serialization purposes, it would be easier to make a Position class that is serializable with 3 floats. We all dislike the fact that Unity made Vectors not serializable...

avatar image Ziron999 RobAnthem · Feb 17, 2017 at 03:23 AM 0
Share

actually this is to send a chunk of vector3 information over a network and then convert back on the other side so...and I'm actually wondering how much of a difference this will make..would it be better to just send the vector3 array directly? is converting to bytes even a good idea? I'm not really sure on this.

avatar image RobAnthem Ziron999 · Feb 17, 2017 at 03:25 AM 0
Share

Well no matter what, adding additional conversions will slow things down. If you can avoid ANY conversions, do so. If you can't avoid them, take the least path of resistance.

avatar image Ziron999 RobAnthem · Feb 17, 2017 at 03:28 AM 0
Share

This is exactly what i have feared so....converting will kill the processor sending in full will kill the network speed right?

avatar image RobAnthem RobAnthem · Feb 17, 2017 at 03:32 AM 0
Share

Well I guess it really depends on your project, if you are making an $$anonymous$$$$anonymous$$O then network latency is going to be a very large factor, if you are simply adding local multiplayer then I wouldn't worry about it too much unless you start sending B$$anonymous$$P byte arrays over or something. You have to expect a measure of data transfer and the best way to handle it is through coroutines so that neither the host or client are ever waiting on the other.

avatar image Ziron999 RobAnthem · Feb 17, 2017 at 03:38 AM 0
Share

well i actually am doing the impossible and trying to get an RTS to work on multiplayer across a network so massive amount of information is being sent :( I have made massive optimizations up to this point but can't find a way to get the movement information across the network without it being way to much traffic.

avatar image RobAnthem RobAnthem · Feb 17, 2017 at 03:55 AM 0
Share

Hmm, well if it is grid-based then you may consider solid integers, as the size would be half that of a floating point number. Also I assume there is no jumping or flight, and if there is a flying troop I'd guess it has a specific height, you could completely remove the call to the height vector. Just an idea. As well, just converting, to floats may be faster as a Vector3 contains more data than just 3 floats.

avatar image Ziron999 RobAnthem · Feb 17, 2017 at 03:59 AM 0
Share

Actually i have already thought of just sending y rotation and x/z transform...guess I'll just do that and hope it holds.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

286 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 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 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 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 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 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 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 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 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need some help with to grap .x with a Raycasthit (c#) 1 Answer

EnemyHover Script Error? 0 Answers

C# Question Regarding"new" keyword and Vector3 type 2 Answers


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