Hi,
* Update: 04.Sep.2018
TransformMatrix + array has renamed to LocalToWorld in Entities preview.11
Based on the unity Matrix4x4 would be also recommended.
Further Float4x4 + array and another great new fields based on Unity.Mathematics would be required in order use Job and ECS.
(Unity.Mathematics owns some further great fields that can be implemented.)
It's not possible not transform Matrix4x4 = LocalToWorld directly. The only way currently is:
var tfvalue = LocalToWorld .Value; // .Value transform to float4x4
Matrix4x4.SetColumn(0, tfvalue.c0);
Matrix4x4.SetColumn(1, tfvalue.c1);
Matrix4x4.SetColumn(2, tfvalue.c2);
Matrix4x4.SetColumn(3, tfvalue.c3);
and
tfvalue.c0 = Matrix4x4.GetColumn(0);
tfvalue.c1 = Matrix4x4.GetColumn(1);
tfvalue.c2 = Matrix4x4.GetColumn(2);
tfvalue.c3 = Matrix4x4.GetColumn(3);
LocalToWorld .Value = tfvalue;
Additionals:
The following Code describe how to save a NativeArray<t> into a standard Byte Array. The Byte Array can be saved by EasySave directly.
Code: Select all
// Declare a float4 NativeArray
var float4_NA = new NativeArray<float4>(10, Allocator.Persistent);
float4_NA[1] = 1.11f;
float4_NA[9] = 9.99f;
// Create a Byte Array and save contents from the NativeArray
byte[] b_Array = new byte[0];
SaveToByteArray(ref float4_NA, ref b_Array);
// Test:
// Dispose NativeArray and declare a different NativeArray that does not fit with stored NativeArray in the ByteArray
float4_NA.Dispose();
float4_NA = new NativeArray<float4>(0, Allocator.Persistent);
LoadFromByteArray(ref b_Array, ref float4_NA);
// Display the contents of the loaded NativeArray from the ByteArray
Debug.Log(float4_NA[1] + " " + float4_NA[9]);
public unsafe void SaveToByteArray<T>(ref NativeArray<T> src, ref byte[] dst) where T : struct {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(NativeArrayUnsafeUtility.GetAtomicSafetyHandle(src));
if (dst == null)
throw new ArgumentNullException(nameof(dst));
#endif
var size = UnsafeUtility.SizeOf<T>() * src.Length;
var srcAddr = (byte*)src.GetUnsafeReadOnlyPtr();
if (dst.Length != size)
dst = new byte[size];
fixed (byte* dstAddr = dst) {
UnsafeUtility.MemCpy(&dstAddr[0], &srcAddr[0], size);
}
}
public unsafe void LoadFromByteArray<T>(ref byte[] src, ref NativeArray<T> dst) where T : struct {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(NativeArrayUnsafeUtility.GetAtomicSafetyHandle(dst));
if (src == null)
throw new ArgumentNullException(nameof(src));
#endif
var size = UnsafeUtility.SizeOf<T>();
if (src.Length != (size * dst.Length)) {
dst.Dispose();
dst = new NativeArray<T>(src.Length / size, Allocator.Persistent);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(NativeArrayUnsafeUtility.GetAtomicSafetyHandle(dst));
#endif
}
var dstAddr = (byte*)dst.GetUnsafeReadOnlyPtr();
fixed (byte* srcAddr = src) {
UnsafeUtility.MemCpy(&dstAddr[0], &srcAddr[0], src.Length);
}
}