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

Unity DOTS系列之Aspect核心机制分析

最近DOTS发布了正式的版本, 我们来分享一下DOTS里面Aspect机制,方便大家上手学习掌握Unity DOTS开发。

Aspect 机制概述

当我们使用ECS开发的时候,编写某个功能可能需要某个entity的一些组件,如果我们一个个组件的查询出来,可能参数会写很长。如果我们编写某个功能的时候,需要entity的一些组件的引用,我们如何高效的来获得呢?Unity DOTS引入了Aspect机制。

对惹,这里有一个游戏开发交流小组,大家可以点击进来一起交流一下开发经验呀!

Aspect是一个特殊的数据结构,可以把它理解为是entity中一些组件的引用Wrapper”包装盒”,把entity中的一些组件的引用包含在一起。方便在System中通过这个Aspect来获取entity当中的组件的引用,高效方便的访问entity中的一些组件数据。定义一个Aspect, 需要继承自Iaspect Interface, Aspect里面的成员可以包含以下的内容:

  • Entity类型的引用;
  • RefRW<T> 与RefRO<T>组件数据的引用;
  • EnabledRefRW与EnabledRefRO的Enable Component组件数据的引用;
  • DynamicBuffer<T> 类型数据buffer;
  • shared component 类型的组件的引用;
  • 其它的Aspect类型;

Aspect定义与使用

定义一个Aspect,需要定义一个readonly的partial结构体并继承自接口类IAspect。

using Unity.Entities; 
readonly partial struct MyAspect : IAspect 
{ // Your Aspect code 
}

结构体里面的字段可以使用上面字段所规定的类型, 我们还可以把某个字段通过attribute设置为[Optional]。这样这个字段在entity里面就不是必须的,如果某个entity类没有这个可选字段,也能生成对应的Aspect。如果想要DynamicBuffer字段为只读,可以定义attibute [ReadOnly]。RefRO修饰的组件是只读的,RefRW修饰的组件可读写。

在System中我们要基于定义好的Aspect类型来操作entity中的组件数据,我们可以为Entity生成一个Aspect对象。通过API:SystemAPI.GetAspect<TASpect>来获取entity对应的Aspect对象。

// Throws if the entity is missing any of // the required components of MyAspect. MyAspect asp = SystemAPI.GetAspect<MyAspect>(myEntity);

如果这个entity类型无法生成对应的Aspect,那么asp就会返回null。当我们在System中需要迭代所有Entity的某种Aspect,可以使用API:

SystemAPI.Query

参考代码如下:

struct CannonBall : IComponentData
{public float3 Speed;
}// Aspects must be declared as a readonly partial struct
readonly partial struct CannonBallAspect : IAspect
{// An Entity field in an Aspect gives access to the Entity itself.// This is required for registering commands in an EntityCommandBuffer for example.public readonly Entity Self;// Aspects can contain other aspects.// A RefRW field provides read write access to a component. If the aspect is taken as an "in"// parameter, the field behaves as if it was a RefRO and throws exceptions on write attempts.readonly RefRW<LocalTransform> Transform;readonly RefRW<CannonBall> CannonBall;// Properties like this aren't mandatory. The Transform field can be public instead.// But they improve readability by avoiding chains of "aspect.aspect.aspect.component.value.value".public float3 Position{get => Transform.ValueRO.Position;set => Transform.ValueRW.Position = value;}public float3 Speed{get => CannonBall.ValueRO.Speed;set => CannonBall.ValueRW.Speed = value;}
}
public partial struct MySystem : ISystem
{public void OnUpdate(ref SystemState state){foreach (var cannonball in SystemAPI.Query<CannonBallAspect>()){// use cannonball aspect here}}
}

上面代码中定义了一个struct CannonBall 的ComponentData, 定义了一个CannonBallAspect,包含了entity本身引用,以及所需要的其它组件的引用(字段里面还可以基于get/set)。System中通过查询当前World里面所有含有CannonBallAspect对象的entity,然后统一处理它们。

Aspect的代码自动生成

不同类型的Entity可能有同一个类型的Aspect,那么Unity DOTS如何来处理呢?例如Entity类型A与Entity类型B,都有Aspect所定义的组件与引用,那么系统如何把A类型的Entity与B类型的Entity都生成它对应的Aspcet对象呢?那么这个时候就需要通过扫描所有的代码,来自动生成相关的代码自动生成对应的伪代码如下:

MyAspect CreateAspectWithEntityA(entity实例) { Var myAspect = new MyAspect();把A类entity实例对应的ArchType的ComponentData块的引用,生成一个MyAspect实例。Return myAspect;
}
MyAspect CreateAspectWithEntityB(entity实例) { Var myAspect = new MyAspect();把B类entity实例对应的ArchType的ComponentData块的引用,生成一个MyAspect实例。Return myAspect;
}

entity是否具有某种Aspcet类型的Aspect,也会被快速的生成出来,这样再查询的时候都可以提升查询的速度。具体可以参考相关源码。

http://www.lryc.cn/news/446245.html

相关文章:

  • webpack 的打包target讲解 node环境打包下的文件存储造成不易察觉的坑点
  • JVM面试问题集
  • Go weak包前瞻:弱指针为内存管理带来新选择
  • ZStack AIOS平台智塔入选2024世界计算大会专题展优秀成果
  • 总结 自行解决问题经验
  • 软件设计模式——工厂模式
  • Apache Druid命令执行(CVE-2021-25646)
  • 新的 MathWorks 硬件支持包支持从 MATLAB 和 Simulink 模型到高通 Hexagon 神经处理单元架构的自动化代码生成
  • gorm.io/sharding:改造,当查询条件中不包含分表键时,从自定义方法中获取对应的表进行查询
  • 【CSS】变量的声明与使用
  • 【数学分析笔记】第3章第4节闭区间上的连续函数(1)
  • Mybatis中sql数组为空判断
  • 12.第二阶段x86游戏实战2-CE找基地址
  • 笔记整理—内核!启动!—linux应用编程、网络编程部分(3)文件共享与标准IO
  • plt常用函数介绍一
  • 基于ExtendSim的 电子制造 仿真模型
  • BGP 路由反射器
  • CSRF高级防御绕过
  • MySQL安装文档-Windows
  • html TAB、table生成
  • 2024!再见前端!
  • 【源码+文档+调试讲解】人事管理系统设计与实现Python
  • 基于注意力机制的图表示学习:GRAPH-BERT模型
  • linux服务器安装原生的php环境
  • 数电学习基础(逻辑门电路+)
  • 【艾思科蓝】Spring Boot实战:零基础打造你的Web应用新纪元
  • C++ 二叉树
  • 初探IT世界:从基础到未来
  • 一区黏菌算法+双向深度学习+注意力机制!SMA-BiTCN-BiGRU-Attention黏菌算法优化双向时间卷积双向门控循环单元融合注意力机制多变量回归预测
  • 机器翻译之Bahdanau注意力机制在Seq2Seq中的应用