/// <summary> /// 创建ui /// </summary> /// <param name="uiName">ui类的名称</param> /// <param name="parent">父窗口,当窗口为空时,UIWindowConfig.level生效,窗口对象直接挂在对应的level上</param> /// <param name="targetObject">ui的依附游戏对象</param> /// <returns></returns> private UIWindow _createUI(string uiName, UIWindow parent = null, GameObject selfObject = null) { UIWindow wnd; JWindowConfig structData = _getStructData(uiName); if (structData != null) { wnd = _createUIByStructData(structData, selfObject); if (parent != null) { return(wnd); } else if (wnd != null) { //设置窗口层 if (_setUILeve(structData.layer, wnd)) { m_UIWindowList[uiName] = wnd; return(wnd); } else { Debug.LogError("设置UI level 失败"); return(null); } } } else { Debug.LogError("没有找到" + uiName + "窗口"); } return(null); }
private JWindowConfig _getStructData(string name) { JWindowConfig config = null; m_UIWindowConfigs.TryGetValue(name, out config); return(config); }
/// <summary> /// 创建ui类 /// </summary> /// <param name="config"></param> /// <param name="targetObject">ui类属于的对像,如果为空,即创建类的gameObject</param> /// <returns></returns> private UIWindow _createUIByStructData(JWindowConfig config, GameObject targetObject) { GameObject objTem = targetObject; if (targetObject == null) { objTem = Resources.Load <GameObject>(config.prefab); } if (objTem != null) { Type type = _getTypeByString(config.name); if (type != null) { GameObject obj = targetObject; if (obj == null) { obj = GameObject.Instantiate(objTem, objTem.transform.position, objTem.transform.rotation) as GameObject; Resources.UnloadUnusedAssets(); obj.name = config.name; } UIWindow wnd = obj.GetComponent(type) as UIWindow; if (wnd == null) { wnd = obj.AddComponent(type) as UIWindow; if (wnd == null) { Debug.LogError(config.name + "没有组件" + config.name); } } if (wnd != null) { wnd.Initialize(config); wnd.SetShow(false); return(wnd); } return(null); } else { Debug.LogError(config.name + "没有组件" + config.name); } } else { Debug.Log("找不到Prefab " + config.name); } return(null); }
/// <summary> /// 为确保正确的初始化顺序,Initialize 在UIWindow创建后手动调用 /// </summary> public void Initialize(JWindowConfig config) { m_Panel = gameObject.GetComponent <Image>(); m_Collider = GetComponent <PolygonCollider2D>(); m_bIsCreated = true; m_Config = config; m_CanvasGroup = GetComponent <CanvasGroup>(); if (m_CanvasGroup == null) { m_CanvasGroup = gameObject.AddComponent <CanvasGroup>(); m_CanvasGroup.alpha = 0; } m_Panel.raycastTarget = m_Config.hasMessage; if (!_initialize()) { Destroy(this); } }