UI簡易框架-----泰斗社區

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;
        }
}

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);
        }
}

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
 
public class UILogin : UIBase
{
        public override UIManager.EUI eUI
        {
                get
                {
                        return UIManager.EUI.UILogin;
                }
        }
    InputField usernameInput;
    InputField passwordInput;
    Button btnLogin;
    protected override void InitComponents()
    {
        usernameInput = UStaticFuncs.FindChildComponent<InputField>(transform, "username");
        passwordInput = UStaticFuncs.FindChildComponent<InputField>(transform, "username");
        btnLogin = UStaticFuncs.FindChildComponent<Button>(transform, "Login");
 
        btnLogin.onClick.AddListener(
            delegate ()
            {
                string username = usernameInput.text;
                string password = passwordInput.text;
                //do login
            });
    }
}

附贈兩個遞歸查找子物體的方法:
[C#] 純文本查看 複製代碼
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>();
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章