Status
Added in 3.4.3
Complexity
5/10
Description
Support for the new native collection types in Unity 2018.2, which use the new 'Job' system.
NativeArray<T>: https://docs.unity3d.com/ScriptReferenc ... ray_1.html
NativeList<T>, NativeHashMap<T> and NativeQueue<T> are also available, but currently undocumented by Unity.
Support for NativeArray<T0> [ADDED]
Re: Support for NativeArray<T0> and other low-level collecti
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.
* 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);
}
}
Last edited by Cabo on Mon Sep 03, 2018 10:33 pm, edited 2 times in total.
Re: Support for NativeArray<T0> and other low-level collecti
Thanks for the suggestions and information. We'll look to add support for those also when coming to implement these features.
All the best,
Joel
All the best,
Joel
- Cranktrain
- Posts: 16
- Joined: Thu Nov 09, 2017 7:15 pm
Re: Support for NativeArray<T0> and other low-level collections
Hi! Any movement on this feature, now we're a few years down the line and the collections package is more mature?
Right now I'm unpacking my Unity.Collections.NativeHashMap into a System.Collections.Generic.Dictionary on save, and then back again on load. It's not that big a deal, it's just unnecessary boilerplate that I don't love writing every time. I've a feeling that with DOTS becoming more and more the way of doing things in Unity it's a feature that'll become more important in the next few years.
Right now I'm unpacking my Unity.Collections.NativeHashMap into a System.Collections.Generic.Dictionary on save, and then back again on load. It's not that big a deal, it's just unnecessary boilerplate that I don't love writing every time. I've a feeling that with DOTS becoming more and more the way of doing things in Unity it's a feature that'll become more important in the next few years.
Re: Support for NativeArray<T0> [ADDED]
Hi there,
Coincidentally we're adding support for NativeArray in the next update. If you private message me your invoice number I can send an advance copy of this to you.
We are unable to add NativeSlice as the nature of how it works means it's unsuitable for runtime serialisation.
We're also not currently adding support for other Native collections as these exist within a package which is not installed by default, and Unity has yet to provide a clean way for assets to conditionally compile based on what packages are installed. We'll create a separate feature request for this however.
All the best,
Joel
Coincidentally we're adding support for NativeArray in the next update. If you private message me your invoice number I can send an advance copy of this to you.
We are unable to add NativeSlice as the nature of how it works means it's unsuitable for runtime serialisation.
We're also not currently adding support for other Native collections as these exist within a package which is not installed by default, and Unity has yet to provide a clean way for assets to conditionally compile based on what packages are installed. We'll create a separate feature request for this however.
All the best,
Joel
- Cranktrain
- Posts: 16
- Joined: Thu Nov 09, 2017 7:15 pm
Re: Support for NativeArray<T0> [ADDED]
Thanks Joel, I'll skip the early access only because I'm looking mainly at NativeHashMap (and NativeMultiHashMap) rather than NativeArray. Glad it's coming together though!