Unity2019 ECS entry instance: create an Entity

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 Entityadded ComponentDatato complete the above functions.

  1. According to my understandingEntity≠GameObject,而 GameObject = (Entity + Component(data) +System)
  2. Have certainly want to display objects in the scene Mesh(形状)and Material(材质)so we have to do is to Entityadd theseComponent(数据)
  3. This case is not writtenSystem(行为)

2. Setting up the environment

  1. My version of Unity: Unity2019.4.7LTS
  2. Use Window->PackageManage to search for Entities, click Install, remember to check show preview packages
  3. Similarly install Jobs, Mathematics, Hybrid Render
  4. 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 GameObjectconvert 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 Debuggerwe can see that Entityit 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 Debuggeryou can’t see it in the scene, it means there is a problem with the data set for him.

  1. Create a new one in the scene Cubeand add a ConvertToEntityscript
  2. Run and open Entity Debugger, check the converted Entitydata, and find that there are a few more data, and the data types are all render boundsrelated
  3. For Entityadding RenderBoundsdata

The first run, Entity information
After adding Renderbounds

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

Guess you like

Origin blog.csdn.net/weixin_36382492/article/details/109266676