Unity DOTS技术(六)World详解


下面讲解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);

相关推荐

  1. UnityDOTS备忘

    2024-06-06 13:04:02       26 阅读
  2. word的第课笔记

    2024-06-06 13:04:02       41 阅读
  3. 数字电子技术实验(

    2024-06-06 13:04:02       41 阅读
  4. 美赛world排版技巧

    2024-06-06 13:04:02       51 阅读
  5. NLP - word2vec详解

    2024-06-06 13:04:02       26 阅读
  6. AR技术详解

    2024-06-06 13:04:02       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 13:04:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 13:04:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 13:04:02       82 阅读
  4. Python语言-面向对象

    2024-06-06 13:04:02       91 阅读

热门阅读

  1. 深入理解Linux网络总结

    2024-06-06 13:04:02       31 阅读
  2. 政府部门在数据安全方面的责任与挑战

    2024-06-06 13:04:02       31 阅读
  3. 14.FreeRTOS 消息缓存 Message Buffer

    2024-06-06 13:04:02       28 阅读
  4. web前端三大典型应用框架

    2024-06-06 13:04:02       24 阅读
  5. 【mybatis】缓存

    2024-06-06 13:04:02       24 阅读
  6. mysql 解析json

    2024-06-06 13:04:02       30 阅读
  7. Orchestrator 记录

    2024-06-06 13:04:02       34 阅读
  8. QTransform 笔记

    2024-06-06 13:04:02       27 阅读
  9. Candence17.4安装及破解教程

    2024-06-06 13:04:02       28 阅读