Unity2019 ECS getting started example: create an Entity
I. Introduction
Benpian to achieve the creation of an entity, and this Entity is displayed in the scene, with ECS框架
to explain is to create a Entity
, and this is Entity
added ComponentData
to complete the above functions.
- According to my understanding
Entity≠GameObject,而 GameObject = (Entity + Component(data) +System)
- Have certainly want to display objects in the scene
Mesh(形状)
andMaterial(材质)
so we have to do is toEntity
add theseComponent(数据)
- This case is not written
System(行为)
2. Setting up the environment
- My version of Unity: Unity2019.4.7LTS
- Use Window->PackageManage to search for Entities, click Install, remember to check
show preview packages
- Similarly install Jobs, Mathematics, Hybrid Render
- After the installation is complete, you can start coding. If there is a problem with the above steps, you can search for the corresponding problem or leave a message.
3. Coding to create an Entity and display it in the scene
In Unity, there are many ways to create or GameObject
convert into Entity
, here we mainly use the way of creation, in the newly created project, create a scriptCreateEntity.cs
CreateEntity.cs
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
public class CreateEntity : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
manager = World.DefaultGameObjectInjectionWorld.EntityManager;//获取默认世界的实体管理器
CreateEntityObj();
}
// Update is called once per frame
void Update()
{
}
private EntityManager manager;
private void CreateEntityObj()
{
//在EntityManager中提供了public Entity CreateEntity(EntityArchetype archetype);方法
//在此之前,我们需要一个
//同样在EntityManager中,有一个 CreateArchetype(params ComponentType[] types);方法
var archeType = manager.CreateArchetype
(
//以下等于规定了Entity中一系列数据类型
ComponentType.ReadWrite<LocalToWorld>(),
ComponentType.ReadWrite<Translation>(),
ComponentType.ReadOnly<RenderMesh>()
);
var entity = manager.CreateEntity(archeType);//到此我们就创建了一个entity
manager.SetName(entity, "cube");//设置一个名字方便观察
//需要为Entity设置位置,形状,材质等信息,这里的信息可以使用公有变量在检视面板中设置
//此处直接使用一个标准的数据
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//为实体设置数据
manager.SetComponentData<LocalToWorld>(entity, new LocalToWorld() { Value = Unity.Mathematics.float4x4.zero });
manager.SetComponentData<Translation>(entity, new Translation() { Value = new Unity.Mathematics.float3(0, 0, 0) });
manager.SetSharedComponentData<RenderMesh>(entity, new RenderMesh()
{
material = cube.GetComponent<MeshRenderer>().material,
mesh = cube.GetComponent<MeshFilter>().sharedMesh,
subMesh = 0,
castShadows = UnityEngine.Rendering.ShadowCastingMode.On,
receiveShadows = true,
needMotionVectorPass = true
});
Destroy(cube);//临时使用的cube进行销毁
}
}
but! After mounting and running, I did not find the one we wanted in the scene Cube
, but Entity Debugger
we can see that Entity
it does exist in the scene, which means there should be in the scene, but we can only say that we did not find it.
4. Problem solving
If Entity Debugger
you can’t see it in the scene, it means there is a problem with the data set for him.
- Create a new one in the scene
Cube
and add aConvertToEntity
script - Run and open
Entity Debugger
, check the convertedEntity
data, and find that there are a few more data, and the data types are allrender bounds
related - For
Entity
addingRenderBounds
data
after editedCreateEntity.cs
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
public class CreateEntity : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
manager = World.DefaultGameObjectInjectionWorld.EntityManager;//获取默认世界的实体管理器
CreateEntityObj();
}
// Update is called once per frame
void Update()
{
}
private EntityManager manager;
private void CreateEntityObj()
{
//在EntityManager中提供了public Entity CreateEntity(EntityArchetype archetype);方法
//在此之前,我们需要一个
//同样在EntityManager中,有一个 CreateArchetype(params ComponentType[] types);方法
var archeType = manager.CreateArchetype
(
//以下等于规定了Entity中一系列数据类型
ComponentType.ReadWrite<LocalToWorld>(),
ComponentType.ReadWrite<Translation>(),
ComponentType.ReadOnly<RenderMesh>(),
ComponentType.ReadWrite<RenderBounds>()
);
var entity = manager.CreateEntity(archeType);//到此我们就创建了一个entity
manager.SetName(entity, "cube");//设置一个名字方便观察
//需要为Entity设置位置,形状,材质等信息,这里的信息可以使用公有变量在检视面板中设置
//此处直接使用一个标准的数据
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//为实体设置数据
RenderBounds rb = new RenderBounds()
{
Value = new Unity.Mathematics.AABB()
{
Center = new Unity.Mathematics.float3(0, 0, 0),
Extents = new Unity.Mathematics.float3(0.5f,0.5f,0.5f)
}
};
manager.SetComponentData<RenderBounds>(entity,rb)//只需添加archerType即可,不需要手动设置数据
manager.SetComponentData<LocalToWorld>(entity, new LocalToWorld() { Value = Unity.Mathematics.float4x4.zero });
manager.SetComponentData<Translation>(entity, new Translation() { Value = new Unity.Mathematics.float3(0, 0, 0) });
manager.SetSharedComponentData<RenderMesh>(entity, new RenderMesh()
{
material = cube.GetComponent<MeshRenderer>().material,
mesh = cube.GetComponent<MeshFilter>().sharedMesh,
subMesh = 0,
castShadows = UnityEngine.Rendering.ShadowCastingMode.On,
receiveShadows = true,
needMotionVectorPass = true
});
Destroy(cube);//临时使用的cube进行销毁
}
}
Welcome to my personal blog Snail's Blog