123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using GameFramework.ObjectPool;
- using GameFramework.Resource;
- using System;
- using System.Collections.Generic;
- namespace GameFramework.UI
- {
- /// <summary>
- /// 界面管理器。
- /// </summary>
- internal sealed partial class UIManager : GameFrameworkModule, IUIManager
- {
- private readonly Dictionary<string, UIGroup> m_UIGroups;
- private readonly Dictionary<int, string> m_UIFormsBeingLoaded;
- private readonly HashSet<int> m_UIFormsToReleaseOnLoad;
- private readonly Queue<IUIForm> m_RecycleQueue;
- private readonly LoadAssetCallbacks m_LoadAssetCallbacks;
- private IObjectPoolManager m_ObjectPoolManager;
- private IResourceManager m_ResourceManager;
- private IObjectPool<UIFormInstanceObject> m_InstancePool;
- private IUIFormHelper m_UIFormHelper;
- private int m_Serial;
- private bool m_IsShutdown;
- private EventHandler<OpenUIFormSuccessEventArgs> m_OpenUIFormSuccessEventHandler;
- private EventHandler<OpenUIFormFailureEventArgs> m_OpenUIFormFailureEventHandler;
- private EventHandler<OpenUIFormUpdateEventArgs> m_OpenUIFormUpdateEventHandler;
- private EventHandler<OpenUIFormDependencyAssetEventArgs> m_OpenUIFormDependencyAssetEventHandler;
- private EventHandler<CloseUIFormCompleteEventArgs> m_CloseUIFormCompleteEventHandler;
- /// <summary>
- /// 初始化界面管理器的新实例。
- /// </summary>
- public UIManager()
- {
- m_UIGroups = new Dictionary<string, UIGroup>(StringComparer.Ordinal);
- m_UIFormsBeingLoaded = new Dictionary<int, string>();
- m_UIFormsToReleaseOnLoad = new HashSet<int>();
- m_RecycleQueue = new Queue<IUIForm>();
- m_LoadAssetCallbacks = new LoadAssetCallbacks(LoadAssetSuccessCallback, LoadAssetFailureCallback, LoadAssetUpdateCallback, LoadAssetDependencyAssetCallback);
- m_ObjectPoolManager = null;
- m_ResourceManager = null;
- m_InstancePool = null;
- m_UIFormHelper = null;
- m_Serial = 0;
- m_IsShutdown = false;
- m_OpenUIFormSuccessEventHandler = null;
- m_OpenUIFormFailureEventHandler = null;
- m_OpenUIFormUpdateEventHandler = null;
- m_OpenUIFormDependencyAssetEventHandler = null;
- m_CloseUIFormCompleteEventHandler = null;
- }
- /// <summary>
- /// 获取界面组数量。
- /// </summary>
- public int UIGroupCount
- {
- get
- {
- return m_UIGroups.Count;
- }
- }
- /// <summary>
- /// 获取或设置界面实例对象池自动释放可释放对象的间隔秒数。
- /// </summary>
- public float InstanceAutoReleaseInterval
- {
- get
- {
- return m_InstancePool.AutoReleaseInterval;
- }
- set
- {
- m_InstancePool.AutoReleaseInterval = value;
- }
- }
- /// <summary>
- /// 获取或设置界面实例对象池的容量。
- /// </summary>
- public int InstanceCapacity
- {
- get
- {
- return m_InstancePool.Capacity;
- }
- set
- {
- m_InstancePool.Capacity = value;
- }
- }
- /// <summary>
- /// 获取或设置界面实例对象池对象过期秒数。
- /// </summary>
- public float InstanceExpireTime
- {
- get
- {
- return m_InstancePool.ExpireTime;
- }
- set
- {
- m_InstancePool.ExpireTime = value;
- }
- }
- /// <summary>
- /// 获取或设置界面实例对象池的优先级。
- /// </summary>
- public int InstancePriority
- {
- get
- {
- return m_InstancePool.Priority;
- }
- set
- {
- m_InstancePool.Priority = value;
- }
- }
- /// <summary>
- /// 打开界面成功事件。
- /// </summary>
- public event EventHandler<OpenUIFormSuccessEventArgs> OpenUIFormSuccess
- {
- add
- {
- m_OpenUIFormSuccessEventHandler += value;
- }
- remove
- {
- m_OpenUIFormSuccessEventHandler -= value;
- }
- }
- /// <summary>
- /// 打开界面失败事件。
- /// </summary>
- public event EventHandler<OpenUIFormFailureEventArgs> OpenUIFormFailure
- {
- add
- {
- m_OpenUIFormFailureEventHandler += value;
- }
- remove
- {
- m_OpenUIFormFailureEventHandler -= value;
- }
- }
- /// <summary>
- /// 打开界面更新事件。
- /// </summary>
- public event EventHandler<OpenUIFormUpdateEventArgs> OpenUIFormUpdate
- {
- add
- {
- m_OpenUIFormUpdateEventHandler += value;
- }
- remove
- {
- m_OpenUIFormUpdateEventHandler -= value;
- }
- }
- /// <summary>
- /// 打开界面时加载依赖资源事件。
- /// </summary>
- public event EventHandler<OpenUIFormDependencyAssetEventArgs> OpenUIFormDependencyAsset
- {
- add
- {
- m_OpenUIFormDependencyAssetEventHandler += value;
- }
- remove
- {
- m_OpenUIFormDependencyAssetEventHandler -= value;
- }
- }
- /// <summary>
- /// 关闭界面完成事件。
- /// </summary>
- public event EventHandler<CloseUIFormCompleteEventArgs> CloseUIFormComplete
- {
- add
- {
- m_CloseUIFormCompleteEventHandler += value;
- }
- remove
- {
- m_CloseUIFormCompleteEventHandler -= value;
- }
- }
- /// <summary>
- /// 界面管理器轮询。
- /// </summary>
- /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
- /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
- internal override void Update(float elapseSeconds, float realElapseSeconds)
- {
- while (m_RecycleQueue.Count > 0)
- {
- IUIForm uiForm = m_RecycleQueue.Dequeue();
- uiForm.OnRecycle();
- m_InstancePool.Unspawn(uiForm.Handle);
- }
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- uiGroup.Value.Update(elapseSeconds, realElapseSeconds);
- }
- }
- /// <summary>
- /// 关闭并清理界面管理器。
- /// </summary>
- internal override void Shutdown()
- {
- m_IsShutdown = true;
- CloseAllLoadedUIForms();
- m_UIGroups.Clear();
- m_UIFormsBeingLoaded.Clear();
- m_UIFormsToReleaseOnLoad.Clear();
- m_RecycleQueue.Clear();
- }
- /// <summary>
- /// 设置对象池管理器。
- /// </summary>
- /// <param name="objectPoolManager">对象池管理器。</param>
- public void SetObjectPoolManager(IObjectPoolManager objectPoolManager)
- {
- if (objectPoolManager == null)
- {
- throw new GameFrameworkException("Object pool manager is invalid.");
- }
- m_ObjectPoolManager = objectPoolManager;
- m_InstancePool = m_ObjectPoolManager.CreateSingleSpawnObjectPool<UIFormInstanceObject>("UI Instance Pool");
- }
- /// <summary>
- /// 设置资源管理器。
- /// </summary>
- /// <param name="resourceManager">资源管理器。</param>
- public void SetResourceManager(IResourceManager resourceManager)
- {
- if (resourceManager == null)
- {
- throw new GameFrameworkException("Resource manager is invalid.");
- }
- m_ResourceManager = resourceManager;
- }
- /// <summary>
- /// 设置界面辅助器。
- /// </summary>
- /// <param name="uiFormHelper">界面辅助器。</param>
- public void SetUIFormHelper(IUIFormHelper uiFormHelper)
- {
- if (uiFormHelper == null)
- {
- throw new GameFrameworkException("UI form helper is invalid.");
- }
- m_UIFormHelper = uiFormHelper;
- }
- /// <summary>
- /// 是否存在界面组。
- /// </summary>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <returns>是否存在界面组。</returns>
- public bool HasUIGroup(string uiGroupName)
- {
- if (string.IsNullOrEmpty(uiGroupName))
- {
- throw new GameFrameworkException("UI group name is invalid.");
- }
- return m_UIGroups.ContainsKey(uiGroupName);
- }
- /// <summary>
- /// 获取界面组。
- /// </summary>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <returns>要获取的界面组。</returns>
- public IUIGroup GetUIGroup(string uiGroupName)
- {
- if (string.IsNullOrEmpty(uiGroupName))
- {
- throw new GameFrameworkException("UI group name is invalid.");
- }
- UIGroup uiGroup = null;
- if (m_UIGroups.TryGetValue(uiGroupName, out uiGroup))
- {
- return uiGroup;
- }
- return null;
- }
- /// <summary>
- /// 获取所有界面组。
- /// </summary>
- /// <returns>所有界面组。</returns>
- public IUIGroup[] GetAllUIGroups()
- {
- int index = 0;
- IUIGroup[] results = new IUIGroup[m_UIGroups.Count];
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- results[index++] = uiGroup.Value;
- }
- return results;
- }
- /// <summary>
- /// 获取所有界面组。
- /// </summary>
- /// <param name="results">所有界面组。</param>
- public void GetAllUIGroups(List<IUIGroup> results)
- {
- if (results == null)
- {
- throw new GameFrameworkException("Results is invalid.");
- }
- results.Clear();
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- results.Add(uiGroup.Value);
- }
- }
- /// <summary>
- /// 增加界面组。
- /// </summary>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="uiGroupHelper">界面组辅助器。</param>
- /// <returns>是否增加界面组成功。</returns>
- public bool AddUIGroup(string uiGroupName, IUIGroupHelper uiGroupHelper)
- {
- return AddUIGroup(uiGroupName, 0, uiGroupHelper);
- }
- /// <summary>
- /// 增加界面组。
- /// </summary>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="uiGroupDepth">界面组深度。</param>
- /// <param name="uiGroupHelper">界面组辅助器。</param>
- /// <returns>是否增加界面组成功。</returns>
- public bool AddUIGroup(string uiGroupName, int uiGroupDepth, IUIGroupHelper uiGroupHelper)
- {
- if (string.IsNullOrEmpty(uiGroupName))
- {
- throw new GameFrameworkException("UI group name is invalid.");
- }
- if (uiGroupHelper == null)
- {
- throw new GameFrameworkException("UI group helper is invalid.");
- }
- if (HasUIGroup(uiGroupName))
- {
- return false;
- }
- m_UIGroups.Add(uiGroupName, new UIGroup(uiGroupName, uiGroupDepth, uiGroupHelper));
- return true;
- }
- /// <summary>
- /// 是否存在界面。
- /// </summary>
- /// <param name="serialId">界面序列编号。</param>
- /// <returns>是否存在界面。</returns>
- public bool HasUIForm(int serialId)
- {
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- if (uiGroup.Value.HasUIForm(serialId))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 是否存在界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <returns>是否存在界面。</returns>
- public bool HasUIForm(string uiFormAssetName)
- {
- if (string.IsNullOrEmpty(uiFormAssetName))
- {
- throw new GameFrameworkException("UI form asset name is invalid.");
- }
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- if (uiGroup.Value.HasUIForm(uiFormAssetName))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 获取界面。
- /// </summary>
- /// <param name="serialId">界面序列编号。</param>
- /// <returns>要获取的界面。</returns>
- public IUIForm GetUIForm(int serialId)
- {
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- IUIForm uiForm = uiGroup.Value.GetUIForm(serialId);
- if (uiForm != null)
- {
- return uiForm;
- }
- }
- return null;
- }
- /// <summary>
- /// 获取界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <returns>要获取的界面。</returns>
- public IUIForm GetUIForm(string uiFormAssetName)
- {
- if (string.IsNullOrEmpty(uiFormAssetName))
- {
- throw new GameFrameworkException("UI form asset name is invalid.");
- }
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- IUIForm uiForm = uiGroup.Value.GetUIForm(uiFormAssetName);
- if (uiForm != null)
- {
- return uiForm;
- }
- }
- return null;
- }
- /// <summary>
- /// 获取界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <returns>要获取的界面。</returns>
- public IUIForm[] GetUIForms(string uiFormAssetName)
- {
- if (string.IsNullOrEmpty(uiFormAssetName))
- {
- throw new GameFrameworkException("UI form asset name is invalid.");
- }
- List<IUIForm> results = new List<IUIForm>();
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- results.AddRange(uiGroup.Value.GetUIForms(uiFormAssetName));
- }
- return results.ToArray();
- }
- /// <summary>
- /// 获取界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="results">要获取的界面。</param>
- public void GetUIForms(string uiFormAssetName, List<IUIForm> results)
- {
- if (string.IsNullOrEmpty(uiFormAssetName))
- {
- throw new GameFrameworkException("UI form asset name is invalid.");
- }
- if (results == null)
- {
- throw new GameFrameworkException("Results is invalid.");
- }
- results.Clear();
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- uiGroup.Value.InternalGetUIForms(uiFormAssetName, results);
- }
- }
- /// <summary>
- /// 获取所有已加载的界面。
- /// </summary>
- /// <returns>所有已加载的界面。</returns>
- public IUIForm[] GetAllLoadedUIForms()
- {
- List<IUIForm> results = new List<IUIForm>();
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- results.AddRange(uiGroup.Value.GetAllUIForms());
- }
- return results.ToArray();
- }
- /// <summary>
- /// 获取所有已加载的界面。
- /// </summary>
- /// <param name="results">所有已加载的界面。</param>
- public void GetAllLoadedUIForms(List<IUIForm> results)
- {
- if (results == null)
- {
- throw new GameFrameworkException("Results is invalid.");
- }
- results.Clear();
- foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
- {
- uiGroup.Value.InternalGetAllUIForms(results);
- }
- }
- /// <summary>
- /// 获取所有正在加载界面的序列编号。
- /// </summary>
- /// <returns>所有正在加载界面的序列编号。</returns>
- public int[] GetAllLoadingUIFormSerialIds()
- {
- int index = 0;
- int[] results = new int[m_UIFormsBeingLoaded.Count];
- foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
- {
- results[index++] = uiFormBeingLoaded.Key;
- }
- return results;
- }
- /// <summary>
- /// 获取所有正在加载界面的序列编号。
- /// </summary>
- /// <param name="results">所有正在加载界面的序列编号。</param>
- public void GetAllLoadingUIFormSerialIds(List<int> results)
- {
- if (results == null)
- {
- throw new GameFrameworkException("Results is invalid.");
- }
- results.Clear();
- foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
- {
- results.Add(uiFormBeingLoaded.Key);
- }
- }
- /// <summary>
- /// 是否正在加载界面。
- /// </summary>
- /// <param name="serialId">界面序列编号。</param>
- /// <returns>是否正在加载界面。</returns>
- public bool IsLoadingUIForm(int serialId)
- {
- return m_UIFormsBeingLoaded.ContainsKey(serialId);
- }
- /// <summary>
- /// 是否正在加载界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <returns>是否正在加载界面。</returns>
- public bool IsLoadingUIForm(string uiFormAssetName)
- {
- if (string.IsNullOrEmpty(uiFormAssetName))
- {
- throw new GameFrameworkException("UI form asset name is invalid.");
- }
- return m_UIFormsBeingLoaded.ContainsValue(uiFormAssetName);
- }
- /// <summary>
- /// 是否是合法的界面。
- /// </summary>
- /// <param name="uiForm">界面。</param>
- /// <returns>界面是否合法。</returns>
- public bool IsValidUIForm(IUIForm uiForm)
- {
- if (uiForm == null)
- {
- return false;
- }
- return HasUIForm(uiForm.SerialId);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, false, null);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="priority">加载界面资源的优先级。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, priority, false, null);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, bool pauseCoveredUIForm)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, pauseCoveredUIForm, null);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="userData">用户自定义数据。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, object userData)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, false, userData);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="priority">加载界面资源的优先级。</param>
- /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, bool pauseCoveredUIForm)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, priority, pauseCoveredUIForm, null);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="priority">加载界面资源的优先级。</param>
- /// <param name="userData">用户自定义数据。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, object userData)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, priority, false, userData);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
- /// <param name="userData">用户自定义数据。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, bool pauseCoveredUIForm, object userData)
- {
- return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, pauseCoveredUIForm, userData);
- }
- /// <summary>
- /// 打开界面。
- /// </summary>
- /// <param name="uiFormAssetName">界面资源名称。</param>
- /// <param name="uiGroupName">界面组名称。</param>
- /// <param name="priority">加载界面资源的优先级。</param>
- /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
- /// <param name="userData">用户自定义数据。</param>
- /// <returns>界面的序列编号。</returns>
- public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, bool pauseCoveredUIForm, object userData)
- {
- if (m_ResourceManager == null)
- {
- throw new GameFrameworkException("You must set resource manager first.");
- }
- if (m_UIFormHelper == null)
- {
- throw new GameFrameworkException("You must set UI form helper first.");
- }
- if (string.IsNullOrEmpty(uiFormAssetName))
- {
- throw new GameFrameworkException("UI form asset name is invalid.");
- }
- if (string.IsNullOrEmpty(uiGroupName))
- {
- throw new GameFrameworkException("UI group name is invalid.");
- }
- UIGroup uiGroup = (UIGroup)GetUIGroup(uiGroupName);
- if (uiGroup == null)
- {
- throw new GameFrameworkException(Utility.Text.Format("UI group '{0}' is not exist.", uiGroupName));
- }
- int serialId = ++m_Serial;
- UIFormInstanceObject uiFormInstanceObject = m_InstancePool.Spawn(uiFormAssetName);
- if (uiFormInstanceObject == null)
- {
- m_UIFormsBeingLoaded.Add(serialId, uiFormAssetName);
- m_ResourceManager.LoadAsset(uiFormAssetName, priority, m_LoadAssetCallbacks, OpenUIFormInfo.Create(serialId, uiGroup, pauseCoveredUIForm, userData));
- }
- else
- {
- InternalOpenUIForm(serialId, uiFormAssetName, uiGroup, uiFormInstanceObject.Target, pauseCoveredUIForm, false, 0f, userData);
- }
- return serialId;
- }
- /// <summary>
- /// 关闭界面。
- /// </summary>
- /// <param name="serialId">要关闭界面的序列编号。</param>
- public void CloseUIForm(int serialId)
- {
- CloseUIForm(serialId, null);
- }
- /// <summary>
- /// 关闭界面。
- /// </summary>
- /// <param name="serialId">要关闭界面的序列编号。</param>
- /// <param name="userData">用户自定义数据。</param>
- public void CloseUIForm(int serialId, object userData)
- {
- if (IsLoadingUIForm(serialId))
- {
- m_UIFormsToReleaseOnLoad.Add(serialId);
- m_UIFormsBeingLoaded.Remove(serialId);
- return;
- }
- IUIForm uiForm = GetUIForm(serialId);
- if (uiForm == null)
- {
- throw new GameFrameworkException(Utility.Text.Format("Can not find UI form '{0}'.", serialId));
- }
- CloseUIForm(uiForm, userData);
- }
- /// <summary>
- /// 关闭界面。
- /// </summary>
- /// <param name="uiForm">要关闭的界面。</param>
- public void CloseUIForm(IUIForm uiForm)
- {
- CloseUIForm(uiForm, null);
- }
- /// <summary>
- /// 关闭界面。
- /// </summary>
- /// <param name="uiForm">要关闭的界面。</param>
- /// <param name="userData">用户自定义数据。</param>
- public void CloseUIForm(IUIForm uiForm, object userData)
- {
- if (uiForm == null)
- {
- throw new GameFrameworkException("UI form is invalid.");
- }
- UIGroup uiGroup = (UIGroup)uiForm.UIGroup;
- if (uiGroup == null)
- {
- throw new GameFrameworkException("UI group is invalid.");
- }
- uiGroup.RemoveUIForm(uiForm);
- uiForm.OnClose(m_IsShutdown, userData);
- uiGroup.Refresh();
- if (m_CloseUIFormCompleteEventHandler != null)
- {
- CloseUIFormCompleteEventArgs closeUIFormCompleteEventArgs = CloseUIFormCompleteEventArgs.Create(uiForm.SerialId, uiForm.UIFormAssetName, uiGroup, userData);
- m_CloseUIFormCompleteEventHandler(this, closeUIFormCompleteEventArgs);
- ReferencePool.Release(closeUIFormCompleteEventArgs);
- }
- m_RecycleQueue.Enqueue(uiForm);
- }
- /// <summary>
- /// 关闭所有已加载的界面。
- /// </summary>
- public void CloseAllLoadedUIForms()
- {
- CloseAllLoadedUIForms(null);
- }
- /// <summary>
- /// 关闭所有已加载的界面。
- /// </summary>
- /// <param name="userData">用户自定义数据。</param>
- public void CloseAllLoadedUIForms(object userData)
- {
- IUIForm[] uiForms = GetAllLoadedUIForms();
- foreach (IUIForm uiForm in uiForms)
- {
- if (!HasUIForm(uiForm.SerialId))
- {
- continue;
- }
- CloseUIForm(uiForm, userData);
- }
- }
- /// <summary>
- /// 关闭所有正在加载的界面。
- /// </summary>
- public void CloseAllLoadingUIForms()
- {
- foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
- {
- m_UIFormsToReleaseOnLoad.Add(uiFormBeingLoaded.Key);
- }
- m_UIFormsBeingLoaded.Clear();
- }
- /// <summary>
- /// 激活界面。
- /// </summary>
- /// <param name="uiForm">要激活的界面。</param>
- public void RefocusUIForm(IUIForm uiForm)
- {
- RefocusUIForm(uiForm, null);
- }
- /// <summary>
- /// 激活界面。
- /// </summary>
- /// <param name="uiForm">要激活的界面。</param>
- /// <param name="userData">用户自定义数据。</param>
- public void RefocusUIForm(IUIForm uiForm, object userData)
- {
- if (uiForm == null)
- {
- throw new GameFrameworkException("UI form is invalid.");
- }
- UIGroup uiGroup = (UIGroup)uiForm.UIGroup;
- if (uiGroup == null)
- {
- throw new GameFrameworkException("UI group is invalid.");
- }
- uiGroup.RefocusUIForm(uiForm, userData);
- uiGroup.Refresh();
- uiForm.OnRefocus(userData);
- }
- /// <summary>
- /// 设置界面实例是否被加锁。
- /// </summary>
- /// <param name="uiFormInstance">要设置是否被加锁的界面实例。</param>
- /// <param name="locked">界面实例是否被加锁。</param>
- public void SetUIFormInstanceLocked(object uiFormInstance, bool locked)
- {
- if (uiFormInstance == null)
- {
- throw new GameFrameworkException("UI form instance is invalid.");
- }
- m_InstancePool.SetLocked(uiFormInstance, locked);
- }
- /// <summary>
- /// 设置界面实例的优先级。
- /// </summary>
- /// <param name="uiFormInstance">要设置优先级的界面实例。</param>
- /// <param name="priority">界面实例优先级。</param>
- public void SetUIFormInstancePriority(object uiFormInstance, int priority)
- {
- if (uiFormInstance == null)
- {
- throw new GameFrameworkException("UI form instance is invalid.");
- }
- m_InstancePool.SetPriority(uiFormInstance, priority);
- }
- private void InternalOpenUIForm(int serialId, string uiFormAssetName, UIGroup uiGroup, object uiFormInstance, bool pauseCoveredUIForm, bool isNewInstance, float duration, object userData)
- {
- try
- {
- IUIForm uiForm = m_UIFormHelper.CreateUIForm(uiFormInstance, uiGroup, userData);
- if (uiForm == null)
- {
- throw new GameFrameworkException("Can not create UI form in UI form helper.");
- }
- uiForm.OnInit(serialId, uiFormAssetName, uiGroup, pauseCoveredUIForm, isNewInstance, userData);
- uiGroup.AddUIForm(uiForm);
- uiForm.OnOpen(userData);
- uiGroup.Refresh();
- if (m_OpenUIFormSuccessEventHandler != null)
- {
- OpenUIFormSuccessEventArgs openUIFormSuccessEventArgs = OpenUIFormSuccessEventArgs.Create(uiForm, duration, userData);
- m_OpenUIFormSuccessEventHandler(this, openUIFormSuccessEventArgs);
- ReferencePool.Release(openUIFormSuccessEventArgs);
- }
- }
- catch (Exception exception)
- {
- if (m_OpenUIFormFailureEventHandler != null)
- {
- OpenUIFormFailureEventArgs openUIFormFailureEventArgs = OpenUIFormFailureEventArgs.Create(serialId, uiFormAssetName, uiGroup.Name, pauseCoveredUIForm, exception.ToString(), userData);
- m_OpenUIFormFailureEventHandler(this, openUIFormFailureEventArgs);
- ReferencePool.Release(openUIFormFailureEventArgs);
- return;
- }
- throw;
- }
- }
- private void LoadAssetSuccessCallback(string uiFormAssetName, object uiFormAsset, float duration, object userData)
- {
- OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
- if (openUIFormInfo == null)
- {
- throw new GameFrameworkException("Open UI form info is invalid.");
- }
- if (m_UIFormsToReleaseOnLoad.Contains(openUIFormInfo.SerialId))
- {
- m_UIFormsToReleaseOnLoad.Remove(openUIFormInfo.SerialId);
- ReferencePool.Release(openUIFormInfo);
- m_UIFormHelper.ReleaseUIForm(uiFormAsset, null);
- return;
- }
- m_UIFormsBeingLoaded.Remove(openUIFormInfo.SerialId);
- UIFormInstanceObject uiFormInstanceObject = UIFormInstanceObject.Create(uiFormAssetName, uiFormAsset, m_UIFormHelper.InstantiateUIForm(uiFormAsset), m_UIFormHelper);
- m_InstancePool.Register(uiFormInstanceObject, true);
- InternalOpenUIForm(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup, uiFormInstanceObject.Target, openUIFormInfo.PauseCoveredUIForm, true, duration, openUIFormInfo.UserData);
- ReferencePool.Release(openUIFormInfo);
- }
- private void LoadAssetFailureCallback(string uiFormAssetName, LoadResourceStatus status, string errorMessage, object userData)
- {
- OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
- if (openUIFormInfo == null)
- {
- throw new GameFrameworkException("Open UI form info is invalid.");
- }
- if (m_UIFormsToReleaseOnLoad.Contains(openUIFormInfo.SerialId))
- {
- m_UIFormsToReleaseOnLoad.Remove(openUIFormInfo.SerialId);
- return;
- }
- m_UIFormsBeingLoaded.Remove(openUIFormInfo.SerialId);
- string appendErrorMessage = Utility.Text.Format("Load UI form failure, asset name '{0}', status '{1}', error message '{2}'.", uiFormAssetName, status, errorMessage);
- if (m_OpenUIFormFailureEventHandler != null)
- {
- OpenUIFormFailureEventArgs openUIFormFailureEventArgs = OpenUIFormFailureEventArgs.Create(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup.Name, openUIFormInfo.PauseCoveredUIForm, appendErrorMessage, openUIFormInfo.UserData);
- m_OpenUIFormFailureEventHandler(this, openUIFormFailureEventArgs);
- ReferencePool.Release(openUIFormFailureEventArgs);
- return;
- }
- throw new GameFrameworkException(appendErrorMessage);
- }
- private void LoadAssetUpdateCallback(string uiFormAssetName, float progress, object userData)
- {
- OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
- if (openUIFormInfo == null)
- {
- throw new GameFrameworkException("Open UI form info is invalid.");
- }
- if (m_OpenUIFormUpdateEventHandler != null)
- {
- OpenUIFormUpdateEventArgs openUIFormUpdateEventArgs = OpenUIFormUpdateEventArgs.Create(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup.Name, openUIFormInfo.PauseCoveredUIForm, progress, openUIFormInfo.UserData);
- m_OpenUIFormUpdateEventHandler(this, openUIFormUpdateEventArgs);
- ReferencePool.Release(openUIFormUpdateEventArgs);
- }
- }
- private void LoadAssetDependencyAssetCallback(string uiFormAssetName, string dependencyAssetName, int loadedCount, int totalCount, object userData)
- {
- OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
- if (openUIFormInfo == null)
- {
- throw new GameFrameworkException("Open UI form info is invalid.");
- }
- if (m_OpenUIFormDependencyAssetEventHandler != null)
- {
- OpenUIFormDependencyAssetEventArgs openUIFormDependencyAssetEventArgs = OpenUIFormDependencyAssetEventArgs.Create(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup.Name, openUIFormInfo.PauseCoveredUIForm, dependencyAssetName, loadedCount, totalCount, openUIFormInfo.UserData);
- m_OpenUIFormDependencyAssetEventHandler(this, openUIFormDependencyAssetEventArgs);
- ReferencePool.Release(openUIFormDependencyAssetEventArgs);
- }
- }
- }
- }
|