Unity中简单 UI 管理类 - UIManager

来源:互联网 发布:淘宝店怎么提高访客 编辑:程序博客网 时间:2024/06/04 19:07

进入下一个界面

    // Enter Next UI;    // MonoBehaviour can replace your UIBase Class;    public T Enter<T>() where T: MonoBehaviour    {        foreach(var o in mUIList)        {                        T ui = o.GetComponent<T>();            if(ui!=null)            {                if(mGo_CurPage!=null)                {                    mGo_CurPage.SetActive(false);                }                ui.gameObject.SetActive(true);                // ui init;                // ...                // ...                mGo_CurPage = ui.gameObject;                mCurOpenList.Add(mGo_CurPage);                return ui;            }        }        return null;    }

这里可以不继承MonoBehaviour类,可以自己定义一个 UIBase 类,用于一些初始化等操作

返回上一个界面

    // Return Before UI;    public void Back()    {        for(int i=mCurOpenList.Count-1;i>=0;--i)        {            if(i>=0)            {                if(i-1>=0)                {                    // before page;                    mCurOpenList[i-1].SetActive(true);                    // cur page;                    mCurOpenList[i].SetActive(false);                    RemoveOpenList(mGo_CurPage);                    mGo_CurPage = mCurOpenList[i-1];                    return ;                }            }        }    }

使用

例如你写了三个界面(三个Class)分别为

UIPage1
UIPage2
UIPage3

将这三个 Class 都挂在 gameObject 上面,拖到如图所示的地方
UIManager

外部即可直接调用

UIManager.Enter();
UIManager.Back();

你也可以自己在外面再封装一层

完整代码

using System;using System.Collections.Generic;using UnityEngine;public class UIPage1{}public class UIPage2{}public class UIPage3{}public class UIManager : MonoBehaviour{         public bool Debug = true;    public List<GameObject> mUIList = new List<GameObject>();    private List<GameObject> mCurOpenList = new List<GameObject>();    private GameObject mGo_CurPage=null;    void Start()    {        if(!Debug)        {            Enter<UIPage1>();        }    }    // Enter Next UI;    // MonoBehaviour can replace your UIBase Class;    public T Enter<T>() where T:MonoBehaviour    {        foreach(var o in mUIList)        {                        T ui = o.GetComponent<T>();            if(ui!=null)            {                if(mGo_CurPage!=null)                {                    mGo_CurPage.SetActive(false);                }                ui.gameObject.SetActive(true);                // ui init;                // ...                // ...                mGo_CurPage = ui.gameObject;                mCurOpenList.Add(mGo_CurPage);                return ui;            }        }        return null;    }    // Return Before UI;    public void Back()    {        for(int i=mCurOpenList.Count-1;i>=0;--i)        {            if(i>=0)            {                if(i-1>=0)                {                    // before page;                    mCurOpenList[i-1].SetActive(true);                    // cur page;                    mCurOpenList[i].SetActive(false);                    RemoveOpenList(mGo_CurPage);                    mGo_CurPage = mCurOpenList[i-1];                    return ;                }            }        }    }    // Remove Opened UI;    void RemoveOpenList(GameObject go)      {        for(int i=0;i<mCurOpenList.Count;++i)        {            if(mCurOpenList[i] ==go)            {                mCurOpenList.RemoveAt(i);                return;            }        }    }}
0 0
原创粉丝点击