MVC 设计架构:
C基类的创建:
using UnityEngine;
public class UICtrlBase
{
protected virtual string Key { get { return "UICtrlBase"; } }//C 的key 数值
protected virtual string Path { get { return ""; } }//加载的资源路径
/// <summary> 打开流程
/// OnInit --> OnPost --> ResLoad --> OnForward
/// </summary>
/// <summary> 关闭流程
/// OnDispose --> UnLoadRes
/// </summary>
///
private object data_;//C层持有的数据
private ResLoader loader;//资源加载者
private UIBase uiBase;//面板
public virtual void OnPost(System.Action<bool> pCb)//异步逻辑的处理(例如网络通信层的处理)返回true 界面加载成功 返回false 界面打开失败
{
pCb.Invoke(true);
}
public void OnSetData(Object pData)//基本数据的设置
{
data_ = pData;
}
public virtual void OnInit(){ }//初始化
public void ResLoad(System.Action<UnityEngine.GameObject> pCB)//资源加载
{
loader = ResLoadManager.Load(Path,(oGO)=> {
pCB.Invoke(oGO as GameObject);
if (oGO != null)
{
var go = oGO as GameObject;
uiBase = go.GetComponent<UIBase>();
}
});
}
public void UnLoadRes()//资源卸载
{
loader.UnLoadAsset();
}
public void OnForward()//界面切到前台
{
uiBase.OnForward();
}
public virtual void OnDispose(){}//c 退出
}
C 类打开面板执行者
using System.Collections.Generic;
using UnityEngine;
public class UICtrlPolicy
{
public GameObject SubRoot;
public int BaseLayer;
private List<UICtrlBase> ctrls = new List<UICtrlBase>();
public UICtrlPolicy(GameObject pSubRoot,int pBaseLayer)
{
SubRoot = pSubRoot;
BaseLayer = pBaseLayer;
}
public void Open(UICtrlBase pCtrl,System.Action pFinishCB, OpenData pOpenData)
{
Flow flow = new Flow(1);
flow.AddStep(1, (pOnFinish) =>{
pCtrl.OnInit();
pOnFinish.Invoke(true);
});
flow.AddStep(2, (pOnFinish) => {
pCtrl.OnPost(pOnFinish);
});
flow.AddStep(3, (pOnFinish) => {
pCtrl.ResLoad((oGO)=> {
if (oGO != null){
oGO.transform.SetParent(SubRoot.transform);
oGO.transform.localPosition = Vector3.zero;
pOnFinish.Invoke(true);
}
else{
pOnFinish.Invoke(false);
}
});
});
flow.AddStep(4, (pOnFinish) => {
pCtrl.OnForward();
pOnFinish.Invoke(true);
});
flow.RunAllStep(()=> {
ctrls.Add(pCtrl);
pFinishCB.Invoke();
});
}
public void CloseTop()
{
if (ctrls.Count > 0)
{
UICtrlBase ctrl = ctrls[ctrls.Count - 1];
Flow flow = new Flow(2);
flow.AddStep(1, (pOnFinish) => {
ctrl.OnDispose();
pOnFinish.Invoke(true);
});
flow.AddStep(2, (pOnFinish) => {
ctrl.UnLoadRes();
pOnFinish.Invoke(true);
});
flow.RunAllStep(() => {
ctrls.Remove(ctrl);
});
}
}
}
public enum OpenType
{
None = 0,
DisablePre = 1,
DestoryPre = 2,
}
public class OpenData
{
public OpenType OpenType = OpenType.None;
}
面板Prefab 的UIBase基类
using UnityEngine;
public class UIBase : MonoBehaviour {
private bool isInit = false;
private void Awake(){Init();}
private void Init(){
if (!isInit){
isInit = true;
OnInit();
}
}
public virtual void OnInit(){ }//面板的初始化
public void DoForward(){OnForward();}
public virtual void OnForward(){ } //面板被切到最上层显示
public virtual void DoDestroy(){ }//面板被销毁
private void OnDestroy(){DoDestroy();}
}
封装的调用接口的管理类:
using UnityEngine;
public class UICtrlManager
{
public static UICtrlPolicy BasePolicy = new UICtrlPolicy(GameObject.Find("Canvas"),100);
public static void OpenBaseUI(UICtrlBase ctrl,System.Action pCB)
{
BasePolicy.Open(ctrl, pCB, new OpenData());
}
public static void CloseTopBaseUI()
{
BasePolicy.CloseTop();
}
}
使用实例:
登录面板Ctr
public class LoginPanelCtrl : UICtrlBase
{
protected override string Key { get { return "LoginPanelCtrl"; } }
protected override string Path { get { return "Assets/_ABs/UIPrefabs/LoginPanel.prefab"; } }
}
登录面板UIPrefab
using UnityEngine.UI;
public class LoginPanel : UIBase
{
public Button ABtn;
public override void OnInit()
{
ABtn.onClick.AddListener(()=> {
UICtrlManager.OpenBaseUI(new MainPanelCtrl(), () => { });
});
}
public override void OnForward(){
}
public override void DoDestroy()
{
ABtn.onClick.RemoveAllListeners();
}
}
登录界面打开合关闭:
UICtrlManager.OpenBaseUI(new LoginPanelCtrl(),()=> { });
UICtrlManager.CloseTopBaseUI();