https://www.bilibili.com/video/av36225838
EC:把实体需要的数据资料合并,且最简化 S:把所有的实体一起处理。
总结:
1.EC有两种表现形式,第一种是"EntityArchetype"原型形式,代码往里加component数据。第二种是"prefab"形式,引用一个外部的prefab,prefab上面挂各种各样的component数据。
可以通过继承IComponent接口,自定义component数据。
2.System的结构是固定形式,而且通用于各个场景(只要工程目录里有这种文件就会自动运行)。首先,声明struct结构体,里面放着要获得的数据类型。然后这个结构体对应着一个[Inject]标签的结构体对象。系统会自动获取拥有这种数据的实体的数据,存放到上述对象里。最后,在OnUpdate()中统一处理这些数据。
image.png
1.EntityArchetype:原型,形成了一个连贯的内存区间,直达速度快
using Unity.Entities;
using Unity.Rendering;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Unity.Collections;
public class SpawnOneBlock : MonoBehaviour
{
public static EntityArchetype BlockArchetype; //原型
[Header("Mesh Info")]
public Mesh blockMesh;
[Header("Nature Block Type")]
public Material blockMaterial;
public EntityManager manager;
public Entity entities;
public GameObject Prefab_ref;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Initialize()
{
EntityManager manager = World.Active.GetOrCreateManager<EntityManager>();
//创建原型
BlockArchetype = manager.CreateArchetype(
typeof(Position)
);
}
void Start()
{
manager = World.Active.GetOrCreateManager<EntityManager>();
//根据原型创建实体
Entity entities = manager.CreateEntity(BlockArchetype);
manager.SetComponentData(entities, new Position { Value = new int3 ( 2, 0, 0 ) });
manager.AddComponentData(entities, new BlockTag { }); //自定义结构体
manager.AddSharedComponentData(entities, new MeshInstanceRenderer
{
mesh = blockMesh,
material = blockMaterial
});
if(Prefab_ref) //预制体不为空的时候,执行一下操作
{
//只有一个Entity的Array
NativeArray<Entity> entityArray = new NativeArray<Entity>(1, Allocator.Temp);
manager.Instantiate(Prefab_ref, entityArray);
manager.SetComponentData(entityArray[0], new Position { Value = new float3(4, 0f, 0f) });
entityArray.Dispose();
}
}
// Update is called once per frame
void Update()
{
}
}
//自定义组件数据结构
public struct BlockTag : IComponentData
{
}
2.实体组件预制体
image.png
image.png
3.代码稍加修改
image.png
if(Prefab_ref) //预制体不为空的时候,执行一下操作
{
//只有一个Entity的Array
NativeArray<Entity> entityArray = new NativeArray<Entity>(10, Allocator.Temp);
manager.Instantiate(Prefab_ref, entityArray);
int i = 2;
foreach(var x in entityArray)
{
manager.SetComponentData(x, new Position { Value = new float3(i, 0f, 0f) });
i += 2;
}
entityArray.Dispose();
}
3.照抄MineCraft
image.png
此处高能!
关于材质的GpuInstance,勾选了之后帧率从3.5提高到了30。
=====================================================================
以上都是EC,entity component.静态摆放
Ecs是批量操作
image.png
===========================System===========================
Struct来获得系统中,拥有这种区块结构的所有数据
并自动注入到这个看似“未实例化”的对象中
最后在OnUpdate中,对这些数据进行操作