返回

ECS初试

最简单的ECS尝试

ECS最简单的概念就是Entity实体对象挂载Component组件,组件中包含纯数据,然后由System系统来驱动组件修改组件数据。

接下来,我们尝试用ECS实现一个最简单的Debug。(所有脚本都需要引用命名空间Unity.Entities

  1. 创建组件Component。

    我们需要创建一个脚本PrintComponent,继承接口IComponentData。需要注意的是该对象是个结构体,并声明了一个整形数据作为后续Log的数据。

    using Unity.Entities;
    
    public struct PrintComponent : IComponentData
    {
        public int Value;
    }
    
    
  2. 创建系统System

    创建脚本PrintSystem,继承ComponentSystem并实现方法OnUpdate;

    在方法中,我们使用全局对象Entities,遍历查找整个ECS系统中的所有PrintComponent组件,并打印其组件数据

    using Unity.Entities;
    using UnityEngine;
    
    public class PrintSystem : ComponentSystem
    {
        protected override void OnUpdate()
        {
            Entities.ForEach((ref PrintComponent printComponent) =>
            {
                Debug.Log(printComponent.Value);
            });      
        }
    }
    
    
  3. 创建实体Entity

    创建脚本PrintEntity作为将场景对象转换为ECS架构中的Entity对象的代理。需要继承接口IConvertGameObjectToEntity并实现方法Convert。在方法中我们将为对象添加PrintComponent组件,并将该对象添加到EntityManager对象管理中。

    using Unity.Entities;
    using UnityEngine;
    
    public class PrintEntity : MonoBehaviour, IConvertGameObjectToEntity
    {
        public int _Value;
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            dstManager.AddComponentData(entity, new PrintComponent() { Value = _Value }) ; 
        }
    }
    
  4. 接下来我们将PrintAuthoring组件添加到场景中的对象上,并为对象添加上ConvertToEntity组件将其对象转换为ECS下的Entity对象,启动游戏就可以在日志上看到log输出了

Entity DeBugger

当我们启动上面的Log项目后,我们会发现,原本添加脚本的物体在Hierarchy面板下消失了,且场景中该物体也无法选中。这时候要如何查看物体信息呢。

在项目运行前提下,点击菜单栏下Window->Analysis->EntityDebugger,会弹出EntityDebugger面板,在该面板中我们可以查看ECS框架下所有正在运行的对象,其中就可以找到我们的前面创建的PrintSysetm

点击All Entities可以查看当前场景中的所有正在运行的对象,最右边的Chunk Info面板划分了携带不同组件的对象。点击中间显示的物体名,即可在Inspector面板中查看该物体的详细组件信息(只读)。打开或关闭系统选项框可以开启或关闭该系统在框架中的运行。

使用系统组件

编辑对象上已有的组件

这里拿Translation举例,从上图的Chunk栏可以看到,场景中大多数物体都是有Translation组件的,这里我们直接对他进行修改。

  1. 创建脚本TransfromSystem来驱动组件,并引用命名空间Unity.Transforms

    using Unity.Entities;
    using Unity.Transforms;
    
    public class TransfromSystem : ComponentSystem
    {
        protected override void OnUpdate()
        {
            Entities.ForEach((ref Translation translation) =>
            {
                translation.Value = new Unity.Mathematics.float3(1, 1, 1);
            });
        }
    }
    
  2. 在场景中创建Cube,并添加ConvertToEntity组件,然后运行游戏,我们将发现原本位置的Cube,移动到了(1,1,1)的位置

编辑系统中已有但未添加的组件

这里拿RotationEulerXYZ举例,相比对象已添加的组件,我们只需要多一步,编写一个RotationEntity脚本给对象添加该组件即可。

using Unity.Entities;
using UnityEngine;
using Unity.Transforms;

public class RotationEntity : MonoBehaviour, IConvertGameObjectToEntity
{
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        dstManager.AddComponentData(entity, new RotationEulerXYZ());
    }
}
using Unity.Entities;
using Unity.Transforms;

public class RotationSystem : ComponentSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((ref RotationEulerXYZ rotationEulerXYZ) =>
        {
            rotationEulerXYZ.Value += new Unity.Mathematics.float3(0,0.1f,0);
        });      
    }
}

同样的,在场景中为物体添加ConvertToEntity组件,并添加上RotationEntity组件。

运行游戏后我们就能观察到物体在原地旋转。

其他

实例化ECS对象

using Unity.Entities;
using Unity.Transforms;
using UnityEngine;

public class ECSPrefabCreator : MonoBehaviour
{
    public GameObject cube;

    private void Start()
    {
        // 对象的变换设置(传入:默认世界对象)
        GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld,null);
        // 变换游戏对象的层级
        Entity tempEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(cube, settings);

        // 获取对象管理对象
        EntityManager tempEntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        Translation translation = new Translation();
        for (int i = 0; i < 20; i++)
        {
            // 实例化对象
            Entity entity = tempEntityManager.Instantiate(tempEntityPrefab);

            ////Translation为值类型,无法修改
            //Translation translation = tempEntityManager.GetComponentData<Translation>(tempEntityPrefab);
           
            translation.Value.x += 2;
            tempEntityManager.SetComponentData(entity, translation);
        }
       
    }
}
Licensed under CC BY-NC-SA 4.0
0