NGUI之UI模块基类

来源:互联网 发布:java开源crm系统 编辑:程序博客网 时间:2024/06/11 19:39
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);    }}

0 0
原创粉丝点击