Unity2013.1.19_DOTS_Burst compiler

Unity2013.1.19_DOTS_Burst compiler

DOTS是一种新产品,现在尚在起步阶段。由于它处于持续发展中,随着我们努力使其达到最佳状态,您将看到API会不断演变和日趋成熟。

DOTS包含以下元素:

  1. 实体组件系统(ECS) - 提供使用面向数据的方法进行编码的框架。它通过Entities软件包进行分发,您可以通过Package Manager来添加编辑器。

  2. C#作业系统 - 提供一种生成多线程代码的简单方法。它通过Jobs软件包进行分发。

  3. Burst编译器 - 可生成快速、优化的本机代码。它通过Burst软件包进行分发,可通过Package Manager在编辑器中使用。

  4. 本机容器 - 属于ECS数据结构,可提供对内存的控制。

继续跟进DOTS的第三个部分Burst compiler。官方是正道。

Unity - Manual: Burst (unity3d.com)

Burst compiler简介

Burst设计Burst compiler是和JobSystem一起工作的。

在你的代码中使用Burst compiler,先用  [BurstCompile]属性封装一个 Job struct 。

再添加[BurstCompile] 到你想要Burst编译的类型和静态方法中。

Burst compiles your code just-in-time (JIT) while in Play mode in the Editor, and ahead-of-time (AOT) when your application runs in a Player. For more information on compilation, see Burst compilation

using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;

public class MyBurst2Behavior : MonoBehaviour
{
    void Start()
    {
        var input = new NativeArray<float>(10, Allocator.Persistent);
        var output = new NativeArray<float>(1, Allocator.Persistent);
        for (int i = 0; i < input.Length; i++)
            input[i] = 1.0f * i;

        var job = new MyJob
        {
            Input = input,
            Output = output
        };
        job.Schedule().Complete();

        Debug.Log("The result of the sum is: " + output[0]);
        input.Dispose();
        output.Dispose();
    }

    // Using BurstCompile to compile a Job with Burst

    [BurstCompile]
    private struct MyJob : IJob
    {
        [ReadOnly]
        public NativeArray<float> Input;

        [WriteOnly]
        public NativeArray<float> Output;

        public void Execute()
        {
            float result = 0.0f;
            for (int i = 0; i < Input.Length; i++)
            {
                result += Input[i];
            }
            Output[0] = result;
        }
    }
}

参考文档:

官方:

About Burst | Burst | 1.8.12 (unity3d.com)

Unity 之Burst Compile底层原理 - 知乎 (zhihu.com)

Unity Live Help

什么是DOTS?为什么说DOTS非常重要? - Unity Learn

Unity-Technologies/ECS-Network-Racing-Sample: ECS multiplayer racing sample to showcase using Unity Entities and netcode with best practices (github.com)

ECS系列教程:

UnityECS_嵩小帽子啊的博客-CSDN博客[Unity ECS入门]8.System执行顺序-ECS入门-笨木头与游戏开发 (benmutou.com)

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jennifer33K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值