当前位置: 首页 > news >正文

App Instance 架构示例

前言

        在Unity程序设计过程中,我们处理的第一个对象是Application Instance。

        它的主要职责是启动流程管理、卸载流程管理,次要职责是管理在内部的子系统生命周期。其他职责,提供或桥接应用程序的配置信息、及其他第三方接口。

        它通常以单例的形式存在在场景中,即使在切换场景时,也不会被删除。

设计需求        
  1. 可以自定义异步启动流程、或卸载流程
  2. App 自动完成子系统的初始化和卸载工作
  3. 子系统是可轻松扩展的
  4. 通过App Instance可以轻松访问到子系统实例
UML结构
UML类结构示例
代码示例
//> XApplication.Utils.cs
//> Create by UniMarknamespace XF 
{public interface IApplication {void Startup();void Initialize();void Deinitialize();void Shutdown();}public interface IApplicationSubSystem {int InitializeSequence { get; }void Initialize(XApplication application);void Deinitialize();}
}
//> XApplication.cs
//> Create by UniMarkusing System;
using System.Collections.Generic;
using UnityEngine;namespace XF
{public sealed class XApplication : MonoBehaviour{private IApplication Application;private IApplicationSubSystem[] ApplicationSubSystems = new IApplicationSubSystem[0];private Dictionary<Type, IApplicationSubSystem> QuickReferenceTable;private void Awake (){Debug.Log($"启动 [XApplication] 实例 ({gameObject.name})");if ( ( Application = GetComponent<IApplication>() ) == null ){throw new Exception("你需要实现IApplication的接口");}ApplicationSubSystems = GetAllApplicationSubSystemWithSequence();QuickReferenceTable = GetQuickReferenceTableWithAppSubSystem(ApplicationSubSystems);Application.Startup();}private void Start (){if ( ApplicationSubSystems.Length > 0 ){Debug.Log("实例化应用内联系统 ...");for ( int index = 0; index < ApplicationSubSystems.Length; index++ ){var subSystem = ApplicationSubSystems[index];var time = Time.time;subSystem.Initialize(this);Debug.Log($"释放: {subSystem.GetType().Name} 耗时 {( Time.time - time ).ToString("{0.000}")}s");}}if ( Application != null ){Debug.Log("实例化应用 ...");Application?.Initialize();}}private void OnDestroy (){if ( Application != null ){Debug.Log("释放应用 ...");Application?.Deinitialize();}if ( ApplicationSubSystems.Length > 0 ){Debug.Log("释放应用内联系统 ..."); for ( int index = ApplicationSubSystems.Length - 1; index >= 0; index-- ){var subSystem = ApplicationSubSystems[index];var time = Time.time;subSystem.Deinitialize();Debug.Log($"释放: {subSystem.GetType().Name} 耗时 {(Time.time - time).ToString("{0.000}")}s");}}Debug.Log($"关闭 [XApplication] 实例 ({gameObject.name})");Application.Shutdown();Application = null;ApplicationSubSystems = null;QuickReferenceTable = null;}private IApplicationSubSystem[] GetAllApplicationSubSystemWithSequence (){var list = new List<IApplicationSubSystem>();GetComponentsInChildren(true, list);list.Sort(( a, b ) => a.InitializeSequence.CompareTo(b.InitializeSequence));return list.ToArray();}private Dictionary<Type, IApplicationSubSystem> GetQuickReferenceTableWithAppSubSystem ( IApplicationSubSystem[] systems ){Dictionary<Type, IApplicationSubSystem> result = new Dictionary<Type, IApplicationSubSystem>();foreach ( var system in systems ){if ( false == result.TryAdd(system.GetType(), system) ){throw new Exception($"包含相同类型的子系统 {system.GetType().Name}");}}return result;}#region APIpublic T GetSubSystem<T> () where T : MonoBehaviour, IApplicationSubSystem{if ( QuickReferenceTable.TryGetValue(typeof(T), out var system) ){return system as T;}throw new Exception($"不存在的子系统 {typeof(T).Name}");}#endregion}
}
//> XGame.cs
//> Create by UniMarkusing System;
using UnityEngine;
using XF;public abstract class XGame<T> : MonoBehaviour, IApplicationwhere T : XGame<T>
{public static T Instance{get; private set;}public XApplication Owner{get; private set;}//> 定义你自己的系统在这里    public TestSubSystem TestSystem { get; private set; }void IApplication.Startup (){if ( Instance != null ){throw new Exception($"[{gameObject.name}]{this.GetType().Name}类存在多实例异常!");}Instance = this as T;GameObject.DontDestroyOnLoad(gameObject);Owner = GetComponent<XApplication>();//> 初始化你的系统在这里TestSystem = Owner.GetSubSystem<TestSubSystem>();}void IApplication.Shutdown (){Instance = null;Owner = null;//> 释放你的系统在这里TestSystem = null;}void IApplication.Initialize () => StartGame();void IApplication.Deinitialize () => QuitGame();protected abstract void StartGame ();protected abstract void QuitGame ();
}
//> GameIns.cs
//> Create by UniMarkusing UnityEngine;
using XF;public class GameIns : XGame<GameIns>
{#region 生命周期protected override async void StartGame (){//> 加载配置文件 //> await 配置文件加载完毕//> 做个测试var testSystem =  Owner.GetSubSystem<TestSubSystem> ();Debug.Log(testSystem.GetMessage());//> 进入第一个场景}protected override void QuitGame (){//> 在这里卸载业务载入的资源和数据}#endregion
}
//> TestSubSystem.cs
//> Create by UniMarkusing UnityEngine;namespace XF
{#region 非正式代码public class TestSubSystem : MonoBehaviour, IApplicationSubSystem{[SerializeField]private int InitializeSequence;int IApplicationSubSystem.InitializeSequence => InitializeSequence;private XApplication Owner;void IApplicationSubSystem.Initialize ( XApplication application ){Owner = application;}void IApplicationSubSystem.Deinitialize (){Owner = null;}public string GetMessage (){return "Hello Here!";}}#endregion
}
代码组织结构

-| Scripts

        --| Core

                ---| SubSystems

                        ---- TestSubSystem.cs

                --- XApplication.cs

                --- XApplication.Utils.cs

                --- XGame.cs

        -- GameIns.cs

创建应用程序预制体
预制体组织结构
XApplication 节点
TestSubSystem 节点
下载代码示例 

 下载demo

http://www.lryc.cn/news/406760.html

相关文章:

  • 【论文速读】| MoRSE:利用检索增强生成技术填补网络安全专业知识的空白
  • pip install albumentations安装下载超级细水管
  • 驱动开发系列07 - 驱动程序如何分配内存
  • 【Jackson】注解及其使用
  • LeetCode24 两两交换链表中的节点
  • AI OS
  • Dubbo 黑白名单机制详解
  • 配电房智能巡检机器人怎么选?
  • husky引发git commit报错的解决方案
  • 韩顺平0基础学Java——第37天
  • Layer2区块链扩容方案(1)——总述
  • AWS监控工具,监控性能指标
  • 义务外贸wordpress独立站主题
  • 初等数论精解【4】
  • MongoDB教程(二十二):MongoDB固定集合
  • 20240724----idea的Java环境卸载与安装
  • C语言 ——— 函数指针数组的讲解及其用法
  • 鸿蒙仓颉语言【cryptocj 库】(介绍与SHA、MD5、HMAC摘要算法)
  • 设计App的后端接口分类以及环境依赖包详情
  • 接入百度文心一言API教程
  • classfinal太强了,再也不怕被反编译了
  • DP-适配器模式代码重新理解
  • minio安装小计一则
  • # Redis 入门到精通(九)-- 主从复制(1)
  • SpringBoot整合ElasticSearch的elasticsearch-java出现jakarta.json.spi.JsonProvider报错问题
  • git 操作汇总【迭代更新中】
  • 看 Unity 组件的源码 —— ILSpy
  • Axivion Suite 7.8现已发布
  • 从零开始!JupyterNotebook的安装教程
  • 使用kali进行端口扫描