Unity之浅析 Entity Component System (ECS)

首先放出ECS官方文档

随着目前游戏对CPU性能要求的不断提升,单核高频的CPU对我们的帮助越来越有限。所以ECS(一种面向数据编程)多核心工作的方式也是大势所趋。

笔者对ECS的浅显理解就是

  • Entity 传统组件的集合,代替了GameObject的位置
  • Component 纯数据,不含有其他逻辑行为。;例如:旋转速度,缩放大小之类的
  • System 业务逻辑,根据组件的集合(Enitites)和纯数据(Components)编写对应的逻辑

下面是环境配置,笔者使用的版本为Unity 2018.2.0b8 (64-bit)

目前的ECS分为两种 Pure ECS 与 HyBridECS 区别主要就是Pure版的不能使用GameObjectsMonoBehaviours 下面的示例用的是Hybrid版本

工程文件下载

下载安装后把.Net版本换成4.6
img_1ce3fe67efff5d821f15d34df3ab3197.png
然后安装对应的Packages包,这是一个unity的新功能,以后安装插件会更容易管理
img_3c56aecdfa570bc6518635ee2f3ae060.png
拖拽出对应的Entity Debugge面板
img_2487c620e4bb1daddf9dd5e01ebaaeb0.png
在两个对应的模型上添加两个脚本 GameObjectEntity(Unity自带脚本,没有这个ECS系统是不会执行的,相当于向ECS中注册)与RotationPlanet(稍后讲解)
这是会在Debugge面板上看到两个物体对应的Entity
img_c60ee0a268385cb10e6188c87fc30252.png
接下来是业务执行脚本 RotationPlanetSystem
img_8fc4cf44e5906548483b3fbedb7c7c32.png
运行
img_8f86a389e4fda125a1e2dfaa4a6dfda5.png
img_c9937234a404e550a51faa3ae2009bba.gif
多数据操作(ComponentArray)

效果如下:

img_c6b42bd266fac9f52c04f7986caf0a37.gif

纯数据

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 纯数据
/// </summary>
public class InputComponent : MonoBehaviour
{
    public float horizontal;
    public float vertical;
}

控制所有星球的移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;

//根据指定的数值对星球进行移动控制
public class PlanetMovementSystem : ComponentSystem
{
    /// <summary>
    /// Entity 组件集合
    /// </summary>
    private struct component
    {
        public Rigidbody rigidbody;
        public InputComponent inputComponent;
    }
    /// <summary>
    /// 具体逻辑操作
    /// </summary>
    protected override void OnUpdate()
    {
        var deltaTime = Time.deltaTime;

        foreach (var entity in GetEntities<component>())//获取所有指定的Entity
        {
            var moveVector = new Vector3(entity.inputComponent.horizontal, 0, entity.inputComponent.vertical);
            var movePosition = entity.rigidbody.position + moveVector.normalized * 6 * deltaTime;

            entity.rigidbody.MovePosition(movePosition);
        }
    }
}

控制所有纯数据的数值更改

其中Inject特性在ECS features in detail中有讲解
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public class InputSystem : ComponentSystem
{
    private struct Data
    {
        public int Length;
        public ComponentArray<InputComponent> inputComponents; //数据集合
    }
    /// <summary>
    /// Inject特性在unity启动时自动赋值合适的Value
    /// </summary>
    [Inject] private Data data;
    protected override void OnUpdate()
    {
        var horizontal =  Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");
        
        for (int i = 0; i < data.inputComponents.Length; i++)
        {
            data.inputComponents[i].horizontal = horizontal;
            data.inputComponents[i].vertical = vertical;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值