找回密码
 立即注册
查看: 332|回复: 0

[笔记] Unity JobSystem Burst学习

[复制链接]
发表于 2022-4-22 16:50 | 显示全部楼层 |阅读模式
JobSystem 是Unity Dots(多线程运行框架)的核心组件之一,可以充分利用当前CPU的多核特性,方便书写线程安全的多线程代码。
JobSystem 的数据结构必须是一个struct,因为Dots框架的内存完全是由框架自己管理,Dots目前对引用类型的资源管理尚不可靠,所有目前使用JobSystem 必须都是值类型。一张图表示可以使用的数量类型


1.创建工程


2.导入Burst unityPackage



3.创建TestBurst脚本


4.编写脚本
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using Unity.Burst;


public class TestBurst : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {

    }

    void TestCount()
    {
        int value = 0;
        for (int i = 0; i < 999999; i++)
        {
            value += i;
        }

    }


    // Update is called once per frame
    void Update()
    {
        float realstarttime = Time.realtimeSinceStartup;


        //for (int i = 0; i < 10; i++)
        //{
        //    TestCount();
        //}


        //===================Job=================
        //TestJob m_jonb= new TestJob();
        //JobHandle handle= m_jonb.Schedule();
        //handle.Complete();

        //=============NativeArray===========================
        //NativeArray<JobHandle> jobHandles = new NativeArray<JobHandle>(10, Allocator.Temp);

        //for (int i = 0; i < 10; i++)
        //{
        //    TestJob m_jonb = new TestJob();
        //    JobHandle handle = m_jonb.Schedule();
        //    jobHandles = handle;
        //}
        //JobHandle.CompleteAll(jobHandles);
        //jobHandles.Dispose();

        //========================IJobParallelFor==============

        TestParallelJob jobs = new TestParallelJob();
        JobHandle jobHandle = jobs.Schedule(10, 32);
        jobHandle.Complete();//Complete在调用的时候 会卡主主线程 当有多个complete方法调用的时候 可以合并后调用一次


        float realendTime = Time.realtimeSinceStartup;

        Debug.Log((realendTime-realstarttime)*1000);
    }

}

/// <summary>
///Burst 支持的类型 bool char sbyte/ byte short/ ushort int/ uint long/ ulong float double
/// </summary>
[BurstCompile]//Burst是一个编译器,它使用LLVM将IL / .NET字节码转换为高度优化的本机代码。它作为Unity包发布,并使用Unity Package Manager集成到Unity中。使用属性[BurstCompile]装饰Job结构,从而在代码中简单地使用burst编译器 。
public struct TestJob : IJob //IJob将运算运行在一个新的单一线程中
{
    public void Execute()
    {
        int value = 0;
        for (int i = 0; i < 999999; i++)
        {
            value += i;
        }
    }
}

[BurstCompile]
public struct TestParallelJob : IJobParallelFor
{
    public void Execute(int index)
    {
        int value = 0;
        for (int i = 0; i < 999999; i++)
        {
            value += i;
        }

    }
}5.开启Profile观察线程运行



6.总结:
     面向数据进行抽象
     提取数据利用for循环完成业务逻辑
     串行job化(JobHandle.Complete())
     Profiler调优
     梳理jobl依赖关系,去掉中间过程的Complete调用
     开启Burst

附上官网直播的讲解:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 08:44 , Processed in 0.134539 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表