窗口UI管理器-Dictionary

来源:互联网 发布:数据库与网站 编辑:程序博客网 时间:2024/05/21 10:24

CJUIWindowMgr类

<span style="font-size:18px;">using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 窗口UI管理器/// </summary>public class CJUIWindowMgr{    private static CJUIWindowMgr instance;    public static CJUIWindowMgr GetInstance()    {        if (instance == null)            instance = new CJUIWindowMgr();        return instance;    }    //窗口缓存器    public Dictionary<string, GameObject> windowCache = new Dictionary<string, GameObject>();    /// <summary>    /// 打开窗口    /// </summary>    /// <param name="windowPath">窗口预设/。。。路径</param>    /// <param name="parent">窗口挂点(父节点)</param>    /// <returns></returns>    public GameObject OpenWindow(string windowPath,Transform parent)    {        GameObject windowUI = null;        if (windowCache.ContainsKey(windowPath))         {            windowUI = windowCache[windowPath];            if (windowUI == null)            {                Debug.LogError("The Window is Deleted");                return null;            }            windowUI.SetActive(true);        }        else        {            //加载窗口资源            GameObject windowPrefab = Resources.Load(windowPath) as GameObject;            //实例化窗口资源            windowUI = GameObject.Instantiate(windowPrefab);            //Transform组件重置            windowUI.transform.parent = parent;            windowUI.transform.localPosition = Vector3.zero;            windowUI.transform.localScale = Vector3.one;            windowUI.transform.localRotation = Quaternion.Euler(Vector3.zero);            //缓存窗口            windowCache.Add(windowPath,windowUI);        }        return windowUI;    }    /// <summary>    /// 关闭窗口    /// </summary>    /// <param name="windowPath">窗口资源的路径</param>    /// <param name="remove">是否Destroy</param>    public void CloseWindow(string windowPath,bool remove = false)    {        if (windowCache.ContainsKey(windowPath))        {            GameObject windowUI  = windowCache[windowPath];            if (windowUI == null)            {                Debug.LogError("The Window is Deleted");                return;            }            else            {                if (remove)                {                    GameObject.Destroy(windowUI);                    windowCache.Remove(windowPath);                }                else                {                    windowUI.SetActive(false);                }            }        }    }}</span>

CJUIWindowCtrBase类

<span style="font-size:24px;">using UnityEngine;using System.Collections;public class CJUIWindowCtrBase : MonoBehaviour {    void Awake()    {        OnAwake();    }void Start () {        OnStart();}// Update is called once per framevoid Update () {        OnUpdate();}    void OnDestroy()    {        BeforeOnDestroy();    }    protected virtual void OnAwake() { }    protected virtual void OnStart() { }    protected virtual void OnUpdate() { }    protected virtual void BeforeOnDestroy() {        CJUIWindowMgr.GetInstance().windowCache.Clear();    }}</span>

CJUIWindow_FunctionCtr类

using UnityEngine;using System.Collections;/// <summary>/// 功能菜单窗口控制器/// </summary>public class CJUIWindow_FunctionCtr : CJUIWindowCtrBase {    bool toggle = false;// Use this for initialization    protected override void OnStart()    {        base.OnStart();        Init();    }    void Init()    {        UIButton[] btnArray = GetComponentsInChildren<UIButton>();        for (int index = 0; index < btnArray.Length; index++)        {            UIEventListener.Get(btnArray[index].gameObject).onClick = delegate(GameObject go)            {                switch (go.name)                {                    case "Shop":                        break;                    case "Bag":                        if (!toggle)                        {                            CJUIWindowMgr.GetInstance().OpenWindow("UI/UIWindow/StartMenuScene/UIWindow_Bag", GameObject.Find("CenterContainer").transform);                            toggle = true;                        }                        else                        {                            CJUIWindowMgr.GetInstance().CloseWindow("UI/UIWindow/StartMenuScene/UIWindow_Bag", false);                            toggle = false;                        }                        break;                    default:                        Debug.LogError("The Button is Not Register Call Back");                        break;                }            };        }    }    protected override void BeforeOnDestroy()    {        base.BeforeOnDestroy();    }}



0 0
原创粉丝点击