一.简介
块组件(ChunkComponent),与共享组件类似,但在共用组件时并不会将组件移动到新的块里面去.
当将块组件的值进行修改时.将会把组件中的值进行一个统一的变化,不会产生一个新的组件
二.例子
1.创建ChunkComponent
public struct ChunkComponent9 : IComponentData
{
public int data;
}
2.创建控制类
public class Test9 : MonoBehaviour
{
void Start()
{
EntityArchetype tempEntityArchtype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(ComponentType.ChunkComponent(typeof(ChunkComponent9)));
World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchtype);
}
}
3.测试创建多个ChunkComponent后,修改值
void Start()
{
//创建Chunk,注意这里需要用ComponentType.ChunkComponent函数包裹
EntityArchetype tempEntityArchtype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(ComponentType.ChunkComponent(typeof(ChunkComponent9)));
//创建第一个Chunk
Entity tempEntity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchtype);
//创建第二个Chunk
World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchtype);
//找到Chunk
ArchetypeChunk tempChunk = World.DefaultGameObjectInjectionWorld.EntityManager.GetChunk(tempEntity);
//修改Chunk值
World.DefaultGameObjectInjectionWorld.EntityManager.SetChunkComponentData(tempChunk, new ChunkComponent9() { data = 8 });
}
4.任意物体中挂载后查看
代码中我们只修改了一个值,但两个Chunk值都修改了
5.块组件的增删改查操作
using Unity.Entities;
using UnityEngine;
public class Test9 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//创建Chunk,注意这里需要用ComponentType.ChunkComponent函数包裹
EntityArchetype tempEntityArchtype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(ComponentType.ChunkComponent(typeof(ChunkComponent9)));
//创建第一个Chunk
Entity tempEntity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchtype);
//创建第二个Chunk
World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(tempEntityArchtype);
//找到Chunk
ArchetypeChunk tempChunk = World.DefaultGameObjectInjectionWorld.EntityManager.GetChunk(tempEntity);
//修改Chunk值
World.DefaultGameObjectInjectionWorld.EntityManager.SetChunkComponentData(tempChunk, new ChunkComponent9() { data = 8 });
//获取Chunk
World.DefaultGameObjectInjectionWorld.EntityManager.GetChunkComponentData<ChunkComponent9>(tempChunk);
//删除Chunk
World.DefaultGameObjectInjectionWorld.EntityManager.RemoveChunkComponent<ChunkComponent9>(tempEntity);
//增加Chunk
World.DefaultGameObjectInjectionWorld.EntityManager.AddChunkComponentData<ChunkComponent9>(tempEntity);
//存在判断
World.DefaultGameObjectInjectionWorld.EntityManager.HasChunkComponent<ChunkComponent9>(tempEntity);
}
}