Unity DOTS技术(六)World详解

2024-06-04 23:20
文章标签 技术 详解 unity world dots

本文主要是介绍Unity DOTS技术(六)World详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一.实体操作
        • 1.创建实体
        • 2.创建实体并挂载组件
        • 4.使用NativeArray存储实体
        • 5.查找出所有的组件
        • 6.类型条件查询
        • 7.删除单个实体
        • 8.删除组里的实体
        • 9.按查找结果删除组
  • 二.组件操作
        • 1.添加组件
        • 2.组数组批量添加组件
        • 3.创建初始化并赋值
        • 4.使用特性挂载组件
        • 5.获取实体中的组件
        • 6.修改实体上的组件值
        • 7.删除指实体上的组件
        • 8.删除组内所有实体的指定组件
        • 9.删除查询结果中的组件


下面讲解World的一些基本操作
在这里插入图片描述

一.实体操作

1.创建实体
Entity tempERntity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity();
2.创建实体并挂载组件
Entity tempERntity =World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(typeof(PrintComponentData1),typeof(RotationEulerXYZAuthoring3 ));

####3.复制实体

World.DefaultGameObjectInjectionWorld.EntityManager.Instantiate(tempErntity);
4.使用NativeArray存储实体
NativeArray类似于List,在Dots中的实体应当使用NativeArray进行储存及操作.
NativeArray<Entity> tempNativeArray = new NativeArray<Entity>(5, Allocator.Temp);
World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchetype, tempNativeArray);
5.查找出所有的组件
NativeArray<Entity> tempEntitis = World.DefaultGameObjectInjectionWorld.EntityManager.GetAllEntities();
foreach (var item in tempEntitis)
{Debug.Log(item.Index);
}
6.类型条件查询

需要注意的是查询结束后需要释放查询

EntityQuery tempEntityQuery = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(typeof(PrintComponentData1), typeof(RotationEulerXYZAuthoring3));//这里的查询条件是And的关系
NativeArray<Entity> tempEntities2 = tempEntityQuery.ToEntityArray(Allocator.TempJob);
foreach (var item in tempEntities2)
{Debug.Log(item.Index);
}
tempEntities2.Dispose();
7.删除单个实体

//创建

Entity tempErntity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(typeof(PrintComponentData1), typeof(RotationEulerXYZAuthoring3));
World.DefaultGameObjectInjectionWorld.EntityManager.Instantiate(tempErntity);
//删除
World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(tempErntity);
8.删除组里的实体
//创建组 
EntityArchetype tempEntityArchetype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(typeof(PrintComponentData1), typeof(RotationEulerXYZAuthoring3));
NativeArray<Entity> tempNativeArray = new NativeArray<Entity>(5, Allocator.Temp);
World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchetype, tempNativeArray);
//删除组
World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(teimpNativeArray);
9.按查找结果删除组
World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(tempEntityQuery);

二.组件操作

1.添加组件

方法一

World.DefaultGameObjectInjectionWorld.EntityManager.AddComponent(tempErntity1, typeof(PrintComponentData1));

方法二

World.DefaultGameObjectInjectionWorld.EntityManager.AddComponent<PrintComponentData1>(tempErntity1);
2.组数组批量添加组件

方法一

World.DefaultGameObjectInjectionWorld.EntityManager.AddComponent<PrintComponentData1>(tempNativeArray);

方法二

World.DefaultGameObjectInjectionWorld.EntityManager.AddComponent(tempNativeArray, typeof(PrintComponentData1));

方法三同时添加多个组件

World.DefaultGameObjectInjectionWorld.EntityManager.AddComponents(tempErntity1, new ComponentTypes(typeof(PrintComponentData1), typeof(RotationEulerXYZAuthoring3)));
3.创建初始化并赋值
World.DefaultGameObjectInjectionWorld.EntityManager.AddComponentData(tempErntity1, new PrintComponentData1()
{printData = 5
});
4.使用特性挂载组件

[GenerateAuthoringComponent]
在这里插入图片描述

5.获取实体中的组件
PrintComponentData1 temPrintComponentData = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<PrintComponentData1>(tempErntity1);
6.修改实体上的组件值
World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentData(tempErntity1, new PrintComponentData1() { printData = 888 }); 
7.删除指实体上的组件
World.DefaultGameObjectInjectionWorld.EntityManager.RemoveComponent<PrintComponentData1>(tempErntity1);
8.删除组内所有实体的指定组件
World.DefaultGameObjectInjectionWorld.EntityManager.RemoveComponent<PrintComponentData1>(tempNativeArray);
9.删除查询结果中的组件
World.DefaultGameObjectInjectionWorld.EntityManager.RemoveComponent<PrintComponentData1>(tempEntityQuery);

这篇关于Unity DOTS技术(六)World详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1031414

相关文章

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

详解python pycharm与cmd中制表符不一样

《详解pythonpycharm与cmd中制表符不一样》本文主要介绍了pythonpycharm与cmd中制表符不一样,这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽... 这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽度不同导致的。在PyChar

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va