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;
        }
    }
}
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/666173

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。