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

相关文章

QT6中绘制UI的两种方法详解与示例代码

《QT6中绘制UI的两种方法详解与示例代码》Qt6提供了两种主要的UI绘制技术:​​QML(QtMeta-ObjectLanguage)​​和​​C++Widgets​​,这两种技术各有优势,适用于不... 目录一、QML 技术详解1.1 QML 简介1.2 QML 的核心概念1.3 QML 示例:简单按钮

一文详解PostgreSQL复制参数

《一文详解PostgreSQL复制参数》PostgreSQL作为一款功能强大的开源关系型数据库,其复制功能对于构建高可用性系统至关重要,本文给大家详细介绍了PostgreSQL的复制参数,需要的朋友可... 目录一、复制参数基础概念二、核心复制参数深度解析1. max_wal_seChina编程nders:WAL

Nginx路由匹配规则及优先级详解

《Nginx路由匹配规则及优先级详解》Nginx作为一个高性能的Web服务器和反向代理服务器,广泛用于负载均衡、请求转发等场景,在配置Nginx时,路由匹配规则是非常重要的概念,本文将详细介绍Ngin... 目录引言一、 Nginx的路由匹配规则概述二、 Nginx的路由匹配规则类型2.1 精确匹配(=)2

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

SQL Server中的PIVOT与UNPIVOT用法具体示例详解

《SQLServer中的PIVOT与UNPIVOT用法具体示例详解》这篇文章主要给大家介绍了关于SQLServer中的PIVOT与UNPIVOT用法的具体示例,SQLServer中PIVOT和U... 目录引言一、PIVOT:将行转换为列核心作用语法结构实战示例二、UNPIVOT:将列编程转换为行核心作用语

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

C#特性(Attributes)和反射(Reflection)详解

《C#特性(Attributes)和反射(Reflection)详解》:本文主要介绍C#特性(Attributes)和反射(Reflection),具有很好的参考价值,希望对大家有所帮助,如有错误... 目录特性特性的定义概念目的反射定义概念目的反射的主要功能包括使用反射的基本步骤特性和反射的关系总结特性

详解如何在SpringBoot控制器中处理用户数据

《详解如何在SpringBoot控制器中处理用户数据》在SpringBoot应用开发中,控制器(Controller)扮演着至关重要的角色,它负责接收用户请求、处理数据并返回响应,本文将深入浅出地讲解... 目录一、获取请求参数1.1 获取查询参数1.2 获取路径参数二、处理表单提交2.1 处理表单数据三、

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW