Unity中简单 UI 管理类 - UIManager
2015-11-03 14:21
483 查看
进入下一个界面
// 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; } } } }
相关文章推荐
- Unity3D脚本技术框架
- Unity3d 引擎原理详细介绍
- Unity3D-深入剖析NGUI的游戏UI架构
- 通过unity画弧线
- 【VR】Leap Motion 官网文档(一)Unity资源与插件
- Shader 学习笔记 20151103
- UnityShader学习笔记(1)
- Unity3D中脚本的执行顺序和编译顺序(vs工程引用关系)
- unity, OnTriggerEnter2D不触发
- Microsoft.Practices.Unity AOP unity 3.x
- 【Unity游戏开发之十一】基于NGUI的表情图文混排解决方案
- 【Unity3D游戏开发】NGUI制作字体的三种方法 (二一)
- unity制作斗地主游戏大致流程
- Unity3d 4.6.8 ShareSDK 分享新浪,微信,qq,经验总结
- Unity 鼠标事件 GUI GUILayerOut Input Application(截屏,getMouseDown,getKeyCode) 射线
- 说说Unity3D里的各种坐标系
- Unity3D脚本语言UnityScript初探
- unity, particle play once and destroy
- Unity3D移动平台动态读取外部文件全解析
- Unity3D 5.0简单的实现跳跃功能