特殊说明:本文参照siki学院视频教程以及官方示例做的一个简单版Entitas多上下文系统使用的例子。
编写代码:例子目录如下图所示:
具体流程如下:
1.使用Entitas工具定义多个上下文,此处就是Game,Input以及Ui,具体设置参照上面的例子目录示意图中的Entitas设置部分,然后点击Generated按钮生成导出代码。
2.在Sources/Components目录下新建Components.cs文件。该文件主要用来统一管理所有的组件定义。此处只有一个简单的多上下文销毁组件定义,代码如下:
using UnityEngine;
using Entitas;
namespace MultiReactive
{
/// <summary>
/// 多上下文销毁组件
/// </summary>
[Game, Input, Ui]
public class DestoryComponent : IComponent
{
}
}
3.使用Entitas工具生成组件导出文件。此时会在Generated目录下生成一个Components/Interfaces目录以及一个实体接口。同时还发现在对应标签的目录中都生成一个Components目录以及相关组件导出文件,该文件继承自生成的实体接口。如图所示:
4.在Sources/Systems目录中定义DestorySystem.cs文件。该文件主要是使用MultiReactiveSystem来对多上文实体进行监听并进行相关逻辑的处理。由于Entitas导出的多上下文实体接口是没有继承自IEntity的,而MultiReactiveSystem必须对继承自IEntity类型的实体进行监听。所以这里我们要自己定义一个接口继承自IEntity和多上下文实体接口。此时也要将多上下文组件所在的上文文继承自该接口,从而在将该定义的接口传递给MultiReactiveSystem进行监听时能够获取到正确类型的上下文和对应上下文中定义的实体。代码如下:
using UnityEngine;
using System.Collections.Generic;
using Entitas;
namespace MultiReactive
{
public interface IDestoryEntity : IEntity, IMultiReactiveDestoryEntity {}
/// <summary>
/// 多上下文销毁系统
/// </summary>
public class DestorySystem : MultiReactiveSystem<IDestoryEntity, Contexts>
{
public DestorySystem(Contexts context) : base(context)
{
}
protected override bool Filter(IDestoryEntity entity)
{
return entity.isMultiReactiveDestory;
}
protected override ICollector[] GetTrigger(Contexts context)
{
return new ICollector[]
{
context.game.CreateCollector(GameMatcher.MultiReactiveDestory),
context.input.CreateCollector(InputMatcher.MultiReactiveDestory),
context.ui.CreateCollector(UiMatcher.MultiReactiveDestory)
};
}
protected override void Execute(List<IDestoryEntity> entities)
{
foreach (IDestoryEntity entity in entities)
{
Debug.Log("在" + entity.contextInfo.name + "上下文中销毁");
}
}
}
}
public partial class GameEntity : MultiReactive.IDestoryEntity { }
public partial class InputEntity : MultiReactive.IDestoryEntity { }
public partial class UiEntity : MultiReactive.IDestoryEntity { }
5.在Sources/Features目录中定义GameFeature.cs文件。主要对编写的System文件进行整合。代码如下:
using UnityEngine;
using Entitas;
namespace MultiReactive
{
public class GameFeature : Feature
{
public GameFeature(Contexts context)
{
Add(new DestorySystem(context));
}
}
}
6.在Sources/GameController目录中定义GameController.cs文件,该文件主要是启动Entitas的生命周期调用。代码如下:
using UnityEngine;
using System.Collections;
using Entitas;
namespace MultiReactive
{
public class GameController : MonoBehaviour
{
private Systems _systems;
// Use this for initialization
void Start()
{
Contexts context = Contexts.sharedInstance;
_systems = new Feature("Systems").Add(new GameFeature(context));
}
// Update is called once per frame
void Update()
{
_systems.Execute();
_systems.Cleanup();
}
}
}
7.在Sources/GameController目录中定义InputController.cs文件,该文件主要是通过鼠标左右键进行实体和组件的创建操作。代码如下:
using UnityEngine;
using System.Collections;
using Entitas;
public class InputController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Contexts context = Contexts.sharedInstance;
if (Input.GetMouseButtonDown(0))
{
context.game.CreateEntity().isMultiReactiveDestory = true;
}
if (Input.GetMouseButtonDown(1))
{
context.input.CreateEntity().isMultiReactiveDestory = true;
}
}
}
8.新建常见并将GameController.cs和InputController.cs文件挂载上去,运行后鼠标左右键操作后会发现多上下文系统交互成功。如图所示: