Unity使用ECS架构entitas实现ui响应及回放系统

来源:互联网 发布:娃娃幼儿园软件 编辑:程序博客网 时间:2024/06/07 10:52

前言

最开始听说了守望先锋在开发的时候,使用了一个ECS的框架,就非常感兴趣。
ECS是一个可以基于现有的游戏引擎开发的架构,它是Entity、Component、System的集合简称,在游戏开发中,component仅用来存储数据,多个component可组成一个entity,而system多用于操作component,真正做到了以数据驱动游戏。
说到数据驱动的好处,就不得不提守望先锋的回放系统,在游戏中可谓是相当惊艳的功能,说得通俗一点,只要存储了数据,就能在你想看的时候,再重现一遍。

Entitas

在了解一些ECS架构的特点就希望自己能使用实际操作一下,然后在网上找到了entitas架构,然后还发现实际上早在unite2015和2016上,都有过相关的演示演讲,只能说自己孤陋寡闻了。
在github的介绍,Entitas是针对c#和unity实现ECS快速架构的解决方案。这就很完美的了,不需要重复造轮子,自然省下不少时间。

Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity

github链接:https://github.com/sschmid/Entitas-CSharp

性能对比

演讲中还提到了与unity的MonoBehaviour的性能对比
这里写图片描述

HelloWorld

  • 下载entitas,并导入到unity工程中
  • 在unity中,选择Tools/Entitas/Preferences,全部设置为Everything,因为是简单demo,Contexts也只设置为Game,在前面的版本Contexts其实叫pool,所以也可以理解为对象池,所有的Entity都是从这里取的
    这里写图片描述
  • 按下Shift+ctrl+G,开始编译
  • 新建一个脚本GameController,并开始实现简单的System添加
using System.Collections;using System.Collections.Generic;using UnityEngine;using Entitas;public class GameController : MonoBehaviour {    Systems _systems;    // Use this for initialization    void Start () {        var contexts = Contexts.sharedInstance;        _systems = CreateSystems(contexts);        _systems.Initialize();    }    // Update is called once per frame    void Update () {        _systems.Execute();        _systems.Cleanup();    }    private void OnDestroy()    {        _systems.TearDown();    }     Systems CreateSystems(Contexts contexts)    {        return new Feature("Root System")            ;    }}
  • GameController绑定在场景的物体的中,运行就能看见场景的System与GameContext

回放系统

  • 准备
    • 在看了entitas在unite2016演示后,然后就打算重现一下他演示的demo。
    • 首先说一下思路,demo以计数帧来驱动游戏的,然后记录下在某一帧的重要操作,然后在实现回放系统的时候,只需要从0开始重新运算,并在记录帧更新响应的数据,就实现了回放的系统,就这么简单,一方面这只是一个demo,一方面记录数据量不大,在实际中肯定还需要做一些优化计算的,不过还是大致思路还是一样的。
    • 这里有一个技巧就是,为了方便我们操作回放系统的数据,我们可以将回放系统的相关Systems,作为一个变量存在Component中。
  • 实现一个简单的ui界面
  • 为我们所需要的数据定义Component,这时候我们只需要新建一个类继承至IComponent即可,然后为其添加相应的变量,比如当前帧,能量值,消耗能量,是否为暂停。其中标记Game表示你所对应的Contexts,Unique表示唯一Entity
using Entitas;using Entitas.CodeGeneration.Attributes;[Game,Unique]public class TickComponent:IComponent{    //当前帧    public long currentTick;}
using Entitas;using Entitas.CodeGeneration.Attributes;[Game,Unique]public class ElixirComponent:IComponent{    //能量值    public float amount;}
using Entitas;[Game]public class ConsumeElixirComponent:IComponent{    //消耗的能量值    public int amount;}
using Entitas;using Entitas.CodeGeneration.Attributes;[Game,Unique]public class PauseComponent:IComponent {}
  • 在unity中按下Shift+Ctrl+G开始编译
  • 然后编写自己的TickUpdateSystem,IInitializeSystem是初始化执行的接口,IExecuteSystem是Unity的Update或者FixedUpdate执行的接口
using Entitas;//更新帧计数public class TickUpdateSystem : IInitializeSystem, IExecuteSystem{    readonly GameContext _context;    public TickUpdateSystem(Contexts contexts)    {        _context = contexts.game;    }    public void Initialize()    {        _context.ReplaceTick(0);    }    public void Execute()    {        if (!_context.isPause)            _context.ReplaceTick(_context.tick.currentTick + 1);    }}
  • 然后将TickUpdateSystem,添加到最开始的Root System,在GameContext下就可以看着帧数的变化
using System.Collections;using System.Collections.Generic;using UnityEngine;using Entitas;public class GameController : MonoBehaviour {    Systems _systems;    // Use this for initialization    void Start () {        var contexts = Contexts.sharedInstance;        _systems = CreateSystems(contexts);        _systems.Initialize();    }    // Update is called once per frame    void Update () {        _systems.Execute();        _systems.Cleanup();    }    private void OnDestroy()    {        _systems.TearDown();    }     Systems CreateSystems(Contexts contexts)    {        return new Feature("Root System")        .Add(new TickUpdateSystem(contexts))            ;    }}
  • 这样基本上就知道Component与System的编写,我就不再详细写下每一步了,感觉太多了。
    最后还是给出demo工程,链接:https://github.com/coding2233/Entitas-Replay-Demo

演示

这里写图片描述

原创粉丝点击