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

Entities - Entity 的创建模式

1、 Unity 下创建游戏对象的过程

  • UnityEngine.GameObject是一个C#类,是Unity.Component实例的容器,并且必须有一个TransformComponent,可以组织成父子对象层级。
  • UnityEngine.Component也是一个C#类,里边有Update方法和其他方法在引擎的GameLoop时调用,并且可以被多个托管类型对象引用,包括GameObject和其他Component,Asset等
  • 托管对象无法使用Burst编译,所以除一些特殊类型外,一般来说无法在Jobs中使用,并且托管对象需要GC回收,所以创建和销毁GameObject和它们的Components开销较高。
  • 托管对象无法整体的在内存中打包,对Cache不友好

​​​​​​​

2、Unity 下创建 Entities 对象的模式

2.1 Authoring 模式创建

  • Entity SubScene时一个用于GameObject Baked的场景
  • Baking 将编辑器中的GameObject转换为写入Entity Scene的Entity也就是将Auhtoring下的数据转换为运行时数据。每次编辑EntityScene场景中的对象时都会触发Baking,并且是增量Baking.

  • Baker用来创建自定义数据,用来将自定义组件数据附加到所属的已经创建的Entity上。
  • Baking System用来处理Baker产生的数据输出,它只作用在Entity数据上,而不能作用在托管的Authoring数据上,可以使用Jobs和Burst。这个功能是可选的。

举个例子:

在 SubScene 下有一个 Cube,让他进行旋转,如果要让它旋转,先要将旋转的数据 Bake 在 Entity 对象上:

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;namespace DOTS.DOD
{struct RotateSpeedComonent : IComponentData{public float rotateSpeed;}public class RotateSpeedAuthoring : MonoBehaviour{[Range(0, 360)] public float rotationSpeed = 360f;public class Baker : Baker<RotateSpeedAuthoring>{public override void Bake(RotateSpeedAuthoring authoring){var entity = this.GetEntity(TransformUsageFlags.Dynamic);var data = new RotateSpeedComonent{rotateSpeed = math.radians(authoring.rotationSpeed)};AddComponent<RotateSpeedComonent>(entity);SetComponent<RotateSpeedComonent>(entity, data);}}}
}

然后通过一个 System 来对这个带有这个 Component 的对象进行操作

using System.Collections;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;namespace DOTS.DOD
{[BurstCompile]public partial struct CubeRotateSystem: ISystem{[BurstCompile]public void OnCreate(ref SystemState state){Debug.Log("[CubeRotateSystem] Start");}[BurstCompile]public void OnDestroy(ref SystemState state){Debug.Log("[CubeRotateSystem] OnDestroy");}[BurstCompile]public void OnUpdate(ref SystemState state){float deltaTime = SystemAPI.Time.DeltaTime;foreach (var (transform, speed) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<RotateSpeedComonent>>()){transform.ValueRW = transform.ValueRO.RotateY(speed.ValueRO.rotateSpeed * deltaTime);}}}   
}

2.2 Runtime 模式创建

  • World是Enitites集合,每个Entity在一个世界中ID是唯一的,但也可能和其他世界的EntityID碰撞,所以通常情况下通过ID查找并不是一个安全的方式。
  • EnitityManager用来管理世界的所有Entities,有CreateEntity, DestroyEntity, AddComponent, RemoveComponent, GetComponentData,SetComponentData等接
  • Entity是一个整型 Id 与 Version 数据组成的结构体,会与Unity.Entities的Component有映射,Entity之间没有父子关系,id 和 Version 完全一样才是同一个 Entity。
  • Unity.Entities.IComponentData是一个C#结构体(或者类),可以被EntitiesID索引,但无法被托管对象引用(一般情况),并且没有任何方法(通常),在内存中紧密排列存储,通过Query访问更高效
  • System包括SystemBase与ISystem,SystemBase是托管类,简单,但运行在主线程,不能被burst编译,ISystem是Struct,非托管,可以被编译,相当快,但实现起来会有一点复杂。

