玩客云做网站,海口网站建设美丽,WordPress外链自动转内链,织梦网站模板更换文章目录 一.简介二.例子 一.简介
在之前的学习中我们发现Entity不能挂载相同的组件的. 当我们需要用相同的组件时则可以使用.IBufferElementData接口 动态缓冲区组件来实现 二.例子
1.创建IBufferElementData组件
using Unity.Entities;
using UnityEngine;
//[GenerateAu… 文章目录 一.简介二.例子 一.简介
在之前的学习中我们发现Entity不能挂载相同的组件的. 当我们需要用相同的组件时则可以使用.IBufferElementData接口 动态缓冲区组件来实现 二.例子
1.创建IBufferElementData组件
using Unity.Entities;
using UnityEngine;
//[GenerateAuthoringComponent]
public struct BufferComponent8 : IBufferElementData
{public int dataA;public int dataB;
}2.创建管理组件
using Unity.Entities;
using UnityEngine;
public class Authoring8 : MonoBehaviour, IConvertGameObjectToEntity
{public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem){DynamicBufferBufferComponent8 tempBuffer dstManager.AddBufferBufferComponent8(entity);tempBuffer.Add(new BufferComponent8() { dataA 1, dataB 100 });tempBuffer.Add(new BufferComponent8() { dataA 2, dataB 101 });tempBuffer.Add(new BufferComponent8() { dataA 3, dataB 102 });}
}3.在场景中管理组件
4.运行后看到多个相同组件挂到了同一个实体
5.查询组件值
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
public class Test8 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){EntityQuery tempEntityQuery World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(typeof(BufferComponent8));NativeArrayEntity tempEntites1 tempEntityQuery.ToEntityArray(Allocator.TempJob);DynamicBufferBufferComponent8 tempBuffer World.DefaultGameObjectInjectionWorld.EntityManager.GetBufferBufferComponent8(tempEntites1[0]);Debug.Log(tempBuffer[0].dataA);tempEntites1.Dispose();}
}6.编译组件
foreach (var item in tempBuffer)
{Debug.Log(item.dataA);
}7.插入组件
tempBuffer.Insert(0, new BufferComponent8() { dataA 999, dataB 878 });