UI框架

第一步

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UIFramework
{
public class BasePanel : MonoBehaviour
{
//进入页面
public virtual void OnEnter() { }
//暂停页面交互
public virtual void OnPause() { }
//恢复页面交互
public virtual void OnResume() { }
//退出页面
public virtual void OnExit() { }
}
}

第二步,创建界面类型和路径

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UIFramework
{
public class UIPanelType
{
public static string MainUIPanel = “MainUIPanel”;
public static string ShopPanel = “ShopPanel”;
public static string BagPanel = “BagPanel”;
public static string SettingPanel = “SettingPanel”;
public static string SkillPanel = “SkillPanel”;
public static string PopPanel = “PopPanel”;
}
public class UIPanelPath
{
public static string MainUIPanelPath = “UIPanel/MainUIPanel”;
public static string ShopPanelPath = “UIPanel/ShopPanel”;
public static string BagPanelPath = “UIPanel/BagPanel”;
public static string SettingPanelPath = “UIPanel/SettingPanel”;
public static string SkillPanelPath = “UIPanel/SkillPanel”;
public static string PopPanelPath = “UIPanel/PopPanel”;
}
}

第三步 最重要的一步

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UIFramework
{
public class UIManager
{
//单例模式
private static UIManager instance;
public static UIManager Inatance
{
get
{
if (instance == null)
{
instance = new UIManager();
}
return instance;
}
}
//存放界面的栈 后进先出
private Stack panelStack;
//根据名字查找对应的路径
public Dictionary<string, string> panelPathDic;
//根据名字查找游戏物体
public Dictionary<string, BasePanel> panelObjDic;
private static Transform root;
public static Transform Root
{
get
{
if (root == null)
{
root = GameObject.Find(“Canvas”).transform ;
}
return root;
}

    }

    private UIManager()
    {
        panelStack = new Stack<BasePanel>();
        panelPathDic = new Dictionary<string, string>();
        panelObjDic = new Dictionary<string, BasePanel>();
        panelPathDic.Add(UIPanelType .MainUIPanel ,UIPanelPath.MainUIPanelPath);
        panelPathDic.Add(UIPanelType.ShopPanel, UIPanelPath.ShopPanelPath);
        panelPathDic.Add(UIPanelType.SettingPanel ,UIPanelPath .SettingPanelPath );
        panelPathDic.Add(UIPanelType.BagPanel, UIPanelPath.BagPanelPath);
        panelPathDic.Add(UIPanelType.SkillPanel, UIPanelPath.SkillPanelPath);
        panelPathDic.Add(UIPanelType.PopPanel, UIPanelPath.PopPanelPath);
    }
    //打开页面
    public void OpenPanel(string panelType)
    {
        //判断当前栈中是否有界面
        if (panelStack.Count  > 0)
        {
            BasePanel currentPanel = panelStack.Peek();
            currentPanel.OnPause();
        }
        
        //将加载的界面放入栈中
        BasePanel openPanel = GetPanel(panelType);           
        openPanel.OnEnter();
        panelStack.Push(openPanel);
    }
    //关闭界面
    public void ClosePanel()
    {
        if (panelStack.Count <= 0)
            return;
        //关闭当前界面
        BasePanel closePanel = panelStack.Pop();
        closePanel.OnExit();          
        if (panelStack.Count <= 0)
            return;
        //恢复上一界面功能
        BasePanel lastPanel = panelStack.Peek();
        lastPanel.OnResume();
    }
    private BasePanel GetPanel(string panelType)
    {
       
        BasePanel panel=null;           
        panelObjDic.TryGetValue(panelType ,out panel );

        if (panel == null)
        {               
            string panelPath;               
            panelPathDic.TryGetValue(panelType ,out panelPath);
            if (panelPath == null)
            {
                Debug.LogFormat("未找到{0}的路径", panel);
            }
            else
            {
                //找到路径后,实例化                  
                GameObject panelObj = GameObject.Instantiate(Resources.Load<GameObject>(panelPath));                
                panelObj.transform.SetParent(Root ,false );
                panel = panelObj.transform.GetComponent<BasePanel>();
                panelObjDic.Add(panelType, panel);
            }            
        }
        return panel;
    }            
}     

}

第四步 每个界面路径都挂载一个脚本

加上 canvas group 组件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UIFramework;

public class ShopPanel : BasePanel
{
private CanvasGroup canvasGroup;
public CanvasGroup CanvasGroup
{
get
{
if (canvasGroup == null)
{
canvasGroup = GetComponent();
}
return canvasGroup;
}
}
public override void OnEnter()
{
CanvasGroup.alpha = 1;
CanvasGroup.interactable = true;
CanvasGroup.blocksRaycasts = true;
}
public override void OnExit()
{
CanvasGroup.alpha = 0;
CanvasGroup.interactable = false;
CanvasGroup.blocksRaycasts = false;
}
public override void OnPause()
{

    CanvasGroup.interactable = false;
    CanvasGroup.blocksRaycasts = false;
}
public override void OnResume()
{

    CanvasGroup.interactable = true;
    CanvasGroup.blocksRaycasts = true;

}
public void ClosePanel()
{
    UIManager.Inatance.ClosePanel();
}

}

第五步 控制打开主界面

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UIFramework;

public class Root :MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
UIManager.Inatance.OpenPanel(UIPanelType.MainUIPanel);
}
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值