北京做網(wǎng)站好的關(guān)鍵詞熱度分析
前言
? ? ? ? 在Unity程序設(shè)計過程中,我們處理的第一個對象是Application Instance。
? ? ? ? 它的主要職責(zé)是啟動流程管理、卸載流程管理,次要職責(zé)是管理在內(nèi)部的子系統(tǒng)生命周期。其他職責(zé),提供或橋接應(yīng)用程序的配置信息、及其他第三方接口。
? ? ? ? 它通常以單例的形式存在在場景中,即使在切換場景時,也不會被刪除。
設(shè)計需求? ? ? ??
- 可以自定義異步啟動流程、或卸載流程
- App 自動完成子系統(tǒng)的初始化和卸載工作
- 子系統(tǒng)是可輕松擴展的
- 通過App Instance可以輕松訪問到子系統(tǒng)實例
UML結(jié)構(gòu)

代碼示例
//> 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("你需要實現(xiàn)IApplication的接口");}ApplicationSubSystems = GetAllApplicationSubSystemWithSequence();QuickReferenceTable = GetQuickReferenceTableWithAppSubSystem(ApplicationSubSystems);Application.Startup();}private void Start (){if ( ApplicationSubSystems.Length > 0 ){Debug.Log("實例化應(yīng)用內(nèi)聯(lián)系統(tǒng) ...");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("實例化應(yīng)用 ...");Application?.Initialize();}}private void OnDestroy (){if ( Application != null ){Debug.Log("釋放應(yīng)用 ...");Application?.Deinitialize();}if ( ApplicationSubSystems.Length > 0 ){Debug.Log("釋放應(yīng)用內(nèi)聯(lián)系統(tǒng) ..."); 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($"關(guān)閉 [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($"包含相同類型的子系統(tǒng) {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($"不存在的子系統(tǒng) {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;}//> 定義你自己的系統(tǒng)在這里 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>();//> 初始化你的系統(tǒng)在這里TestSystem = Owner.GetSubSystem<TestSubSystem>();}void IApplication.Shutdown (){Instance = null;Owner = null;//> 釋放你的系統(tǒng)在這里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 (){//> 在這里卸載業(yè)務(wù)載入的資源和數(shù)據(jù)}#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
}
代碼組織結(jié)構(gòu)
-| Scripts
? ? ? ? --|?Core
? ? ? ? ? ? ? ? ---| SubSystems
? ? ? ? ? ? ? ? ? ? ? ? ---- TestSubSystem.cs
? ? ? ? ? ? ? ? --- XApplication.cs
? ? ? ? ? ? ? ? --- XApplication.Utils.cs
? ? ? ? ? ? ? ? --- XGame.cs
? ? ? ? -- GameIns.cs
創(chuàng)建應(yīng)用程序預(yù)制體



下載代碼示例?
?下載demo