Unity2023.1.19_DOTS_JobSystem

本文详细介绍了Unity2023.1.19中的DOTSJobSystem,包括其工作原理、如何与Burstcompiler配合以提升性能,以及如何使用NativeContainer和CollectionsPackage创建高性能的多线程代码。作者通过示例展示了JobSystem的使用方法和内存管理技巧。
摘要由CSDN通过智能技术生成

Unity2023.1.19_DOTS_JobSystem

上篇我们知道了DOTS是包含Entity Component System,Job System,Burst compiler三者的。接下来看下JobSystem的工作原理和具体实现。

简介:

官方介绍说:JobSystem允许您编写简单而安全的多线程代码,以便您的应用程序可以使用所有可用的CPU内核来执行代码。也就是说JobSystem是为多线程服务的一个模块。

JobSystem可以单独使用,但为了提高性能,也还应该使用Burst compiler,Burst compiler是专门为Unity的JobSystem编译而设计的。urst compiler改进了代码生成,从而提高了性能并减少了移动设备上的电池消耗。Burst compiler将在JobSystem之后再关注。

JobSystem和Burst compiler一起使用的时候,JobSystem工作效果最好。因为Burst不支持托管对象,所以需要使用非托管类型来访问Job中的数据。你可以使用blittable types,或者使用Unity内置的Native container对象,这是一个线程安全的c#本机内存包装器。NativeContainer对象还允许job访问与主线程共享的数据,而不是使用副本。

ECS为其进行了拓展Unity.Collections命名空间以包含其他类型的 NativeContainer:

NativeList - 可调整大小的 NativeArray,类似于List
NativeHashMap<T, R> - 键/值对,类似于Dictionary<T, R>
NativeMultiHashMap<T, R> - 每个键有多个值。
NativeQueue - 先进先出队列,类似于Queue

也可以在ECS中使用JobSystem创建高性能的面向数据代码。

Collections Package提供可在job和Burst-compiled代码中使用的非托管数据结构。

Unity使用原生的JobSystem处理原生代码使用多个工作线程,工作线程取决于你应用程序运行设备的CPU核的可用数量。

通常Unity默认在程序开始时在一个主线程上执行你的代码,你可以使用JobSystem在多个工作线程上执行你的代码,称之为多线程。

简单代码说明:

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

// Job adding two floating point values together
// It implements IJob, uses a NativeArray to get the results of the job, and uses the Execute method with the implementation of the job inside it:

public struct MyJob : IJob
{
    public float a;
    public float b;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = a + b;
    }
}

下面的例子建立在MyJob任务上,在主线程上调度一个任务:

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

public class MyScheduledJob : MonoBehaviour
{
    // Create a native array of a single float to store the result.
    // Using a NativeArray is the only way you can get the results of the job, whether you're getting one value or an array of values.
    NativeArray<float> result;

    // Create a JobHandle for the job
    // 给任务创建一个任务处理
    JobHandle handle;

    // Set up the job
    // 创建一个任务
    public struct MyJob : IJob
    {
        public float a;
        public float b;
        public NativeArray<float> result;

        public void Execute()
        {
            result[0] = a + b;
        }
    }

    // Update is called once per frame
    // 每一帧更新
    void Update()
    {
        // Set up the job data 设置任务数据
        result = new NativeArray<float>(1, Allocator.TempJob);

        MyJob jobData = new MyJob
        {
            a = 10,
            b = 10,
            result = result
        };

        // Schedule the job 安排任务
        handle = jobData.Schedule();
    }

    private void LateUpdate()
    {
        // Sometime later in the frame, wait for the job to complete before accessing the results.
        // 稍后在框架中,等待作业完成后再访问结果。
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        // float aPlusB = result[0];

        // Free the memory allocated by the result array
        // 释放由结果数组分配的内存
        result.Dispose();
    }


}

 跟一个测试:

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

public class MyScheduledJob : MonoBehaviour
{
    // Create a native array of a single float to store the result.
    // Using a NativeArray is the only way you can get the results of the job, whether you're getting one value or an array of values.
    NativeArray<float> result;

    // Create a JobHandle for the job
    // 给任务创建一个任务处理
    JobHandle handle;

    // Set up the job
    // 创建一个任务
    public struct MyJob : IJob
    {
        public float a;
        public float b;
        public NativeArray<float> result;

        public void Execute()
        {
            result[0] = a + b;
        }
    }

    // Update is called once per frame
    // 每一帧更新
    void Update()
    {
        // Set up the job data 设置任务数据
        result = new NativeArray<float>(1, Allocator.TempJob);

        MyJob jobData = new MyJob
        {
            a = 10,
            b = 10,
            result = result
        };

        // Schedule the job 安排任务
        handle = jobData.Schedule();
    }

    private void LateUpdate()
    {
        // Sometime later in the frame, wait for the job to complete before accessing the results.
        // 稍后在框架中,等待作业完成后再访问结果。
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        // float aPlusB = result[0];

        // Free the memory allocated by the result array
        // 释放由结果数组分配的内存
        result.Dispose();
    }


}

 

意思大概是这样了,具体实践具体看!!

参考文档:

官方文档

Unity - Manual: Job system overview (unity3d.com)

Collections package | Collections | 2.2.1 (unity3d.com)

Unity - Manual: Thread safe types (unity3d.com)

这个博主系列的讲到了DOTS,可以看一下:

DOTS ECS_铸梦xy的博客-CSDN博客

 

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中,要使用android.permission.READ_LOGS权限,需要根据不同的Unity版本和Bugly Unity Plugin版本进行相应的配置。根据引用[1]中的Android SDK使用指南,可以修改导出的Android工程的AndroidManifest.xml文件,将android.permission.READ_LOGS权限添加到权限声明中。具体步骤如下: 1. 打开Unity项目工程。 2. 导入最新版本的Bugly Unity Plugin。根据引用中的通用部分集成步骤,下载并导入Bugly Unity Plugin的相关文件到您的Unity工程中。 3. 打开导出的Android工程的AndroidManifest.xml文件。这个文件位于Unity项目工程的Assets/Plugins/Android目录下。 4. 在AndroidManifest.xml文件中的权限声明部分,添加如下权限: <uses-permission android:name="android.permission.READ_LOGS" /> 通过以上步骤,您就可以将android.permission.READ_LOGS权限添加到Unity项目的AndroidManifest.xml文件中,以实现读取logcat日志的功能。请注意,根据具体的Bugly Unity Plugin版本和Unity版本,可能还需要执行其他配置和集成步骤,具体可以参考Bugly Unity Plugin的官方文档或相关资源。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Bugly Unity Plugin](https://blog.csdn.net/qq_39816832/article/details/80238872)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [unity--关于自动添加READ_PHONE_STATE权限](https://blog.csdn.net/lalate/article/details/84340644)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jennifer33K

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

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

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

打赏作者

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

抵扣说明:

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

余额充值