文章目录
前言
This article is mainly combined with the previous article for further studyUIManagerVarious methods and calls in .
UIManager中的方法
1.打开界面
According to the source code of the article, we can know,在UIManagerOpening an interface is mainly through this method
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;}
对于其他的OpenUIFormA method is an encapsulation of further parameters of the method.
True for the previous part of the methodUIManagerThe judgment of the internal field and the passed parameters,to prevent this method from being called before initialization.
After this we can see the first method call UIGroup uiGroup = (UIGroup)GetUIGroup(uiGroupName);
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;}
mention it here,GF中对UI是以”组”来管理的,Each interface is managed by a certain group,初次打开UIwhenUnityend to set the new oneUIGroup.
在UIGroupsClick the plus sign to create your own group.
回到GetUIGroup()方法中,The method is to get DictionaryManage groups for specific needs within the group,如果没有则返回Null.
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;
得到UIGroup后,如果为null,Next will bethrow Exception,If it is not empty, the corresponding entity will be obtained from the entity pool first(The entity pool saves all entity objects after the resource is loaded,If the corresponding entity does not exist in the pool, the resource will be loaded asynchronously through the resource loading module).
If the entity does not exist, it will exist firstm_UIFormsBeingLoadedAdd the object to the dictionary,This dictionary holds everything that is being loadedUI界面资源,After that, the module will be loaded through the resourceLoad该资源;Passes if the entity existsInternalOpenUIFormmethod to open that internallyUI,and eventually return thatUISerial number of the interface.
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;}}
InternalOpenUIForm()方法中会通过m_UIFormHelperthis interface for further loadingUI,The concrete implementation of this interface is in Unity端,The second part will explain in detail,Here only need to know through the interfaceCreateUIForm()方法会生成UIgame object.
Return the obtained after the generation is completeUIForm,Afterwards it isUIForm的进一步处理,Similar to most other simpleUIModule pair generationUI的处理方法,The success event is called after processing is complete,这里用了ReferencePoolis to cache the success,在catchOn error, a similar failure call event action is performed as on success.
2.在UnityOpen the interface inside
在Unity端对UIThe processing of the module is through the concrete realization of the interface and the integrationUIManager中的方法在UIComponentpackaged again,make it callable
protected override void Awake(){base.Awake();m_UIManager = GameFrameworkEntry.GetModule();if (m_UIManager == null){Log.Fatal("UI manager is invalid.");return;}if (m_EnableOpenUIFormSuccessEvent){m_UIManager.OpenUIFormSuccess += OnOpenUIFormSuccess;}m_UIManager.OpenUIFormFailure += OnOpenUIFormFailure;if (m_EnableOpenUIFormUpdateEvent){m_UIManager.OpenUIFormUpdate += OnOpenUIFormUpdate;}if (m_EnableOpenUIFormDependencyAssetEvent){m_UIManager.OpenUIFormDependencyAsset += OnOpenUIFormDependencyAsset;}if (m_EnableCloseUIFormCompleteEvent){m_UIManager.CloseUIFormComplete += OnCloseUIFormComplete;}}private void Start(){BaseComponent baseComponent = GameEntry.GetComponent();if (baseComponent == null){Log.Fatal("Base component is invalid.");return;}m_EventComponent = GameEntry.GetComponent();if (m_EventComponent == null){Log.Fatal("Event component is invalid.");return;}if (baseComponent.EditorResourceMode){m_UIManager.SetResourceManager(baseComponent.EditorResourceHelper);}else{m_UIManager.SetResourceManager(GameFrameworkEntry.GetModule());}m_UIManager.SetObjectPoolManager(GameFrameworkEntry.GetModule());m_UIManager.InstanceAutoReleaseInterval = m_InstanceAutoReleaseInterval;m_UIManager.InstanceCapacity = m_InstanceCapacity;m_UIManager.InstanceExpireTime = m_InstanceExpireTime;m_UIManager.InstancePriority = m_InstancePriority;UIFormHelperBase uiFormHelper = Helper.CreateHelper(m_UIFormHelperTypeName, m_CustomUIFormHelper);if (uiFormHelper == null){Log.Error("Can not create UI form helper.");return;}uiFormHelper.name = "UI Form Helper";Transform transform = uiFormHelper.transform;transform.SetParent(this.transform);transform.localScale = Vector3.one;m_UIManager.SetUIFormHelper(uiFormHelper);if (m_InstanceRoot == null){m_InstanceRoot = new GameObject("UI Form Instances").transform;m_InstanceRoot.SetParent(gameObject.transform);m_InstanceRoot.localScale = Vector3.one;}m_InstanceRoot.gameObject.layer = LayerMask.NameToLayer("UI");for (int i = 0; i < m_UIGroups.Length; i++){if (!AddUIGroup(m_UIGroups[i].Name, m_UIGroups[i].Depth)){Log.Warning("Add UI group '{0}' failure.", m_UIGroups[i].Name);continue;}}}
public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, bool pauseCoveredUIForm, object userData){return m_UIManager.OpenUIForm(uiFormAssetName, uiGroupName, priority, pauseCoveredUIForm, userData);}
在Awake与Start方法中,对UIManagerVarious events in the add callback,并进行对UIManagerThe field properties in the assignment are assigned,to achieve with thisUnitythe purpose of communicating
public class DefaultUIFormHelper : UIFormHelperBase{private ResourceComponent m_ResourceComponent = null;public override object InstantiateUIForm(object uiFormAsset){return Instantiate((Object)uiFormAsset);}public override IUIForm CreateUIForm(object uiFormInstance, IUIGroup uiGroup, object userData){GameObject gameObject = uiFormInstance as GameObject;if (gameObject == null){Log.Error("UI form instance is invalid.");return null;}Transform transform = gameObject.transform;transform.SetParent(((MonoBehaviour)uiGroup.Helper).transform);transform.localScale = Vector3.one;return gameObject.GetOrAddComponent();}public override void ReleaseUIForm(object uiFormAsset, object uiFormInstance){m_ResourceComponent.UnloadAsset(uiFormAsset);Destroy((Object)uiFormInstance);}private void Start(){m_ResourceComponent = GameEntry.GetComponent();if (m_ResourceComponent == null){Log.Fatal("Resource component is invalid.");return;}}}
在上文中我们提到IUIFormHelper接口,Here is the concrete implementation of the interface,可见生成UIObject time and normalInstantiateThe same operation as the object,在创建UIFormobject firstUI资源转为GameObjectAfter setting the position of the object and the parent class last added or obtainedUIForm组件,and return that component.
在GF中对UIObjects are placed in lifecycle methodsUILogic组件中,UIFormJust have a reference to it.
public sealed class UIForm : MonoBehaviour, IUIForm{private int m_SerialId;private string m_UIFormAssetName;private IUIGroup m_UIGroup;private int m_DepthInUIGroup;private bool m_PauseCoveredUIForm;private UIFormLogic m_UIFormLogic;................}
在OnInit()method will start withGetComponent方法来得到UILogic组件,The later lifecycle methods are correctUILogic方法的调用,以打开为例
public void OnOpen(object userData){try{m_UIFormLogic.OnOpen(userData);}catch (Exception exception){Log.Error("UI form '[{0}]{1}' OnOpen with exception '{2}'.", m_SerialId.ToString(), m_UIFormAssetName, exception.ToString());}}
因此对UIThe method of opening and closing etc. is to require the developer to further writeUILogicmethod in the component.
最后对GF中UISummarize the logic of the call
在第一次使用UIWhen the module is first inUnity端Inspector面板的UIComponentgroup in the component to add a new group,after that through the componentOpenUIForm()方法进行对UIManager对于方法的调用,UIManagerAfter that, the corresponding group will be obtained first,Add new ones to the groupUIForm,,在生成新UIFormWhen the resource loading module is called to load it,并对其进行缓存,之后在UIManager内部Open方法中对UIFormLifecycle method to call,即对UIForm中UILogicThe method in the component makes further calls.