泛型获取UGUI的组件
UGUI UIBehaviour是所有UI组件的基类,UI组件都是直接或者间接继承UIBehaviour这个抽象类的,它继承自MonoBehavior,所以拥有和Unity相同的生命周期public bool TryGetControl<T>(int id, out T ui, bool addIfNotExist = false) where T : MonoBehaviour{ ui = null; UIBehaviour behaviour = id2ui; //id2ui-UIBehaviour[] ui = behaviour as T; if (ui == null){ ui = behaviour.GetComponent<T>(); if (ui == null){ if (addIfNotExist){ ui = behaviour.gameObject.AddComponent<T>(); return true; } return false; } return true; } return true;}
页:
[1]