Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dinesh-Singh · Nov 20, 2018 at 02:52 PM · vector3bytearray

Convert Vector3 array (vector3[]) to byte array (byte[])

I know its very bad question to ask but whatever I'm stuck in there so please help me to get out of it. If you guys have any link or simple solution for this please help me .

P.S Once again - I want to convert array of vector 3 into byte array not vector3 in byte/bytearray.

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 bgulanowski · Nov 20, 2018 at 05:15 PM 0
Share

You want to fit an entire vector3 inside a byte? Did you realize that you are trying to fix 96 bits of information into 8 bits? You will only get 2 bits per vector element (well, one of them can be 3 bits if you want). That's only going to let you encode 256 possible vectors. You might as well use an indexing system, where each byte is an index value into an array of vector3s. But still, only 256 possibly index values means only 256 possible vector values.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SamuelRoos · Nov 21, 2018 at 04:32 PM

What I did when I had to serialize values (like Vector3) over network, was using serialization with MemoryStream (which you can convert into a byte array).


I had three classes, one for arrays, one for ushort and one for the actual Vector3.


The array class requires a type T input which is the type you want to convert (in this case it's Vector3). It also takes an Action, which is either a serialize- or deserialize function from the Vector3Proxy class.


The UShort class is just for converting the array length; I was using ushort instead of ints since the type contains less data to send.


This all relies on a system type called MemoryStream. Values from types are written in there, and can be afterwards converted into a byte array using ToArray().


(If you want to access the MemoryStream or the Stream class, you need to add the using System.IO tag)


An example of doing the conversion:

If you want to convert a Vector3 array to a byte array, this is how you do it with these three classes:


 using (MemoryStream memoryStream = new MemoryStream())
 {
     ArrayProxy<Vector3>.Serialize(
                memoryStream, 
                yourVector3Variable, 
                Vector3Proxy.Serialize);
 
         //Here is the result
         bytes[] conversionArray = memoryStream.ToArray();
 }

This converts the Vector3 array into a byte array. However, if you want to serialize it back from the byte array to a Vector3 array, it's done like this:

 byte[] conversionArray = yourConvertedByteArray;
 
 using (MemoryStream memoryStream = new MemoryStream(conversionArray))
 {
          //Here is the result
     Vector3[] data = ArrayProxy<Vector3>.Deserialize(memoryStream, Vector3Proxy.Deserialize);
 }


The Proxy Classes:


The array proxy class of a type T:

 public static class ArrayProxy<T>
 {
     public static void Serialize(Stream bytes, T[] instance, Action<Stream, T> serialization)
     {
         UShortProxy.Serialize(bytes, (ushort)instance.Length);
         foreach (T arg in instance)
         {
             serialization(bytes, arg);
         }
     }
 
     public static T[] Deserialize(Stream bytes, ArrayProxy<T>.Deserializer<T> serialization)
     {
         ushort num = UShortProxy.Deserialize(bytes);
         T[] array = new T[(int)num];
         for (int i = 0; i < (int)num; i++)
         {
             array[i] = serialization(bytes);
         }
         return array;
     }
 
     public delegate void Serializer<U>(Stream stream, U instance);
 
     public delegate U Deserializer<U>(Stream stream);
 }

The UShort proxy class:

 public static class UShortProxy
 {
     public static void Serialize(Stream bytes, ushort instance)
     {
         byte[] bytes2 = BitConverter.GetBytes(instance);
         bytes.Write(bytes2, 0, bytes2.Length);
     }
 
     public static ushort Deserialize(Stream bytes)
     {
         byte[] array = new byte[2];
         bytes.Read(array, 0, 2);
         return BitConverter.ToUInt16(array, 0);
     }
 }

The Vector3 Proxy class:

 public static class Vector3Proxy
 {
     public static void Serialize(Stream bytes, Vector3 instance)
     {
         bytes.Write(BitConverter.GetBytes(instance.x), 0, 4);
         bytes.Write(BitConverter.GetBytes(instance.y), 0, 4);
         bytes.Write(BitConverter.GetBytes(instance.z), 0, 4);
     }
 
     public static Vector3 Deserialize(Stream bytes)
     {
         byte[] array = new byte[12];
         bytes.Read(array, 0, 12);
         return new Vector3(BitConverter.ToSingle(array, 0), BitConverter.ToSingle(array, 4), 
             BitConverter.ToSingle(array, 8));
     }
 }


Make sure to comment if there any errors or something you'd like to know!

Comment
Add comment · Show 1 · 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 Midenberg · Mar 08, 2020 at 01:10 AM 0
Share

It may be late to comment now, but the data is surely converted to bytes I just cant make the byte[] saved data to deserialize and fill in the vector[]. When I try that the return values are zero, the vector[] which was earlier populated is then empty. I don't know what the chances are for anyone to respond after few years have passed, but I will take my chances as this is very important for me and my project. Best regards!

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

119 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

Related Questions

Converting Vector3[] to byte[] 0 Answers

Converting Vector3 to Byte[]? 4 Answers

Object faces -180 degrees away 1 Answer

Vector3.Lerp doesn't work on build 0 Answers

Whats the best way to get the Distance between two Vector3? 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