转载蛮牛上很好的UI管理脚本
2016-06-23 20:36
513 查看
1.UIManager管理类
2.UIBase基类
3.递归查找子物体的类
4.资源池的管理类
5.原文地址
http://www.manew.com/forum.php?mod=viewthread&tid=90695&page=0
http://www.manew.com/thread-90727-1-1.html
真的非常好用,我也是收藏着,项目中用起来很方便。
using UnityEngine; using System.Collections; using System; using System.Collections.Generic; public class UIManager { public enum EUI { UILogin, Num, } private static Dictionary<EUI, UIBase> dUIs = new Dictionary<EUI, UIBase>(); public static UIBase GetUI(EUI eUI) { if (dUIs.ContainsKey(eUI)) { return dUIs[eUI]; } return null; } public static UIBase CreateUI(EUI eUI) { if (dUIs.ContainsKey(eUI)) { if (dUIs[eUI] != null) { return dUIs[eUI]; } else { dUIs.Remove(eUI); } } Transform tr = Resources.Load<Transform>("UI/" + eUI); Transform uiTr = null; if (tr == null) { //加载AssetBundle } else { uiTr = GameObject.Instantiate(tr) as Transform; } uiTr.name = eUI.ToString(); UIBase ui = uiTr.GetComponent<UIBase>(); if (ui == null) { ui = uiTr.gameObject.AddComponent(Type.GetType(eUI.ToString())) as UIBase; } ui.transform.localPosition = Vector3.zero; dUIs.Add(eUI, ui); return ui; } }
2.UIBase基类
using UnityEngine; using System.Collections; public abstract class UIBase : MonoBehaviour { public virtual UIManager.EUI eUI { get { return UIManager.EUI.Num; } } protected bool bInited; void Awake() { InitComponents(); bInited = true; } protected virtual void InitComponents() { } public virtual void OnCloseUI() { Destroy(this.gameObject); } }
3.递归查找子物体的类
using UnityEngine; using System.Collections; public static class UStaticFuncs { public static Transform FindChild(Transform tr, string childName) { for (int i = 0;i<tr.childCount;i++) { if (tr.GetChild(i).name == childName) { Transform t = tr.GetChild(i); if (t != null) { return t; } } else { Transform t = FindChild(tr.GetChild(i), childName); if (t != null) { return t; } } } return null; } public static T FindChildComponent<T>(Transform tr, string childName) { Transform t = FindChild(tr, childName); return t.GetComponent<T>(); } }
4.资源池的管理类
using UnityEngine; using System.Collections; using System.Collections.Generic; public class UResourcePool { private static UResourcePool s_instance; private static GameObject poolParent; public static UResourcePool Instance { get { if (s_instance == null) { s_instance = new UResourcePool(); poolParent = new GameObject(); poolParent.name = "PoolParent"; poolParent.SetActive(false); } return s_instance; } } private Dictionary<string, List<GameObject>> dPool = new Dictionary<string, List<GameObject>>(); public GameObject OnGetResource(string sResource, string path) { if (!path.Contains("/")) { path = path + "/"; } if (!dPool.ContainsKey(sResource) || dPool[sResource].Count == 0) { GameObject go = Resources.Load<GameObject>(path + sResource); if (go == null) { Debug.LogError("Cannot find resourece " + path + sResource); } GameObject result = GameObject.Instantiate(go) as GameObject; result.name = sResource; return result; } else { GameObject result = dPool[sResource][0]; dPool[sResource].RemoveAt(0); result.transform.localPosition = Vector3.zero; result.transform.localRotation = Quaternion.Euler(Vector3.zero); if (result.transform.parent != poolParent.transform) { Debug.LogError("get resource out of pool"); } return result; } } public T OnGetResource<T>(string sResource, string path) where T : Object { if (!path.Contains("/")) { path = path + "/"; } if (!dPool.ContainsKey(sResource) || dPool[sResource].Count == 0) { T go = Resources.Load<T>(path + sResource); if (go == null) { Debug.LogError("Cannot find resourece " + path + sResource); } T result = GameObject.Instantiate(go) as T; result.name = sResource; return result; } return null; } public void OnReturnResource(GameObject go) { if (go.transform.parent == poolParent.transform) { return; } if (!dPool.ContainsKey(go.name)) { dPool.Add(go.name, new List<GameObject>()); } go.transform.parent = poolParent.transform; dPool[go.name].Add(go); } public void DoDestroy() { dPool.Clear(); s_instance = null; GameObject.Destroy(poolParent.gameObject); } }
5.原文地址
http://www.manew.com/forum.php?mod=viewthread&tid=90695&page=0
http://www.manew.com/thread-90727-1-1.html
真的非常好用,我也是收藏着,项目中用起来很方便。
相关文章推荐
- GUI - Web前端开发框架
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- Shell 脚本编程陷阱
- 【DevOps】为什么我们永远疲于奔命?
- VBS脚本写的Windows硬件检测工具分享
- 网络管理之IP地址篇
- 文件的读出 编辑 管理
- 用vbscript实现隐藏任务栏图标的脚本
- 用autoit编写第一个脚本(Hello World)
- VBS调用WMI快速关闭IE的脚本
- Oracle数据库执行脚本常用命令小结
- 收集的ROS防火墙脚本
- JSP脚本漏洞面面观
- 不错的批处理脚本 第一部分
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- 不错的批处理脚本实例代码 第二部分
- 使用脚本和批处理清除电脑中的痕迹的代码第1/2页
- 让你的脚本说话
- Lua脚本获取喜马拉雅MP3音频地址