打开流程 /// OnInit --> OnPost -_uniframework">
当前位置:   article > 正文

unity UIFrameWork_uniframework

uniframework

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 退出
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

面板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();}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

封装的调用接口的管理类:

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();
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

使用实例:
登录面板Ctr

public class LoginPanelCtrl : UICtrlBase
{
	protected override string Key { get { return "LoginPanelCtrl"; } }
	protected override string Path { get { return "Assets/_ABs/UIPrefabs/LoginPanel.prefab"; } }
}
  • 1
  • 2
  • 3
  • 4
  • 5

登录面板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();

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

登录界面打开合关闭:

UICtrlManager.OpenBaseUI(new LoginPanelCtrl(),()=> { });
UICtrlManager.CloseTopBaseUI();
  • 1
  • 2
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号