Unity3D:ECS架构详解

前言

Unity3D是目前游戏开发领域最为流行的引擎之一,而Unity3DECS架构则是在Unity3D引擎的基础上,针对游戏开发中的实体组件系统进行优化和改进而设计的一种架构。本文将详细介绍Unity3DECS架构的技术细节和代码实现。

对惹,这里有一个游戏开发交流小组,希望大家可以点击进来一起交流一下开发经验呀!

一、Unity3DECS架构概述

Unity3DECS架构是基于实体组件系统(Entity-Component-System,ECS)设计的一种游戏开发架构。ECS是一种游戏开发中常用的设计模式,它将游戏对象(Entity)拆分成多个组件(Component),并通过系统(System)对组件进行管理和处理。与传统的面向对象设计模式相比,ECS更加灵活、高效和易于扩展。

Unity3DECS架构中,游戏对象(Entity)被称为实体(Entity),组件(Component)被称为组件(Component),系统(System)被称为系统(System)。实体(Entity)只是一个标识符,不包含任何数据或行为,而组件(Component)则包含实体的数据和行为,系统(System)则负责对组件进行管理和处理。

二、Unity3DECS架构技术详解

1.实体(Entity)

实体(Entity)是Unity3DECS架构中最基本的概念,它只是一个标识符,用于标识游戏对象。在Unity3DECS架构中,实体不包含任何数据或行为,它只是一个唯一的标识符,用于让系统(System)知道哪些组件(Component)属于哪个实体。

在Unity3DECS架构中,实体(Entity)通常使用整数或长整数作为标识符,例如:

public struct Entity
{
public int id;
}

2.组件(Component)

组件(Component)是Unity3DECS架构中实现游戏对象功能的基本单元,它包含了游戏对象的数据和行为。在Unity3DECS架构中,组件(Component)是可复用的,可以在多个实体(Entity)之间共享。

在Unity3DECS架构中,组件(Component)通常使用结构体或类实现,例如:

public struct Position : IComponentData
{
public float x;
public float y;
public float z;
}
public class Movement : MonoBehaviour, IConvertGameObjectToEntity
{
public float speed;
public void Convert(Entity entity, EntityManager manager, GameObjectConversionSystem conversionSystem)
{
    manager.AddComponentData(entity, new MovementData { speed = speed });
}
}

3.系统(System)

系统(System)是Unity3DECS架构中管理和处理组件(Component)的核心部分,它负责对组件进行管理和处理。系统(System)可以对所有拥有特定组件(Component)的实体(Entity)进行操作,例如更新组件(Component)的状态、处理组件之间的关系等。

在Unity3DECS架构中,系统(System)通常使用结构体或类实现,例如:

public class MovementSystem : SystemBase
{
protected override void OnUpdate()
{
float deltaTime = Time.DeltaTime;
Entities.ForEach((ref Translation translation, in MovementData movement) =>
    {
        translation.Value += deltaTime * movement.speed * Vector3.forward;
    }).Schedule();
}
}

4.实体管理器(EntityManager)

实体管理器(EntityManager)是Unity3DECS架构中管理实体(Entity)的核心部分,它负责创建、销毁、查询和管理实体(Entity)及其组件(Component)。

在Unity3DECS架构中,实体管理器(EntityManager)通常使用EntityManager类实现,例如:

public class GameManager : MonoBehaviour
{
private EntityManager entityManager;
private void Awake()
{
    entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
}

private void Start()
{
    Entity entity = entityManager.CreateEntity(typeof(Position), typeof(Movement));
    entityManager.SetComponentData(entity, new Position { x = 0, y = 0, z = 0 });
    entityManager.SetComponentData(entity, new Movement { speed = 5 });
}
}

三、Unity3DECS架构代码实现

下面是一个简单的示例代码,演示了如何使用Unity3DECS架构实现一个移动的方块:

1.创建一个空对象,并将其命名为“GameManager”。

2.在“GameManager”对象上添加一个“GameManager”脚本,并将其代码设置为下面的内容:

using Unity.Entities;
using Unity.Transforms;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Mathematics;
using UnityEngine;
public class GameManager : MonoBehaviour
{
private EntityManager entityManager;
private PhysicsWorld physicsWorld;
private BuildPhysicsWorld buildPhysicsWorld;
private void Awake()
{
    entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    physicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<PhysicsWorld>();
    buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
}

private void Start()
{
    Entity entity = entityManager.CreateEntity(typeof(Translation), typeof(Rotation), typeof(PhysicsVelocity), typeof(PhysicsMass));
    entityManager.SetComponentData(entity, new Translation { Value = new float3(0, 0, 0) });
    entityManager.SetComponentData(entity, new Rotation { Value = quaternion.identity });
    entityManager.SetComponentData(entity, new PhysicsVelocity { Linear = new float3(0, 0, 5) });
    entityManager.SetComponentData(entity, new PhysicsMass { InverseMass = 1 });

    buildPhysicsWorld.Enabled = true;
}

private void OnDestroy()
{
    buildPhysicsWorld.Enabled = false;
}
}

3.在“Hierarchy”视图中创建一个立方体对象,并将其命名为“Cube”。

4.在“Cube”对象上添加一个“Physics Shape”组件,并将其设置为“Box”。

5.在“Cube”对象上添加一个“Physics Body”组件,并将其设置为“Dynamic”。

6.在“Cube”对象上添加一个“Physics Gravity Factor”组件,并将其设置为“0”。

7.在“Cube”对象上添加一个“Physics Material”组件,并将其设置为“Default”.

8.在“Cube”对象上添加一个“Convert To Entity”组件,并将其设置为“GameManager”。

9.在场景视图中运行游戏,可以看到一个移动的方块。

通过上面的示例代码,可以看出Unity3DECS架构的使用方法和优点。Unity3DECS架构可以让开发者更加灵活和高效地管理和处理游戏对象,从而提高游戏开发的效率和质量。

附:视频教学

两小时搞懂ECS架构​www.bycwedu.com/promotion_channels/1232986610​编辑

猜你喜欢

转载自blog.csdn.net/Thomas_YXQ/article/details/134456011