// 创建新world
var world = new World( "test" ); 
// 获取World中的EntityManager
var entityManager=world.EntityManager;
// 创建Entity
Entity entity=entityManager.CreateEntity();
// 为创建的entity绑定Component
entityManager.AddComponent<CubeGeneratorByScript>(entity);
// 获取Component数据副本,并设置相关数据后再设置回对应的Component
var generator = entityManager.GetComponentData<CubeGeneratorByScript>(entity);
generator.cubecount=CubeCount;
entityManager.SetComponentData(entity,cubePrototype);....// System中实例化
var generator =SystemAPl.GetSingleton<CubeGeneratorByScript>();
var cubes = CollectionHelper.CreateNativeArray<Entity>(generator.cubeCount, Allocator.Temp);
state.entityManager.Instantiate(generator.cubeEntityProtoType, cubes);
cubes.Dispose();


文章转载自:

http://2U25WAFu.zrjzc.cn
http://8FLccfEh.zrjzc.cn
http://a8lwnv07.zrjzc.cn
http://9nqwlEcX.zrjzc.cn
http://r0KkMXRM.zrjzc.cn
http://QFzzahqV.zrjzc.cn
http://F02C2eDl.zrjzc.cn
http://rqvjbaaK.zrjzc.cn
http://4dB7lkOw.zrjzc.cn
http://oukSCt4o.zrjzc.cn
http://RHduTmKG.zrjzc.cn
http://r3TGY4YK.zrjzc.cn
http://cHp2zX0C.zrjzc.cn
http://hBWfvn7W.zrjzc.cn
http://niuyyRCT.zrjzc.cn
http://ojVpy0Mz.zrjzc.cn
http://FiZp53IQ.zrjzc.cn
http://VRRf5tdw.zrjzc.cn
http://UNzg6S7v.zrjzc.cn
http://AwL4maYW.zrjzc.cn
http://4TJ4OwaC.zrjzc.cn
http://NhfVTMh4.zrjzc.cn
http://7L1Eix3N.zrjzc.cn
http://OovrGwKr.zrjzc.cn
http://9P21zTGa.zrjzc.cn
http://JBEVLzsK.zrjzc.cn
http://FMGHpbj5.zrjzc.cn
http://qylTCsLJ.zrjzc.cn
http://ZtFaVblr.zrjzc.cn
http://E8l3fiMX.zrjzc.cn
http://www.dtcms.com/a/382748.html

相关文章:

  • 用html5写王者荣耀之王者坟墓的游戏2deepseek版
  • 【Wit】pure-admin后台管理系统前端与FastAPI后端联调通信实例
  • godot+c#使用godot-sqlite连接数据库
  • 【pure-admin】pureadmin的登录对接后端
  • tcpump | 深入探索网络抓包工具
  • scikit-learn 分层聚类算法详解
  • Kafka面试精讲 Day 18:磁盘IO与网络优化
  • javaweb CSS
  • css`min()` 、`max()`、 `clamp()`
  • 超越平面交互:SLAM技术如何驱动MR迈向空间计算时代?诠视科技以算法引领变革
  • Win11桌面的word文件以及PPT文件变为白色,但是可以正常打开,如何修复
  • 【系统架构设计(31)】操作系统下:存储、设备与文件管理
  • Flask学习笔记(三)--URL构建与模板的使用
  • 基于单片机的电子抢答器设计(论文+源码)
  • TCP与UDP
  • 【WebSocket✨】入门之旅(六):WebSocket 与其他实时通信技术的对比
  • 华为防火墙隧道配置
  • 使用 Matplotlib 让排序算法动起来:可视化算法执行过程的技术详解
  • 【C++深学日志】C++编程利器:缺省参数、函数重载、引用详解
  • 晶体管:从基础原理、发展历程到前沿应用与未来趋势的深度剖析
  • CentOS7 安装 Jumpserver 3.10.15
  • jquery 文件上传 (CVE-2018-9207)漏洞复现
  • QML Charts组件之折线图的鼠标交互
  • 工程机械健康管理物联网系统:AIoT技术赋能装备全生命周期智能运维​
  • 第5课:上下文管理与状态持久化
  • SpringBootCodeGenerator使用JSqlParser解析DDL CREATE SQL 语句
  • 【WebSocket✨】入门之旅(五):WebSocket 的安全性
  • PHP使用echarts制作一个很漂亮的天气预报网站(曲线图+实况+未来一周预报)
  • 数据库造神计划第九天---增删改查(CRUD)(5)
  • 简单的折叠cell