using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using UnityEngine.UI; public class UIBase : MonoBehaviour , UILifeCycleInterface { [HideInInspector] public Canvas m_canvas; #region 重载方法 //当UI第一次打开时调用OnInit方法,调用时机在OnOpen之前 public virtual void OnInit() { } protected virtual void OnUIDestroy() { } #endregion #region 继承方法 private int m_UIID = -1; public int UIID { get { return m_UIID; } //set { m_UIID = value; } } public string UIEventKey { get { return UIName + "@" + m_UIID; } //set { m_UIID = value; } } string m_UIName = null; public string UIName { get { if (m_UIName == null) { m_UIName = name; } return m_UIName; } set { m_UIName = value; } } public void Init(string UIEventKey, int id) { if(UIEventKey != null) { UIName = null; UIName = UIEventKey + "_" + UIName; } m_UIID = id; m_canvas = GetComponent(); CreateObjectTable(); OnInit(); } public void Dispose() { ClearGuideModel(); RemoveAllListener(); CleanItem(); CleanModelShowCameraList(); ClearLoadSprite(); try { OnUIDestroy(); } catch(Exception e) { Debug.LogError("UIBase Dispose Exception -> UIEventKey: " + UIEventKey + " Exception: " + e.ToString()); } DisposeLifeComponent(); } #region 获取对象 public List m_objectList = new List(); //生成对象表,便于快速获取对象,并忽略层级 void CreateObjectTable() { if (m_objects == null) { m_objects = new Dictionary(); } m_objects.Clear(); m_images.Clear(); m_Sprites.Clear(); m_texts.Clear(); m_textmeshs.Clear(); m_buttons.Clear(); m_scrollRects.Clear(); m_reusingScrollRects.Clear(); m_rawImages.Clear(); m_rectTransforms.Clear(); m_inputFields.Clear(); m_Sliders.Clear(); m_joySticks.Clear(); m_joySticks_ro.Clear(); m_longPressList.Clear(); m_Canvas.Clear(); for (int i = 0; i < m_objectList.Count; i++) { if (m_objectList[i] != null) { if (m_objects.ContainsKey(m_objectList[i].name)) { Debug.LogError("CreateObjectTable ContainsKey ->" + m_objectList[i].name + "<-"); } else { m_objects.Add(m_objectList[i].name, m_objectList[i]); } } else { Debug.LogWarning(name + " m_objectList[" + i + "] is Null !"); } } } Dictionary m_uiBases = new Dictionary(); Dictionary m_objects = null; Dictionary m_images = new Dictionary(); Dictionary m_Sprites = new Dictionary(); Dictionary m_texts = new Dictionary(); Dictionary m_textmeshs = new Dictionary(); Dictionary m_buttons = new Dictionary(); Dictionary m_scrollRects = new Dictionary(); Dictionary m_reusingScrollRects = new Dictionary(); Dictionary m_rawImages = new Dictionary(); Dictionary m_rectTransforms = new Dictionary(); Dictionary m_inputFields = new Dictionary(); Dictionary m_Sliders = new Dictionary(); Dictionary m_Canvas = new Dictionary(); Dictionary m_Toggle = new Dictionary(); Dictionary m_joySticks = new Dictionary(); Dictionary m_joySticks_ro = new Dictionary(); Dictionary m_longPressList = new Dictionary(); Dictionary m_dragList = new Dictionary(); public bool HaveObject(string name) { bool has = false; has = m_objects.ContainsKey(name); return has; } public GameObject GetGameObject(string name) { if (m_objects == null) { CreateObjectTable(); } if (m_objects.ContainsKey(name)) { GameObject go = m_objects[name]; if (go == null) { throw new Exception("UIWindowBase GetGameObject error: " + UIName + " m_objects[" + name + "] is null !!"); } return go; } else { throw new Exception("UIWindowBase GetGameObject error: " + UIName + " dont find ->" + name + "<-"); } } public RectTransform GetRectTransform(string name) { if (m_rectTransforms.ContainsKey(name)) { return m_rectTransforms[name]; } RectTransform tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetRectTransform ->" + name + "<- is Null !"); } m_rectTransforms.Add(name, tmp); return tmp; } public UIBase GetUIBase(string name) { if (m_uiBases.ContainsKey(name)) { return m_uiBases[name]; } UIBase tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetUIBase ->" + name + "<- is Null !"); } m_uiBases.Add(name, tmp); return tmp; } public Sprite GetSprite(string name) { if (m_Sprites.ContainsKey(name)) { return m_Sprites[name]; } Sprite tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetImage ->" + name + "<- is Null !"); } m_Sprites.Add(name, tmp); return tmp; } public Image GetImage(string name) { if (m_images.ContainsKey(name)) { return m_images[name]; } Image tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetImage ->" + name + "<- is Null !"); } m_images.Add(name, tmp); return tmp; } public TextMesh GetTextMesh(string name) { if (m_textmeshs.ContainsKey(name)) { return m_textmeshs[name]; } TextMesh tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetText ->" + name + "<- is Null !"); } m_textmeshs.Add(name, tmp); return tmp; } public Text GetText(string name) { if (m_texts.ContainsKey(name)) { return m_texts[name]; } Text tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetText ->" + name + "<- is Null !"); } m_texts.Add(name, tmp); return tmp; } public Toggle GetToggle(string name) { if (m_Toggle.ContainsKey(name)) { return m_Toggle[name]; } Toggle tmp = GetGameObject(name).GetComponent(); if (tmp == null) { throw new Exception(m_EventNames + " GetText ->" + name + "<- is Null !"); } m_Toggle.Add(name, tmp); return tmp; } public Button GetButton(string name) { if (m_buttons.ContainsKey(name)) { return m_buttons[name]; } Button tmp = GetGameObject(name).GetComponent