https://forum.unity.com/threads/nativearray-and-mesh.522951/#post-3841096
http://www.manongjc.com/article/119962.html
https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html
https://www.urablog.xyz/entry/2018/03/24/140423
a NativeArray exposes a buffer of native memory to managed code, making it possible to share data between managed and native without marshalling costs.
behind the scenes, NativeArrays provide systems that allows them to be used safely with jobs, and automatic tracking of memory leaks.
using Unity.Collections;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
struct MyData { public float Value; }
private NativeArray<MyData> input1;
void Start()
{
input1 = new NativeArray<MyData>(new[] {
new MyData { Value = 11 },
new MyData { Value = 22 },
new MyData { Value = 33 },
}, Allocator.Temp);
ShowFloatLog(input1);
}
private void OnDestroy()
{
if(input1.IsCreated)
{
input1.Dispose();
}
}
void ShowFloatLog(NativeArray<MyData> inputs)
{
foreach (var data in inputs)
{
Debug.LogError(data.Value);
}
}
}
上面有几种方式:
//
// 摘要:
// Used to specify allocation type for NativeArray.
[UsedByNativeCode]
public enum Allocator
{
//
// 摘要:
// Invalid allocation.
Invalid = 0,
//
// 摘要:
// No allocation.
None = 1,
//
// 摘要:
// Temporary allocation.
Temp = 2,
//
// 摘要:
// Temporary job allocation.
TempJob = 3,
//
// 摘要:
// Persistent allocation.
Persistent = 4,
//
// 摘要:
// Allocation associated with a DSPGraph audio kernel.
AudioKernel = 5
}
上面的代码,我们使用的是Temp,这个在unity停止之后不会报错。
如果是TempJob或者是Persistent,那么unity停止之后就会报错:
此时需要进行回收处理,将这段代码打开:
private void OnDestroy()
{
if(input1.IsCreated)
{
input1.Dispose();
}
}
下面我们来看看拷贝内存的方法。
using Unity.Collections;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
struct MyData { public float Value; }
private NativeArray<MyData> input1;
void Start()
{
MyData[] datas = new MyData[2];
datas[0] = new MyData { Value = 1};
datas[1] = new MyData { Value = 2 };
input1 = new NativeArray<MyData>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
input1.CopyFrom(datas);
ShowFloatLog(input1);
}
private void OnDestroy()
{
if (input1.IsCreated)
{
input1.Dispose();
}
}
void ShowFloatLog(NativeArray<MyData> inputs)
{
foreach (var data in inputs)
{
Debug.LogError(data.Value);
}
}
}
上面是开辟了长度为2的NativeArray,然后执行拷贝。注意要释放资源。
using Unity.Collections;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
struct MyData { public float Value; }
private NativeArray<MyData> input1;
void Start()
{
MyData[] datas = new MyData[2];
datas[0] = new MyData { Value = 1 };
datas[1] = new MyData { Value = 2 };
input1 = new NativeArray<MyData>(2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
input1.CopyFrom(datas);
MyData[] datas2 = new MyData[2];
input1.CopyTo(datas2);
for (int i = 0; i < datas2.Length; ++i)
{
Debug.LogError(datas2[i].Value);
}
}
private void OnDestroy()
{
if (input1.IsCreated)
{
input1.Dispose();
}
}
void ShowFloatLog(NativeArray<MyData> inputs)
{
foreach (var data in inputs)
{
Debug.LogError(data.Value);
}
}
}
使用不安全的方式拷贝:
using Unity.Collections;
using UnityEngine;
using Unity.Collections.LowLevel.Unsafe;
public class NewBehaviourScript1 : MonoBehaviour
{
private NativeArray<int> input1;
void Start()
{
int[] datas = new int[2];
datas[0] = 1;
datas[1] = 2;
input1 = new NativeArray<int>(2, Allocator.Persistent, NativeArrayOptions.ClearMemory);
unsafe
{
fixed(int* source = &datas[0])
{
int* dest = (int*)input1.GetUnsafePtr();
UnsafeUtility.MemCpy(dest, source, sizeof(int) * 1);
}
}
}
private void OnDestroy()
{
if (input1.IsCreated)
{
input1.Dispose();
}
}
}
这里要勾上:
负责编译不过。
以后还可尝试拷贝下结构体。
如果是编译成dll的C#项目,则需要这里设置下:
单纯的unity项目,是没有这个选项,只能在Palyer Settings里面设置下。
关于NatvieArray的使用,到此告一段落。