unity NativeArray

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的使用,到此告一段落。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值