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.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
- Unity中简单 UI 管理类 - UIManager
- Unity3d游戏开发框架-UI管理类 UIManager
- 在UNITY中创建出一个简单的UI界面
- Unity UI层级管理框架
- Java中UIManager类的几种外观风格
- Unity简单UI框架搭建
- LookAndFeel类&UIManager类
- UIManager
- UIManager
- java中的UIManager(UI管理器)
- JAVA中UIManager的使用
- Unity中实现UI跟随
- VR中Unity UI系统
- Unity框架——UI管理
- Unity UI事件管理系统设计
- Unity中使用有限状态机(FSM)来管理UI面板的切换(UGUI)
- Unity 简单的特效管理
- Unity 基于Lua UI简单框架
- git 免登录 文件
- Ubuntu(64bit)搭建Android测试环境
- 关于 @synchronized 理解
- 代理---代理---代理(这也把问题说清楚了,不过某些命名不是很规范)
- [FLUME-2052]Spooling directory source should be able to replace or ignore malformed characters
- Unity中简单 UI 管理类 - UIManager
- MPU6050 6DOF 数据读取
- ios开发实用快捷键
- Android学习—计算器实现
- PHP常用函数
- linux下的c语言五子棋
- 连接mongodb时候报:Invalid credentials for database 'xxx'.
- iOS实现多个异步线程同步的操作
- Bitmap那些事(3)