NGUI之UI模块基类
2015-08-01 11:44
477 查看
using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// 所有UI模块的基类 /// 作者:小唐 /// 时间:2015.8.1 /// UI格式: /// Root --XXRoot.cs /// -Panel1 --XX.cs(继承 BaseUI) /// -Panel2 --XX.cs(继承 BaseUI) /// -Panel3 --XX.cs(继承 BaseUI) /// -。。。 /// 使用规则: /// 1、预设体根部的脚本以Root结尾,并在Awake中完成初始化(给子panel加脚本),Root中应当只有Panel1,Panel2,Panel3的GameObject、Script的静态字段 /// 2、将所有需要设置属性值的控件的Tag设置为UIGameObject,如果是需要重复的(如ScrollView的Item),Tag设置为UIGobjItem /// 3、Panel1,2,3中的属性访问器应在Root中访问 /// 3、每个挂载载Panel1,2,3上的脚本应当有m_data数据源属性、SetInfo方法、以及若干OnClick,事件注册/撤销的方法 /// </summary> public class BaseUI : MonoBehaviour { private bool isInit = false; private Dictionary<string, GameObject> UI; /// <summary> /// 初始化UI预制体中需要赋值的GameObject /// </summary> /// <param name="BaseTag"> 如果是重复的(如ScrollView的Item),传入UIGobjItem </param> public void UIInit(string BaseTag = "UIGameObject") { // 避免子类不必要的初始化,初始化一次就可以了 if (!isInit) { UI = new Dictionary<string, GameObject>(); Transform[] child = GetComponentsInChildren<Transform>(true); for (int i = 0, imax = child.Length; i < imax; ++i) { if (child[i].tag.Equals(BaseTag)) { //有异常打开下面 //if (UI.ContainsKey(child[i].name)) Debug.LogError(child[i]); UI.Add(child[i].name, child[i].gameObject); } } Init(); UI.Clear(); UI = null; isInit = true; } } /// <summary> /// 返回指定属性控件的GameObject /// </summary> /// <param name="InpropertyName"> 必须与预制体中相应属性控件的GameObject的名字一致 </param> /// <returns> 指定属性控件的GameObject </returns> public T GetProperty<T>(string InpropertyName) where T : MonoBehaviour { if (UI.ContainsKey(InpropertyName)) return UI[InpropertyName].GetComponent<T>(); Debug.LogError("请在脚本挂载的UI预制体内查找是否包含" + InpropertyName + ",或者是否将Tag设置为UIGameObject/UIGobjItem"); return null; } public GameObject GetProperty(string InpropertyName) { if (UI.ContainsKey(InpropertyName)) return UI[InpropertyName]; Debug.LogError("请在脚本挂载的UI预制体内查找是否包含" + InpropertyName + ",或者是否将Tag设置为UIGameObject/UIGobjItem"); return null; } public virtual void Init() { } protected void OnClickCloseButton(GameObject go) { gameObject.SetActive(false); } }
相关文章推荐
- EasyUI学习网站
- druid配置
- 设置uitextview边框
- 错误提示:连接 built-in.o 提示 undefined reference to
- IOS 自定义 tableView cell(UITableViewCell height) 高度
- SolrCloud Wiki翻译(4)Distributed Requests
- UITextfield
- POJ 1679 The Unique MST
- UILabel
- 通过UIAlertView或者ActionSheet控件调用share方法
- UIView
- poj 1019 Number Sequence 【组合数学+数字x的位宽函数】
- UITableViewCell 输入框动态变高
- Ueditor上传图片到本地改造到上传图片到云存储
- 随机生成UUID的工具类
- iOS开发UI篇—transframe属性(形变)
- Serializable serialVersionUID
- UIViewController 视图控制器
- UIButton,UITextField
- [LeetCode]232.Implement Queue using Stacks