# 1. AbstractArchitecture 一个项目需要创建一个类,这个类需要继承AbstractArchitecture,用于注册System、Model、Utility,所有创建的都必须在Init()方法中注册,否则无法获取 ps:目前一个项目只能有一个类继承AbstractArchitecture **示例 代码** ```csharp using UnityEngine; namespace BlueVersion2 { public class TestApp : AbstractArchitecture { protected override void Init() { // 注册 System this.RegisterSystem(new TestSystem()); this.RegisterModel(new TestModel()); } } } ``` # **2.Model、System** 建议使用方式:定义一个Model类型的接口,继承IModel,然后创建Model层类继承定义的Model类型的接口; 做法的意义:相同的数据在不同测试环境下数据处理不同,例如测试环境1数据++,测试环境2数据--。定义接口后继承只需要在TestApp类中更改注册的类即可 this.RegisterModel(new TestModel1()); this.RegisterModel(new TestModel2()); OnInit()用于初始化,例如给字段赋值等 **示例 代码** ```csharp namespace BlueVersion2 { public interface ITestModel : IModel { int TestQuery { get; } BindableProperty Count { get; } } public class TestModel : ITestModel { public int TestQuery { get; set; } public BindableProperty Count { get; } = new BindableProperty(5); public void OnInit() { TestQuery = 5; } } } ``` ``` namespace BlueVersion2 { public interface ITestSystem : ISystem { BindableProperty Count { get; } } public class TestSystem : ITestSystem { public BindableProperty Count { get; } = new BindableProperty(5); public void Init() { } } } ``` # **3.Command** Command需要继承ICommand接口 需要返回值继承ICommand接口 不限定Class、Struct 需要执行的方法写在OnExcute()中 **示例 代码** ```csharp // 定义Command using System; using System.Collections; using System.Threading.Tasks; using UnityEngine; namespace BlueVersion2 { public class AddCommand : ICommand { public void OnExcute() { this.GetModel().Count.Value++; } } public class SubCommand : ICommand { public void OnExcute() { this.GetSystem().Count.Value--; } } public class TaskACommand : ICommand> { public async Task OnExcute() { await Task.Delay(TimeSpan.FromSeconds(2.0f)); Debug.Log("等待了2秒"); return true; } } public class SimpleResultCommand : ICommand { public string OnExcute() { return "Hello Command With Result"; } } public class CoroutineCommand : ICommand { public IEnumerator OnExcute() { Debug.Log("ACoroutineCommandStart:" + Time.time); yield return new WaitForSeconds(1.0f); Debug.Log("ACoroutineCommandFinish:" + Time.time); } } } ``` ```csharp // 发送Command using System.Collections; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; namespace BlueVersion2 { public class Test : MonoBehaviour, IController { public Button btnAdd; public Button btnSub; public Button btnTaskACommand; public Button btnSimpleResultCommand; public Button btnCoroutineCommand; public Text TestText; private void Start() { SubCommand SubCommand = new SubCommand(); btnAdd.onClick.AddListener(() => { this.SendCommand(); }); btnSub.onClick.AddListener(() => { this.SendCommand(SubCommand); }); btnTaskACommand.onClick.AddListener(async () => { var result = await this.SendCommand(new TaskACommand()); Debug.Log($"已经执行TestCommand+{result}"); }); btnSimpleResultCommand.onClick.AddListener(() => { string str = this.SendCommand(new SimpleResultCommand()); Debug.LogError(str); }); btnCoroutineCommand.onClick.AddListener(() => { IEnumerator coro = this.SendCommand(new CoroutineCommand()); StartCoroutine(coro); }); } } ``` # **4.Event** Event需要继承IEvent接口,不限定Class、Struct 可以在Command中发送事件 可以在Controller层中进行监听 **示例 代码** ```csharp //定义事件 namespace BlueVersion2 { public struct TestEvent : IEvent { } } ``` ```csharp // 监听事件 using UnityEngine; namespace BlueVersion2 { public class Test : MonoBehaviour, IController { private void Start() { this.RegisterEvent(TestEventListener); // 监听事件 } private void TestEventListener(TestEvent e) { Debug.Log($"事件触发,事件名:{e.GetType()}"); } } } ``` ```csharp // 发送事件 namespace BlueVersion2 { public class SubCommand : ICommand { public void Execute() { this.SendEvent(); } } } ``` # **5.BindableProperty** ```csharp // 定义BindableProperty namespace BlueVersion2 { public interface ITestSystem : ISystem { BindableProperty Count { get; } } public class TestSystem : ITestSystem { public BindableProperty Count { get; } = new BindableProperty(5); public void Init() { } } } ``` ```csharp // 监听BindableProperty数值变化 using UnityEngine; using UnityEngine.UI; namespace BlueVersion2 { public class Test : MonoBehaviour, IController { public Text TestText; private void Start() { this.GetSystem().Count.Register((newValue) => { TestText.text = newValue.ToString(); }); } } } ``` # **6.Query** Command执行增删改操作,Query执行查操作 Query需要继承 IQuery接口 ``` // 定义Query using System.Threading.Tasks; namespace BlueVersion3 { public class TestQuery1 : IQuery { public int DoQuery() { return this.GetModel().TestQuery; } } public class TestQueryAsync1 : IQuery> { public async Task DoQuery() { await Task.Delay(2000); return 111; } } public class TestQueryAsync2 : IQuery { public string DoQuery() { return "TestQuery2"; } } } ``` ```csharp // 同步查询示例 int testQuery1 = this.SendQuery(new TestQuery1()); //使用示例1 int testQuery2 = this.SendQuery();//使用示例2 Debug.Log("TestQuery1:"+testQuery1); Debug.Log("TestQuery2:"+testQuery2); // 异步查询示例 //使用示例1 IQueryResult> task = this.SendQueryAsync>(new TestQueryAsync1()); task.OnQuerySucceed((value) => { Debug.Log("NewValue:" + value.Result); }); task.OnQueryFailed(() => { Debug.Log("失败:"); }); //使用示例2 this.SendQueryAsync(new TestQueryAsync2()).OnQuerySucceed((value) => { Debug.Log("TestQueryAsync2:"+value); }); ``` # **7.Utility** 工具层---需要继承 IUtility接口 获取跟Model、System类似 ```csharp // 定义Utility namespace BlueVersion3 { public class TestUtility : IUtility { } } ```