/// <summary> /// 创建UI /// </summary> /// <param name="PrefabInfo"></param> /// <returns></returns> private static T CreateUI(CUIInfo UIInfo) { if (UIInfo.IsSigleton) //单例UI { if (m_UIInstance == null) //实例不存在 { m_UIInstance = InstantiateUIAndReturnComponent(UIInfo); } return(m_UIInstance); } else //非单例UI { T TempComp = InstantiateUIAndReturnComponent(UIInfo); UIInstances.Add(TempComp); return(TempComp); } }
/// <summary> /// 创建UI实例并返回其上的UI组件 /// /// </summary> /// <param name="PrefabName">预制体名</param> /// <param name="NeedAnimation">是否需要动画</param> /// <returns></returns> private static T InstantiateUIAndReturnComponent(CUIInfo UIInfo) { //找当前场景中的画布 GameObject _Canvas = GameObject.Find("Canvas"); if (_Canvas == null) { CLOG.E("now scene has not canvas"); return(null); } //创建UI预制体实例 GameObject CreatedUI = CPrefabManager.Instance.CreatePrefabInstance(UIInfo.PrefabName); if (CreatedUI == null) { CLOG.E("the UI {0} create failed", UIInfo.PrefabName); return(null); } //创建UI界面 CreatedUI.name = UIInfo.PrefabName; RectTransform RT = CreatedUI.GetComponent <RectTransform>(); //设置父节点 CreatedUI.transform.SetParent(_Canvas.transform, false); RT.localScale = Vector3.one; //向UI上添加自身组件 T Ins = GetComponentSafe(CreatedUI); //记录UI信息 Ins.UIInfo = UIInfo; if (UIInfo.IsAnimationUI) { Ins.AnimOnCreate(); } //返回UI实例 return(Ins); }