其他分享
首页 > 其他分享> > 将Vector[N]转化为Byte[]

将Vector[N]转化为Byte[]

作者:互联网

Unity将Vector[N]转化为Byte[]

方案1:

 Vector3 vector = Vector3.Zero;
     
 byte[] array = new byte[3];
     
 array[0] = vector.x;
 array[1] = vector.y;
 array[2] = vector.z;

方案2:

从VectorN转换为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) );
...N

 从byte []转换为VectorN:

 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));
...N

有关自定义了类型与byte[]之间的转换:

https://answers.unity.com/questions/174207/not-able-to-pass-an-instance-of-a-class-over-a-net.html?_ga=2.31779518.1662844682.1612841508-643014643.1591682807

标签:byte,float,转化,vect,Vector,BitConverter,Byte,sizeof,buff
来源: https://blog.csdn.net/Yangdongdong_135/article/details/113768426