Unity DOTS1.0(8) Aspect核心机制分析

devtools/2024/10/18 8:20:11/

Aspect

概念:

  • ASPECT是一种用于描述Entity的特性或特征的概念。ASPECT通常用于在系统中筛选出具有特定组件集合的实体。 你可以把5~6个组件得引用保存到一个数据对象里面(Aspect),你通过Aspect就可以拿到这些组件得引用,从而访问这些组件数据;

  • Unity 会为关联的Components组,提供预定义好的aspects。你也可以通过IAspect接口定义自己的Aspect

  • Aspect 可以包含的字段类型:

    • Entity字段:存放EntityID
    • ComponentData的引用:RefRW与RefRO
    • EnabledRefRW与EnabledRefRO fields,可以包含Enbale Component的这个引用,可读写,或只读;
    • DynamicBuffer:这个是可以包含; IBufferElementData
    • lSharedComponent:类型的字段;读取共享的ComponentData;
    • Other aspect types: 也可以在你的Aspect里面引入其它的Aspect;

创建一个Aspect

  • 定义一个Aspect类型: readonly partial stuct 类名 : IAspet{}

  • 当你定义了好一个Aspect类以后,定义与entity对应组件的引用,再用Entity创建Aspect实例,这样当Aspect初始化好了,字段的里面就是Entity中的component数据的引用;

  • 实例:

using Unity.Entities;
readonly partial struct MyAspect : IAspect
{RefRW<transform> a;
}aspect = new Aspect(Enity);
Aspectt.a //这个就是Entity的Transfrom
  • 如果某个字段可以不用制定值: [Optional]来修饰这个字段; 那么这个字段就不是必须的;

  • Apsect就是开发某个代码逻辑的时候,需要entity里面的一些组件时,就定以一个Aspect,然后里面包含了这些引用。

  • 在System中可以通过SystemAPI.GetAspect 来获得一个Entity的Aspect实例:

MyAspect asp = SystemAPI.GetAspect<MyAspect>(myEntity);
  • 在System外,可使用EntityManager.GetAspect 获取
  • 迭代World Entity中某个特定的Aspect,使用SystemAPI.Query来查询

实例代码:

	struct CannonBall : IComponentData{public float3 Speed;}// Aspects must be declared as a readonly partial structreadonly 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}}}

http://www.ppmy.cn/devtools/34470.html

相关文章

读天才与算法:人脑与AI的数学思维笔记19_深度数学

1. 深度数学 1.1. 组合与选择&#xff0c;是发明新事物的两个不可或缺的条件 1.1.1. 保尔瓦雷里&#xff08;Paul Valry&#xff09; 1.2. 利用以往的数学定理证明过程训练算法&#xff0c;以发现新的定理 1.3. 谷歌设在伦敦的总部整体有一种现代牛津大学的感觉&#xff0c…

Cocos Creator UICanvas详解与2D游戏配置详解

前言 Cocos Creator是一款强大的2D游戏开发引擎&#xff0c;提供了丰富的工具和组件来帮助开发者快速制作出优秀的游戏作品。其中&#xff0c;UICanvas是Cocos Creator中一个非常重要的组件&#xff0c;用于管理游戏中的UI界面。 在本文中&#xff0c;我们将深入探讨Cocos Cr…

虚幻引擎5 Gameplay框架(二)

Gameplay重要类及重要功能使用方法&#xff08;一&#xff09; 配置LOG类及PlayerController的网络机制 探索验证GamePlay重要函数、类的执行顺序与含义 我们定义自己的日志&#xff0c;专门建立一个存放自己日志的类&#xff0c;这个类继承自BlueprintFunctionLibrary 然后…

uniapp 应用闪退、崩溃异常日志捕获插件(可对接网络上报)插件 Ba-Crash

应用闪退、崩溃异常日志捕获插件&#xff08;可对接网络上报&#xff09; Ba-Crash 简介&#xff08;下载地址&#xff09; Ba-Crash 是一款uniapp应用闪退、崩溃异常日志捕获插件&#xff0c;支持对接网络上报、设置提示等等&#xff0c;方便对一些远程问题、原生问题进行分…

“Postman 中文版使用教程:如何切换到中文界面?”

Postman 的很好用的接口测试软件。但是&#xff0c;Postman 默认是英文版的&#xff0c;也不支持在软件内切换为中文版。很多同学的英语并不是很好&#xff0c;看到一堆的英文很是头痛。 今天我们来介绍下&#xff1a;切换到 Postman 中文版的方法。想要学习更多的关于 Postma…

在STM32上实现无线传感器网络节点

引言 无线传感器网络&#xff08;WSN&#xff09;是物联网&#xff08;IoT&#xff09;技术的关键组成部分&#xff0c;广泛应用于环境监测、智能建筑、精密农业等领域。 本教程将介绍如何在STM32微控制器上设计和实现一个无线传感器网络节点&#xff0c;包括硬件选择、网络协…

如何提升通信芯片一次性投片成功率

通信芯片设计是一个非常复杂的系统工程&#xff0c;整体流程设计包括产品定义&#xff0c;算法开发&#xff0c;架构设计&#xff0c;电路设计和验证&#xff0c;后端版图设计&#xff0c;晶圆生产到封装测试等多个环节。在每个环节中&#xff0c;都需要严格遵循设计规则和流程…

[Unity实战]热更新如何预防过度裁剪

情景再现 假设你现在有一个游戏客户端&#xff0c;客户端只打包了入口场景&#xff0c;游戏场景都存放在了AB包。 你打的热更包里使用了协程中的waituntil修复游戏场景中空投补给资源加载时机问题&#xff0c;但是打出来的热更在真机跑报如下错误&#xff1a; TypeLoadExcep…