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实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.