void AutoInjectUGUI(BaseUI _go) { Type type = _go.GetType(); //fields :Get All Pulic Fields FieldInfo[] fis = type.GetFields(); for (int i = 0; i < fis.Length; i++) { FieldInfo fi = fis[i]; // Get AutoUGUI Attribute object[] attributes = fi.GetCustomAttributes(typeof(AutoUGUI), true); if (attributes.Length > 0) { AutoUGUI att = attributes[0] as AutoUGUI; SetValueByAttribute(_go, att, fi); } } }
private void CloseUI(UIType _uiType, GameObject _uiObj) { if (_uiObj == null) { dicOpenUIs.Remove(_uiType); } else { BaseUI _baseUI = _uiObj.GetComponent <BaseUI>(); if (_baseUI != null) { _baseUI.StateChanged += CloseUIHandler; _baseUI.Release(); } else { GameObject.Destroy(_uiObj); dicOpenUIs.Remove(_uiType); } } }
private void SetValueByAttribute(BaseUI _target, AutoUGUI attribute, FieldInfo _fi) { string fieldName = _fi.Name; Type type = _fi.FieldType; GameObject obj = _target.gameObject; Transform tf = null; if (!string.IsNullOrEmpty(attribute.Path)) { tf = obj.transform.Find(attribute.Path); } else { Component[] pComp = obj.transform.GetComponentsInChildren(type); for (int i = 0; i < pComp.Length; i++) { if (pComp[i].name.Equals(fieldName)) { tf = pComp[i].transform; break; } } } if (tf != null && !type.IsValueType && tf.GetComponent(_fi.FieldType) != null) { try { // _fi in _target,Set Value To _fi _fi.SetValue(_target, tf.GetComponent(type)); //Debug.Log("注册UGUI:" + fieldName); } catch (Exception e) { Debug.LogWarning("注册UGUI失败:" + e); } } }
/// <summary> /// 协程 加载UI /// </summary> /// <returns></returns> private IEnumerator <int> AsyncLoadData() { UIInfoData _uiInfoData = null; UnityEngine.Object _prefabObj = null; GameObject _uiObject = null; if (stackOpenUIs != null && stackOpenUIs.Count > 0) { do { _uiInfoData = stackOpenUIs.Pop(); _prefabObj = Resources.Load(_uiInfoData.Path); if (_prefabObj != null) { //_uiObject = NGUITools.AddChild(Game.Instance.mainUICamera.gameObject, _prefabObj as GameObject); _uiObject = MonoBehaviour.Instantiate(_prefabObj) as GameObject; BaseUI _baseUI = _uiObject.GetComponent <BaseUI>(); if (null == _baseUI) { _baseUI = _uiObject.AddComponent(_uiInfoData.ScriptType) as BaseUI; } // 自动注册指定UGUI AutoInjectUGUI(_baseUI); // 根据打开参数,View加载UI相关数据:比如音乐、动画等 if (null != _baseUI) { _baseUI.SetUIWhenOpening(_uiInfoData.UIParams); } dicOpenUIs.Add(_uiInfoData.UIType, _uiObject); } } while(stackOpenUIs.Count > 0); } yield return(0); }