unity UIFrameWork

21 篇文章 1 订阅
17 篇文章 0 订阅

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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值