当前位置: 首页 > article >正文

【Unity3D】ECS入门学习(十一)ComponentSystem、JobComponentSystem

 ComponentSystem:仅支持主线程执行,不支持多线程,并且无法使用SystemBase介绍的扩展方法。

using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;

/// <summary>
/// 仅主线程执行,不支持多线程
/// </summary>
public class MyComponentSystem : ComponentSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref Translation trans) =>
        {
            trans.Value = new float3(0, 1, 1);
        });//无法使用SystemBase的其他扩展功能
    }
}

JobComponentSystem:仅支持多线程,不支持主线程,可以使用扩展方法,并且重写的方法与其他都不同,它需要一个JobHandle返回值 

using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
/// <summary>
/// 支持多线程 但不支持主线程执行
/// </summary>
public class MyJobComponentSystem : JobComponentSystem
{
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        return Entities.ForEach((ref Translation trans) =>
        {
            trans.Value = new Unity.Mathematics.float3(0, 0, 1);
        }).WithName("MyName").Schedule(inputDeps);//可以使用SystemBase扩展方法
    }
}

具体文档:Using Entities.ForEach | Entities | 0.50.1-preview.2

如下图是三个执行筛选的方法限制,简单理解就是:
Run:非Burst编译下 可访问所有成员,可修改实体。【主线程执行】
Schedule:仅能获取局部成员,可修改实体。【新开一个后台线程执行】
ScheduleParallel:仅能获取局部成员,可以读取实体,但不能修改实体,并且可读数据只允许存储到WithReadOnly(xxx)方法标记的只读变量内。【多线程并行执行】

Supported FeatureRunScheduleScheduleParallel
Capture local value typexxx
Capture local reference typex (only WithoutBurst and not in ISystem)
Writing to captured variablesx
Use field on the system classx (only WithoutBurst)
Methods on reference typesx (only WithoutBurst and not in ISystem)
Shared Componentsx (only WithoutBurst and not in ISystem)
Managed Componentsx (only WithoutBurst and not in ISystem)
Structural changesx (only WithStructuralChanges and not in ISystem)
SystemBase.GetComponentxxx
SystemBase.SetComponentxx
GetComponentDataFromEntityxxx (only as ReadOnly)
HasComponentxxx
WithDisposeOnCompletionxxx
WithScheduleGranularityx

http://www.kler.cn/a/456172.html

相关文章:

  • information_schema是什么?
  • Python小括号( )、中括号[ ]和大括号{}代表什么
  • 仓颉语言实战——2.名字、作用域、变量、修饰符
  • 在C#中实现事件的订阅和解除订阅
  • C++ OCR 文字识别
  • Redis——数据淘汰策略
  • 关于启动vue项目,出现:Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘xxx‘此类错误
  • Java与SQL Server数据库连接的实践与要点
  • web服务器之云主机、物理机租用、服务器托管的区别
  • sql server index
  • SQL 实战:字符串处理函数 – 数据清洗与文本格式化
  • CSS系列(41)-- Logical Properties详解
  • 数据结构课程设计/校园导游程序及通信线路设计 #3
  • 银河麒麟操作系统安装达梦数据库(超详细)
  • 路径规划之启发式算法之二十四:爬山算法(Hill Climbing Algorithm,HCA)
  • 《揭秘Mask R-CNN:开启智能视觉新征程》
  • FreeRTOS实战——一、基于HAL库项目的FreeRTOS移植步骤
  • [江科大编程技巧] 第1期 定时器实现非阻塞式程序 按键控制LED闪烁模式——笔记
  • SQL 实战:复杂数据去重与唯一值提取
  • Android——自定义按钮button