Page 1 of 1

Support for NativeArray<T0> [ADDED]

Posted: Mon Jul 16, 2018 6:28 am
by Joel
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.

Re: Support for NativeArray<T0> and other low-level collecti

Posted: Tue Aug 07, 2018 9:38 pm
by Cabo
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);
		}
	}

Re: Support for NativeArray<T0> and other low-level collecti

Posted: Wed Aug 08, 2018 8:25 am
by Joel
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

Re: Support for NativeArray<T0> and other low-level collections

Posted: Thu Jun 30, 2022 1:04 am
by Cranktrain
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.

Re: Support for NativeArray<T0> [ADDED]

Posted: Thu Jun 30, 2022 11:15 am
by Joel
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

Re: Support for NativeArray<T0> [ADDED]

Posted: Thu Jun 30, 2022 12:49 pm
by Cranktrain
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!