// 打开窗口 通过callback可以在回调中获取界面对象,param可以传递数据给OnBindData函数 public void OpenWindow <T>(OpenWindowHandler callback) where T : UIWindow { string winName = GetWindowName <T>(); if (string.IsNullOrEmpty(winName)) { return; } UIWindow openedWindow = FindWindow <T>(); if (openedWindow != null) { if (!openedWindow.IsShown) { // 如果窗口有隐藏,则直接显示,如果已经处于显示状态,则直接忽略 openedWindow.OnBindData(null); openedWindow.OnRefreshWindow(); openedWindow.Show(); openedWindow.PlayOpenAnimation(); // TODO 看实际情况决定是否播放开启音效 // 响应外部回调事件 if (callback != null) { callback(openedWindow); } } } else { string path = "GUI/" + winName; GameObject prefab = Resources.Load(path) as GameObject; GameObject go = Instantiate(prefab) as GameObject; InitWindow(winName, go, callback, null); } }
private void InitWindow(string winName, GameObject go, OpenWindowHandler callback, params object[] param) { UIWindow window = go.GetComponent <UIWindow>(); if (!window) { return; } Transform parent = _root.transform; // 根据层级配置决定附加在哪个层 if (window.Layer == UIWindow.WindowLayer.NORMAL) { parent = _root.transform; } else if (window.Layer == UIWindow.WindowLayer.TOPMOST) { parent = _rootTopmost != null ? _rootTopmost.transform : _root.transform; } else if (window.Layer == UIWindow.WindowLayer.BG) { parent = _rootBg != null ? _rootBg.transform : _root.transform; } // 添加Selectable防止穿透 if (!window.IgnoreRaycast) { Selectable selectable = go.GetComponent <Selectable>(); if (selectable == null) { selectable = go.AddComponent <Selectable>(); } selectable.transition = Selectable.Transition.None; } // TODO 看看如何统一局部坐标和全局坐标 if (window.StartPosition.sqrMagnitude > 0) { window.transform.localPosition = window.StartPosition; } else { go.transform.localPosition = Vector3.zero; } if (window.IsShowBackground || window.BackgroundEvent != UIWindow.ClickEvent.NONE) { GameObject goBackground = new GameObject(winName + "_BG"); RectTransform rt = goBackground.AddComponent <RectTransform>(); rt.localPosition = Vector3.zero; rt.localScale = Vector3.one; rt.anchorMin = new Vector2(0, 0); rt.anchorMax = new Vector2(1, 1); rt.anchoredPosition = new Vector2(0.5f, 0.5f); rt.sizeDelta = Vector3.one; rt.pivot = new Vector2(0.5f, 0.5f); Image image = goBackground.AddComponent <Image>(); if (window.IsShowBackground) { image.color = window.BackgroundColor; } else { image.color = new Color(0, 0, 0, 0); } Button btn = goBackground.AddComponent <Button>(); btn.transition = Selectable.Transition.None; btn.onClick.AddListener(window.OnClickBackground); go.transform.SetParent(goBackground.transform, false); goBackground.transform.SetParent(parent, false); window.WindowObject = goBackground; } else { go.transform.SetParent(parent, false); window.WindowObject = go; } // 如果设定为独立互斥窗口,则关闭其他界面 if (window.IsMutexWindow) { CloseAllWindow(winName); } go.name = winName; go.transform.localScale = Vector3.one; go.SetActive(true); _openedWindows.Add(window); window.HasInit = true; window.OnOpenWindow(); window.OnBindData(param); window.OnRefreshWindow(); window.PlayOpenAnimation(); if (!string.IsNullOrEmpty(window.OpenSound)) { AudioController.PlaySound(window.OpenSound); } // 响应外部回调事件 if (callback != null) { callback(window); } if (window.Layer == UIWindow.WindowLayer.TOPMOST) { Utils.SetLayer(window.WindowObject.transform, UITop); } if (window.BackgroundBlur) { EnableBlur(true); } }