using System.Collections; using System.Collections.Generic; using Games.Events; using Games.GlobeDefine; using Games.Scene; using Module.Log; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using EventSystem = UnityEngine.EventSystems.EventSystem; public class UIManager : MonoBehaviour { public delegate void OnOpenUIDelegate(bool bSuccess, object param); private static UIManager m_instance; [SerializeField] public RectTransform BaseUIRoot; // 位于UI最底层,常驻场景,基础交互 [SerializeField] public RectTransform BaseUIRootLeft; // 用于动画效果 [SerializeField] public RectTransform BaseUIRootRight; // 用于动画效果 [SerializeField] public RectTransform CameraTexture; [SerializeField] public Canvas Canvas; [SerializeField] public RectTransform DamageHightRoot; [SerializeField] public RectTransform DeathUIRoot; // public EventSystem EventSystemObj; [SerializeField] public RectTransform GuideUIRoot; [SerializeField] public RectTransform LoadingUIRoot; [SerializeField] public RectTransform MenuPopUIRoot; [SerializeField] public RectTransform MessageTipRoot; [SerializeField] public RectTransform MessageUIRoot; [SerializeField] public RectTransform PopUIRoot; // 位于UI上层,弹出式,互斥 [SerializeField] public RectTransform StoryUIRoot; // 故事背景层 [SerializeField] public RectTransform TipUIRoot; // 位于UI顶层,弹出重要提示信息等 [SerializeField] public Camera UICamera; [SerializeField] public RectTransform UIEffectRoot; private readonly Dictionary m_dicBaseUI = new Dictionary(); private readonly Dictionary m_dicCacheUI = new Dictionary(); private readonly Dictionary m_dicDeathUI = new Dictionary(); private readonly Dictionary m_dicMenuPopUI = new Dictionary(); private readonly Dictionary m_dicMessageTipUI = new Dictionary(); private readonly Dictionary m_dicMessageUI = new Dictionary(); private readonly Dictionary m_dicPopUI = new Dictionary(); private readonly Dictionary m_dicStoryUI = new Dictionary(); private readonly Dictionary m_dicTipUI = new Dictionary(); private readonly Dictionary m_dicWaitLoad = new Dictionary(); private void Awake() { m_dicTipUI.Clear(); m_dicBaseUI.Clear(); m_dicPopUI.Clear(); m_dicStoryUI.Clear(); m_dicMenuPopUI.Clear(); m_dicMessageUI.Clear(); m_dicMessageTipUI.Clear(); m_dicDeathUI.Clear(); m_dicCacheUI.Clear(); m_instance = this; DontDestroyOnLoad(this); UICamera.depthTextureMode = DepthTextureMode.None; UICamera.gameObject.EnsureComponent(); } /// /// CanvasScaler希望的屏幕尺寸,和真实屏幕像素不一样 /// //public Vector2 canvasSize { get; private set; } private void Start() { //var canvasScaler = GetComponent(); //canvasSize = canvasScaler.referenceResolution; SetupScreenOrientation(); EventDispatcher.Instance.Add(EventId.SceneMovie, OnSceneMovie); } private void OnDestroy() { if (!GameManager.applicationQuit) { m_instance = null; EventDispatcher.Instance.Remove(EventId.SceneMovie, OnSceneMovie); } } public static UIManager Instance() { return m_instance; } public void DestroySelf() { gameObject.SetActive(false); Destroy(gameObject); m_instance = null; } /// /// 设置自动旋转屏幕 /// private void SetupScreenOrientation() { Screen.orientation = ScreenOrientation.AutoRotation; Screen.autorotateToLandscapeLeft = true; Screen.autorotateToLandscapeRight = true; Screen.autorotateToPortrait = false; Screen.autorotateToPortraitUpsideDown = false; } private void OnSceneMovie(object args) { var start = (bool) args; //Canvas.enabled = !start; UICamera.enabled = !start; } // private void Update() // { // if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Escape)) // { // if (PlatformHelper.IsChannelHasExitDialog()) // PlatformHelper.ExitGame(); // else // MessageBoxLogic.OpenOKCancelBox(4933, -1, () => { Application.Quit(); }); // } // } //clear ui public void UnLoadScene() { DestroyInDictionary(m_dicBaseUI); DestroyInDictionary(m_dicPopUI); DestroyInDictionary(m_dicTipUI); DestroyInDictionary(m_dicStoryUI); DestroyInDictionary(m_dicDeathUI); DestroyInDictionary(m_dicCacheUI); //foreach (var uiData in m_dicMenuPopUI) //{ // if (!uiData.Value.gameObject.activeSelf) // { // GameObject.Destroy(uiData.Value); // _DestoryUIPath.Add(uiData.Key); // } //} //for (int i = 0; i < _DestoryUIPath.Count; ++i) //{ // m_dicMenuPopUI.Remove(_DestoryUIPath[i]); //} var uiKeyList = new List(); // 关闭所有除CentreNotice以外的MessageUI MessageUIRoot节点保留不隐藏 foreach (var pair in m_instance.m_dicMessageUI) if (UIPathData.m_DicUIName[pair.Key].closeAnyway || !pair.Value.gameObject.activeSelf) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) { m_instance.CloseMessageUI(uiKeyList[i]); m_instance.m_dicMessageUI.Remove(uiKeyList[i]); } uiKeyList.Clear(); foreach (var pair in m_instance.m_dicMessageTipUI) if (UIPathData.m_DicUIName[pair.Key].closeAnyway) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) { m_instance.CloseMessageTipUI(uiKeyList[i]); m_instance.m_dicMessageTipUI.Remove(uiKeyList[i]); } if (m_dicBaseUI.ContainsKey(UIInfo.JumpGuideRoot.name)) { DestroyUI(UIInfo.GuanningAreaRoot, m_dicBaseUI[UIInfo.GuanningAreaRoot.name]); m_dicBaseUI.Remove(UIInfo.JumpGuideRoot.name); } // PVP if (m_dicBaseUI.ContainsKey(UIInfo.PvpSceneInfo.name)) { DestroyUI(UIInfo.PvpSceneInfo, m_dicBaseUI[UIInfo.PvpSceneInfo.name]); m_dicBaseUI.Remove(UIInfo.PvpSceneInfo.name); } // 试炼副本左侧的进度面板 if (m_dicBaseUI.ContainsKey(UIInfo.TestingCopyInfo.name)) { DestroyUI(UIInfo.TestingCopyInfo, m_dicBaseUI[UIInfo.TestingCopyInfo.name]); m_dicBaseUI.Remove(UIInfo.TestingCopyInfo.name); } // 试炼副本-一夫当关副本 if (m_dicBaseUI.ContainsKey(UIInfo.BuffsStateInfo.name)) { DestroyUI(UIInfo.BuffsStateInfo, m_dicBaseUI[UIInfo.BuffsStateInfo.name]); m_dicBaseUI.Remove(UIInfo.BuffsStateInfo.name); } if (ResourcePool.Instance) ResourcePool.Instance.ClearUIItems(); } public void UnLoadUIForce() { //DestroyInDictionary(m_dicBaseUI, true); DestroyInDictionary(m_dicPopUI, true); DestroyInDictionary(m_dicTipUI, true); DestroyInDictionary(m_dicStoryUI, true); DestroyInDictionary(m_dicDeathUI, true); DestroyInDictionary(m_dicCacheUI, true); var uiKeyList = new List(); // 关闭所有除CentreNotice以外的MessageUI MessageUIRoot节点保留不隐藏 foreach (var pair in m_instance.m_dicMessageUI) if (UIPathData.m_DicUIName[pair.Key].closeAnyway || !pair.Value.gameObject.activeSelf) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) { m_instance.CloseMessageUI(uiKeyList[i]); m_instance.m_dicMessageUI.Remove(uiKeyList[i]); } if (ResourcePool.Instance) ResourcePool.Instance.ClearUIItems(); } private void DestroyInDictionary(IDictionary uiDictionary, bool isForce = false) { var destoryUiPath = new List(); var closeUIPath = new List(); foreach (var keyValue in uiDictionary) { var uiPath = UIPathData.m_DicUIName[keyValue.Key]; if (SystemInfo.graphicsMemorySize + SystemInfo.systemMemorySize < 1600 || isForce) { //内存+显存,小于2G的,有点不够用了,直接销毁 //强力销毁 } else { //不销毁不关闭 if (!uiPath.isDestroyOnUnloasScene && !uiPath.closeAnyway) continue; //仅关闭 if (!uiPath.isDestroyOnUnloasScene && uiPath.closeAnyway && keyValue.Value.activeSelf) { closeUIPath.Add(uiPath); //CloseUI(uiPath); continue; } } //销毁 if (!keyValue.Value.activeSelf || uiPath.closeAnyway) { if (uiPath._IsBlurBackground) HideBlurBackGround(uiPath.name); DestroyUI(uiPath, keyValue.Value); destoryUiPath.Add(keyValue.Key); } } for (var i = 0; i < destoryUiPath.Count; i++) uiDictionary.Remove(destoryUiPath[i]); for (var i = 0; i < closeUIPath.Count; i++) CloseUI(closeUIPath[i]); } //特殊场景不显示部分UI,例如PVP不显示ActiveBtns private static bool CanShowUI(string uiName) { if (uiName == UIInfo.ActiveBtns.name || uiName == UIInfo.SuperBenefitFirstRechargeTip.name) if (GameManager.gameManager.RunningScene == GameManager.gameManager.PlayerDataPool.pvpIfo._PvpBattleSceneId || GameManager.gameManager.RunningScene == GameManager.gameManager.PlayerDataPool.pvpIfo._PvpSceneId || GameManager.gameManager.RunningScene == GlobeVar.CROSSSERVERSCENEID) return false; return true; } // 展示UI,根据类型不同,触发不同行为 public static bool ShowUI(UIPathData pathData, OnOpenUIDelegate delOpenUI = null, object param = null, bool defaultHide = false) { if (!m_instance) { LogModule.ErrorLog("game manager is not init"); return false; } if (!CanShowUI(pathData.name)) return false; m_instance.AddLoadDicRefCount(pathData.name); #if !UNITY_EDITOR && !UNITY_STANDALONE_WIN if (pathData.uiType == UIPathData.UIType.TYPE_POP || pathData.uiType == UIPathData.UIType.TYPE_STORY || pathData.uiType == UIPathData.UIType.TYPE_TIP || pathData.uiType == UIPathData.UIType.TYPE_MENUPOP) { if (ProcessInput.Instance != null) ProcessInput.Instance.ReleaseInput(); } #endif Dictionary curDic = null; switch (pathData.uiType) { case UIPathData.UIType.TYPE_BASE: case UIPathData.UIType.TYPE_BASE_LEFT: case UIPathData.UIType.TYPE_BASE_RIGHT: curDic = m_instance.m_dicBaseUI; break; case UIPathData.UIType.TYPE_POP: curDic = m_instance.m_dicPopUI; LuaUIManager.Instance.CloseAnyUI(); break; case UIPathData.UIType.TYPE_STORY: curDic = m_instance.m_dicStoryUI; LuaUIManager.Instance.CloseAnyUI(); break; case UIPathData.UIType.TYPE_TIP: curDic = m_instance.m_dicTipUI; break; case UIPathData.UIType.TYPE_MENUPOP: curDic = m_instance.m_dicMenuPopUI; LuaUIManager.Instance.CloseAnyUI(); break; case UIPathData.UIType.TYPE_MESSAGE: curDic = m_instance.m_dicMessageUI; break; case UIPathData.UIType.TYPE_MESSAGETIP: curDic = m_instance.m_dicMessageTipUI; break; case UIPathData.UIType.TYPE_DEATH: curDic = m_instance.m_dicDeathUI; break; case UIPathData.UIType.TYPE_GUIDE: curDic = m_instance.m_dicBaseUI; break; case UIPathData.UIType.TYPE_LOADING: curDic = m_instance.m_dicPopUI; break; case UIPathData.UIType.TYPE_CAMERA_TEXTURE: curDic = m_instance.m_dicBaseUI; break; default: return false; } if (null == curDic) return false; if (m_instance.m_dicCacheUI.ContainsKey(pathData.name)) { if (!curDic.ContainsKey(pathData.name)) curDic.Add(pathData.name, m_instance.m_dicCacheUI[pathData.name]); m_instance.m_dicCacheUI.Remove(pathData.name); } if (curDic.ContainsKey(pathData.name)) { var uiGO = curDic[pathData.name]; if (uiGO.activeSelf && delOpenUI != null) { if ((int) pathData.uiType >= (int) UIPathData.UIType.TYPE_POP) SetIndexToBtm(uiGO.transform); delOpenUI(true, param); } else { uiGO.SetActive(true); var hash = new Hashtable(); hash.Add("UIData", pathData); hash.Add("OnOpenUIDelegate", delOpenUI); hash.Add("Param", param); hash.Add("DefaultHide", defaultHide); m_instance.DoAddUI(pathData.name, uiGO, hash); } return true; } m_instance.LoadUI(pathData, delOpenUI, param, defaultHide); return true; } // 关闭UI,根据类型不同,触发不同行为 public static void CloseUI(UIPathData pathData) { if (null == m_instance) return; //int MaxCloseCount = PlayerPreferenceData.MaxCleanUICount; //if (MaxCloseCount > 6) //{ // MaxCloseCount = 6; //} //关闭MaxCloseCount次UI的时候,立即GC //if (++m_sCloseUICount >= MaxCloseCount) //{ // Resources.UnloadUnusedAssets(); // GC.Collect(); // m_sCloseUICount = 0; // // LogModule.DebugLog("CloseUI GC 1"); //} //else //{ // //活动,侠客,世界地图,PK,酒楼, 美人,背包界面,伙伴,每次打开都清理 // if (pathData.name == "ActivityController" || // pathData.name == "SwordsManController" || // pathData.name == "SceneMapRoot" || // pathData.name == "PKSetRoot" || // pathData.name == "Restaurant" || // pathData.name == "BelleController" || // pathData.name == "BackPackRoot" || // pathData.name == "PartnerAndMountRoot") // { // Resources.UnloadUnusedAssets(); // GC.Collect(); // m_sCloseUICount = 0; // //LogModule.DebugLog("CloseUI GC 2 " + pathData.name); // } //} //LogModule.DebugLog("m_sCloseUICount : " + m_sCloseUICount + " MaxCloseCount= " + MaxCloseCount); //if (!m_GCTimerGo) //{ // //关闭UI的时候,如果玩家不会进行其他操作,则顺手清理一下内存 // //如果关闭UI的时候玩家需要流畅的玩耍,则要排除掉 // //目前先增加特例,之后等特例多了之后进行统一处理 // if (pathData.name != "NewPlayerGuidRoot") // { // m_GCTimerGo = true; // m_GCWaitTime = Time.fixedTime; // } //} m_instance.RemoveLoadDicRefCount(pathData.name); switch (pathData.uiType) { case UIPathData.UIType.TYPE_BASE: case UIPathData.UIType.TYPE_BASE_LEFT: case UIPathData.UIType.TYPE_BASE_RIGHT: m_instance.CloseBaseUI(pathData.name); break; case UIPathData.UIType.TYPE_POP: m_instance.ClosePopUI(pathData.name); break; case UIPathData.UIType.TYPE_STORY: m_instance.CloseStoryUI(pathData.name); break; case UIPathData.UIType.TYPE_TIP: m_instance.CloseTipUI(pathData.name); break; case UIPathData.UIType.TYPE_MENUPOP: m_instance.CloseMenuPopUI(pathData.name); break; case UIPathData.UIType.TYPE_MESSAGE: m_instance.CloseMessageUI(pathData.name); break; case UIPathData.UIType.TYPE_MESSAGETIP: m_instance.CloseMessageTipUI(pathData.name); break; case UIPathData.UIType.TYPE_DEATH: m_instance.CloseDeathUI(pathData.name); break; case UIPathData.UIType.TYPE_GUIDE: m_instance.CloseBaseUI(pathData.name); break; case UIPathData.UIType.TYPE_LOADING: m_instance.ClosePopUI(pathData.name); break; case UIPathData.UIType.TYPE_CAMERA_TEXTURE: m_instance.CloseBaseUI(pathData.name); break; } if (pathData._IsBlurBackground) m_instance.HideBlurBackGround(pathData.name); if (_WakingUIs.Contains(pathData.name)) _WakingUIs.Remove(pathData.name); if (GuideLogic.Instance()) GuideLogic.Instance().UIClose(pathData.name); } public static void ClosePopUI() { var popKeys = new List(m_instance.m_dicMenuPopUI.Keys); for (var i = 0; i < popKeys.Count; ++i) //if (UIPathData.m_DicUIName[popKeys[i]].closeAnyway) //{ // m_instance.CloseMenuPopUI(popKeys[i]); //} //else if (!m_instance.m_dicMenuPopUI[popKeys[i]].activeSelf) m_instance.CloseMenuPopUI(popKeys[i]); popKeys = new List(m_instance.m_dicPopUI.Keys); for (var i = 0; i < popKeys.Count; ++i) //if (UIPathData.m_DicUIName[popKeys[i]].closeAnyway) //{ // m_instance.ClosePopUI(popKeys[i]); //} //else if (!m_instance.m_dicPopUI[popKeys[i]].activeSelf) m_instance.ClosePopUI(popKeys[i]); popKeys = new List(m_instance.m_dicTipUI.Keys); for (var i = 0; i < popKeys.Count; ++i) //if (UIPathData.m_DicUIName[popKeys[i]].closeAnyway) //{ // m_instance.CloseTipUI(popKeys[i]); //} //else if (!m_instance.m_dicTipUI[popKeys[i]].activeSelf) m_instance.CloseTipUI(popKeys[i]); } // public static void CloseAll() // { // // private Dictionary m_dicTipUI = new Dictionary(); // //private Dictionary m_dicBaseUI = new Dictionary(); // //private Dictionary m_dicPopUI = new Dictionary(); // //private Dictionary m_dicStoryUI = new Dictionary(); // //private Dictionary m_dicMenuPopUI = new Dictionary(); // //private Dictionary m_dicMessageUI = new Dictionary(); // //private Dictionary m_dicDeathUI = new Dictionary(); // //private Dictionary m_dicCacheUI = new Dictionary(); // // var popKeys = new List(m_instance.m_dicMenuPopUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.CloseMenuPopUI(popKeys[i]); // // popKeys = new List(m_instance.m_dicBaseUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.CloseBaseUI(popKeys[i]); // // popKeys = new List(m_instance.m_dicStoryUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.CloseStoryUI(popKeys[i]); // // popKeys = new List(m_instance.m_dicMessageUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.CloseMessageUI(popKeys[i]); // // popKeys = new List(m_instance.m_dicDeathUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.CloseDeathUI(popKeys[i]); // // popKeys = new List(m_instance.m_dicPopUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.ClosePopUI(popKeys[i]); // // popKeys = new List(m_instance.m_dicTipUI.Keys); // for (var i = 0; i < popKeys.Count; ++i) m_instance.CloseTipUI(popKeys[i]); // } public static T GetUIComponent(UIPathData uiData) { var UIObj = GetUIGameObject(uiData); if (UIObj != null) { var t = UIObj.GetComponent(); if (t != null) return t; } return default(T); } public static GameObject GetUIGameObject(UIPathData uiData) { if (null == m_instance) return null; return m_instance.GetUIObj(uiData); } public GameObject GetUIObj(UIPathData uiData) { if (uiData != null) { GameObject UIObj; Dictionary relativeDic = null; switch (uiData.uiType) { case UIPathData.UIType.TYPE_BASE: case UIPathData.UIType.TYPE_BASE_LEFT: case UIPathData.UIType.TYPE_BASE_RIGHT: relativeDic = m_dicBaseUI; break; case UIPathData.UIType.TYPE_POP: relativeDic = m_dicPopUI; break; case UIPathData.UIType.TYPE_STORY: relativeDic = m_dicStoryUI; break; case UIPathData.UIType.TYPE_TIP: relativeDic = m_dicTipUI; break; case UIPathData.UIType.TYPE_MENUPOP: relativeDic = m_dicMenuPopUI; break; case UIPathData.UIType.TYPE_MESSAGE: relativeDic = m_dicMessageUI; break; case UIPathData.UIType.TYPE_MESSAGETIP: relativeDic = m_dicMessageTipUI; break; case UIPathData.UIType.TYPE_DEATH: relativeDic = m_dicDeathUI; break; case UIPathData.UIType.TYPE_GUIDE: relativeDic = m_dicBaseUI; break; case UIPathData.UIType.TYPE_LOADING: relativeDic = m_dicPopUI; break; case UIPathData.UIType.TYPE_CAMERA_TEXTURE: relativeDic = m_dicBaseUI; break; } if (relativeDic != null) if (relativeDic.TryGetValue(uiData.name, out UIObj)) return UIObj; } return null; } private void DoAddUI(string uiName, GameObject curWindow, Hashtable hashParams) { var uiData = hashParams["UIData"] as UIPathData; // 不可能继续执行后续任何操作了 if (uiData == null) return; if (m_dicWaitLoad.Remove(uiData.name)) { GameObject showWindow = null; var isNewWin = false; var defaultHide = false; if (hashParams.ContainsKey("DefaultHide")) defaultHide = (bool) hashParams["DefaultHide"]; if (null != curWindow) { Transform parentRoot = null; Dictionary relativeDic = null; switch (uiData.uiType) { case UIPathData.UIType.TYPE_BASE: parentRoot = BaseUIRoot; relativeDic = m_dicBaseUI; break; case UIPathData.UIType.TYPE_BASE_LEFT: parentRoot = BaseUIRootLeft; relativeDic = m_dicBaseUI; break; case UIPathData.UIType.TYPE_BASE_RIGHT: parentRoot = BaseUIRootRight; relativeDic = m_dicBaseUI; break; case UIPathData.UIType.TYPE_POP: parentRoot = PopUIRoot; relativeDic = m_dicPopUI; break; case UIPathData.UIType.TYPE_STORY: parentRoot = StoryUIRoot; relativeDic = m_dicStoryUI; break; case UIPathData.UIType.TYPE_TIP: parentRoot = TipUIRoot; relativeDic = m_dicTipUI; break; case UIPathData.UIType.TYPE_MENUPOP: parentRoot = MenuPopUIRoot; relativeDic = m_dicMenuPopUI; break; case UIPathData.UIType.TYPE_MESSAGE: parentRoot = MessageUIRoot; relativeDic = m_dicMessageUI; break; case UIPathData.UIType.TYPE_DEATH: parentRoot = DeathUIRoot; relativeDic = m_dicDeathUI; break; case UIPathData.UIType.TYPE_MESSAGETIP: parentRoot = MessageTipRoot; relativeDic = m_dicMessageTipUI; break; case UIPathData.UIType.TYPE_GUIDE: parentRoot = GuideUIRoot; relativeDic = m_dicBaseUI; break; case UIPathData.UIType.TYPE_LOADING: parentRoot = LoadingUIRoot; relativeDic = m_dicPopUI; break; case UIPathData.UIType.TYPE_CAMERA_TEXTURE: parentRoot = CameraTexture; relativeDic = m_dicBaseUI; break; } if (uiData.uiType == UIPathData.UIType.TYPE_POP) if (!defaultHide) { OnLoadNewPopUI(m_dicPopUI, uiData); OnLoadNewPopUI(m_dicMenuPopUI, uiData); } if (uiData.uiType == UIPathData.UIType.TYPE_MENUPOP) if (!defaultHide) OnLoadNewPopUI(m_dicMenuPopUI, uiData); if (null != relativeDic && relativeDic.ContainsKey(uiData.name)) { relativeDic[uiData.name].SetActive(true); showWindow = relativeDic[uiData.name]; if (uiData.uiType >= UIPathData.UIType.TYPE_POP) SetIndexToBtm(showWindow.transform); //显示延迟的原因会导致模糊比UI出现的早,模糊放到第二次显示的时候 if (uiData._IsBlurBackground) ShowBlurBackGround(uiData.name); } else if (null != parentRoot && null != relativeDic) { isNewWin = true; showWindow = InitUIFromPrefab(curWindow, parentRoot.transform, (int) uiData.uiType, defaultHide); if (showWindow != null) { relativeDic.Add(uiData.name, showWindow); LogModule.DebugLog("Add UI:" + curWindow.name); if (uiData.uiType == UIPathData.UIType.TYPE_MENUPOP) LoadMenuSubUIShield(showWindow); if ((uiData.uiType == UIPathData.UIType.TYPE_MENUPOP || uiData.uiType == UIPathData.UIType.TYPE_POP) && uiData != UIInfo.ChatInfoRoot) { if (showWindow.GetComponent() == null) showWindow.AddComponent(); var animator = showWindow.AddComponent(); animator.runtimeAnimatorController = _AnimPopUI.runtimeAnimatorController; } } } if (uiData.uiType == UIPathData.UIType.TYPE_STORY) { //BaseUIRoot.gameObject.SetActive(false); //TipUIRoot.gameObject.SetActive(false); //PopUIRoot.gameObject.SetActive(false); //MenuPopUIRoot.gameObject.SetActive(false); //MessageUIRoot.gameObject.SetActive(false); //StoryUIRoot.gameObject.SetActive(true); } else if (uiData.uiType == UIPathData.UIType.TYPE_MENUPOP) { if (uiData.popAnim && !defaultHide) StartCoroutine(ShowPopAnimUI(showWindow)); } else if (uiData.uiType == UIPathData.UIType.TYPE_DEATH) { ReliveCloseOtherSubUI(); } else if (uiData.uiType == UIPathData.UIType.TYPE_POP) { if (uiData.popAnim && !defaultHide) StartCoroutine(ShowPopAnimUI(showWindow)); } } //if (null != fun) { var delOpenUI = hashParams["OnOpenUIDelegate"] as OnOpenUIDelegate; if (delOpenUI != null) { if (!isNewWin) { delOpenUI(showWindow != null, hashParams["Param"]); } else { if (showWindow != null && !defaultHide) StartCoroutine(ShowUILater(uiName, showWindow, delOpenUI, hashParams["Param"])); else delOpenUI(showWindow != null, hashParams["Param"]); } } } } if (!uiData.archive) LoadAssetBundle.Instance.UnloadAsset(uiData.path, uiData.name); } // 将物体置为本层级的最高层(遮住底层) private static void SetIndexToBtm(Transform child) { var childCount = child.parent.childCount; if (childCount > 0) child.SetSiblingIndex(childCount - 1); } public static GameObject InitUIFromPrefab(GameObject uiPrefab, Transform parentRoot, int uiType = -1, bool defaultHide = false) { GameObject newWindow = null; if (!defaultHide) { newWindow = Instantiate(uiPrefab, parentRoot.transform); newWindow.SetActive(true); } else { uiPrefab.SetActive(false); newWindow = Instantiate(uiPrefab, parentRoot.transform); newWindow.SetActive(false); } if (uiType == -1 && parentRoot != null) { var windowSigns = parentRoot.GetComponentsInParent(); if (windowSigns.Length > 0) windowSigns[0].SetWndDirty(); } if (null != newWindow) { if (uiType >= (int) UIPathData.UIType.TYPE_BASE_LEFT && uiType < (int) UIPathData.UIType.TYPE_MAX) { var wndSign = newWindow.GetComponent(); if (wndSign == null) wndSign = newWindow.AddComponent(); if (wndSign != null) wndSign.Init(uiType); } //添加点击(Button Toggle)音效控制脚本 var playSound = newWindow.GetComponent(); if (playSound == null) newWindow.AddComponent(); var rectTran = newWindow.GetComponent(); if (rectTran != null) { rectTran.localPosition = Vector3.zero; rectTran.localScale = Vector3.one; rectTran.offsetMax = Vector2.zero; rectTran.offsetMin = Vector2.zero; } } if (uiType >= (int) UIPathData.UIType.TYPE_POP) SetIndexToBtm(newWindow.transform); return newWindow; } private void ClosePopUI(string name) { StartCoroutine(OnClosePopUI(m_dicPopUI, name)); } private void CloseStoryUI(string name) { if (TryDestroyUI(m_dicStoryUI, name)) { BaseUIRoot.gameObject.SetActive(true); TipUIRoot.gameObject.SetActive(true); PopUIRoot.gameObject.SetActive(true); MenuPopUIRoot.gameObject.SetActive(true); MessageUIRoot.gameObject.SetActive(true); StoryUIRoot.gameObject.SetActive(true); } } private void CloseBaseUI(string name) { if (m_dicBaseUI.ContainsKey(name)) m_dicBaseUI[name].SetActive(false); } private void CloseTipUI(string name) { TryDestroyUI(m_dicTipUI, name); } private void CloseMenuPopUI(string name) { StartCoroutine(OnClosePopUI(m_dicMenuPopUI, name)); } private void CloseMessageUI(string name) { TryDestroyUI(m_dicMessageUI, name); } private void CloseMessageTipUI(string name) { TryDestroyUI(m_dicMessageTipUI, name); } private void CloseDeathUI(string name) { if (TryDestroyUI(m_dicDeathUI, name)) { // 关闭复活界面时 恢复节点的显示 m_instance.PopUIRoot.gameObject.SetActive(true); m_instance.MenuPopUIRoot.gameObject.SetActive(true); m_instance.TipUIRoot.gameObject.SetActive(true); } } private void LoadUI(UIPathData uiData, OnOpenUIDelegate delOpenUI = null, object param1 = null, bool defaultHide = false) { GameObject curWindow = null; var hash = new Hashtable { {"UIData", uiData}, {"OnOpenUIDelegate", delOpenUI}, {"Param", param1}, {"DefaultHide", defaultHide} }; LoadAssetBundle.Instance.LoadUI(uiData.path, uiData.name, DoAddUI, hash, uiData.archive); } private static void LoadMenuSubUIShield(GameObject newWindow) { //GameObject MenuSubUIShield = ResourceManager.InstantiateResource("Prefab/UI/MenuSubUIShield") as GameObject; //if (MenuSubUIShield == null) //{ // LogModule.ErrorLog("can not open MenuSubUIShield path not found"); // return; //} //MenuSubUIShield.transform.parent = newWindow.transform; //MenuSubUIShield.transform.localPosition = Vector3.zero; //MenuSubUIShield.transform.localScale = Vector3.one; } private static void LoadPopUIShield(GameObject newWindow) { if (GameManager.gameManager.RunningScene == (int) GameDefine_Globe.SCENE_DEFINE.SCENE_LOGIN || GameManager.gameManager.RunningScene == (int) GameDefine_Globe.SCENE_DEFINE.SCENE_LOADINGSCENE) return; var PopUIBlack = ResourceManager.InstantiateResource("Prefab/UI/PopUIBlack"); if (PopUIBlack == null) { LogModule.ErrorLog("can not open PopUIBlack path not found"); return; } PopUIBlack.transform.parent = newWindow.transform; PopUIBlack.transform.localPosition = Vector3.zero; PopUIBlack.transform.localScale = Vector3.one; } private GameObject AddObjToRoot(string name) { var obj = new GameObject(); obj.transform.parent = transform; obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; obj.name = name; return obj; } private bool SubUIShow() { if (m_dicPopUI.Count + m_dicStoryUI.Count + m_dicTipUI.Count + m_dicMenuPopUI.Count > 0) return true; return false; } public static bool IsSubUIShow() { if (m_instance != null) return m_instance.SubUIShow(); return false; } private static void ReliveCloseOtherSubUI() { // 关闭所有PopUI var uiKeyList = new List(); foreach (var pair in m_instance.m_dicPopUI) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) m_instance.ClosePopUI(uiKeyList[i]); uiKeyList.Clear(); // 关闭所有MenuPopUI foreach (var pair in m_instance.m_dicMenuPopUI) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) m_instance.CloseMenuPopUI(uiKeyList[i]); uiKeyList.Clear(); // 关闭所有TipUI foreach (var pair in m_instance.m_dicTipUI) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) m_instance.CloseTipUI(uiKeyList[i]); uiKeyList.Clear(); // 关闭所有除CentreNotice以外的MessageUI MessageUIRoot节点保留不隐藏 foreach (var pair in m_instance.m_dicMessageUI) if (UIPathData.m_DicUIName[pair.Key].closeAnyway || !pair.Value.gameObject.activeSelf) //if (!pair.Key.Contains("CentreNotice")) uiKeyList.Add(pair.Key); for (var i = 0; i < uiKeyList.Count; i++) m_instance.CloseMessageUI(uiKeyList[i]); uiKeyList.Clear(); } public static void NewPlayerGuideCloseSubUI() { // 关闭所有PopUI foreach (var pair in m_instance.m_dicPopUI) { m_instance.ClosePopUI(pair.Key); break; } // 关闭所有MenuPopUI foreach (var pair in m_instance.m_dicMenuPopUI) { m_instance.CloseMenuPopUI(pair.Key); break; } // 关闭所有TipUI foreach (var pair in m_instance.m_dicTipUI) { m_instance.CloseTipUI(pair.Key); break; } // 关闭所有MessageUI // foreach (KeyValuePair pair in m_instance.m_dicMessageUI) // { // m_instance.CloseMessageUI(pair.Key); // break; // } } private void AddLoadDicRefCount(string pathName) { if (m_dicWaitLoad.ContainsKey(pathName)) m_dicWaitLoad[pathName]++; else m_dicWaitLoad.Add(pathName, 1); } private bool RemoveLoadDicRefCount(string pathName) { if (!m_dicWaitLoad.ContainsKey(pathName)) return false; m_dicWaitLoad[pathName]--; if (m_dicWaitLoad[pathName] <= 0) m_dicWaitLoad.Remove(pathName); return true; } public void DestroyUI(UIPathData pathData, GameObject obj) { Destroy(obj); LoadAssetBundle.Instance.UnloadAsset(pathData.path, pathData.name); //StartCoroutine(DestroyUIDelay()); } public void DestroyRawUi(string uiName, GameObject obj) { Destroy(obj); var bundleName = LoadAssetBundle.BUNDLE_PATH_UI + uiName; LoadAssetBundle.Instance.UnloadAsset(bundleName, uiName); } // IEnumerator DestroyUIDelay() // { // yield return null; // Resources.UnloadUnusedAssets(); // } private void OnLoadNewPopUI(Dictionary curList, UIPathData pathData) { if (curList == null) return; var objToRemove = new List(); if (curList.Count > 0) { objToRemove.Clear(); foreach (var objs in curList) { if (pathData.name == objs.Key) continue; //objs.Value.SetActive(false); objToRemove.Add(objs.Key); } for (var i = 0; i < objToRemove.Count; i++) StartCoroutine(OnClosePopUI(curList, objToRemove[i])); } } private IEnumerator OnClosePopUI(Dictionary curList, string curName) { if (curList == null) yield break; if (!curList.ContainsKey(curName)) yield break; if (!UIPathData.m_DicUIName[curName].popAnim) { if (TryDestroyUI(curList, curName)) { } yield break; } HidePopAnimUI(curList[curName]); yield return new WaitForSeconds(_AnimTime); //_BackImg.enabled = false; if (TryDestroyUI(curList, curName)) { } } private bool TryDestroyUI(Dictionary curList, string curName) { if (curList == null) return false; GameObject target; if (!curList.TryGetValue(curName, out target)) return false; curList.Remove(curName); UIPathData pathData; if (UIPathData.m_DicUIName.TryGetValue(curName, out pathData)) { if (pathData.isDestroyOnUnloasScene) { DestroyUI(pathData, target); } else { target.SetActive(false); m_dicCacheUI.Add(curName, target); } } else { DestroyRawUi(curName, target); } HideBlurBackGround(curName); return true; } #region UI instance wake up public static List _WakingUIs = new List(); public IEnumerator ShowUILater(string uiname, GameObject showUI, OnOpenUIDelegate delFunc, object param) { _WakingUIs.Add(uiname); showUI.transform.localPosition = new Vector3(10000, 0, 0); yield return null; while (showUI != null && !showUI.activeSelf) { if (!_WakingUIs.Contains(uiname)) { showUI.transform.localPosition = new Vector3(0, 0, 0); yield break; } yield return null; } if (showUI != null && showUI.transform != null) showUI.transform.localPosition = Vector3.zero; delFunc(showUI, param); _WakingUIs.Remove(uiname); } #endregion #region cameraPos private RectTransform _UICanvasRect; public RectTransform UICanvasRect { get { if (_UICanvasRect == null) _UICanvasRect = gameObject.GetComponent(); return _UICanvasRect; } } public Camera GetWorldCamera() { Camera result = null; if (SceneLogic.CameraController != null) result = SceneLogic.CameraController.MainCamera; if (result == null) result = Camera.main; return result; } /// /// 视野坐标点转换为Ui坐标点 /// /// 视野坐标位置 /// 是否以中心为原点,否会用左下角为原点 public Vector3 ViewPointToUiPoint(Vector3 viewPoint, bool relativeToCenter = true) { if (relativeToCenter) viewPoint -= new Vector3(0.5f, 0.5f, 0f); return new Vector3(viewPoint.x * UICanvasRect.sizeDelta.x, viewPoint.y * UICanvasRect.sizeDelta.y, viewPoint.z); } /// /// 世界坐标点转化为Ui坐标点 /// /// 世界坐标位置 /// 是否以中心为原点,否会用左下角为原点 public Vector3 WorldToUiPoint(Vector3 worldPos, bool relativeToCenter = true) { //if (GameManager.gameManager.PlayerDataPool.IsInWeddingCar // && GameManager.gameManager.PlayerDataPool.WCCamera) // return WorldPosToUIPosByWCCamera(worldPos, relativeToCenter); var viewPoint = GetWorldCamera().WorldToViewportPoint(worldPos); //1024 return ViewPointToUiPoint(viewPoint, relativeToCenter); } //婚车相机替带世界相机 //public Vector3 WorldPosToUIPosByWCCamera(Vector3 worldPos, bool relativeToCenter = true) //{ // if (GameManager.gameManager.PlayerDataPool.IsInWeddingCar // && GameManager.gameManager.PlayerDataPool.WCCamera) // { // var viewPoint = GameManager.gameManager.PlayerDataPool.WCCamera.WorldToViewportPoint(worldPos); //1024 // return ViewPointToUiPoint(viewPoint, relativeToCenter); // } // else // return new Vector3(); //} /// /// 屏幕坐标点转化为Ui坐标点 /// /// 屏幕坐标位置 /// 是否以中心为原点,否会用左下角为原点 public Vector2 ScreenPointToUiPoint(Vector2 screenPoint, bool relativeToCenter = true) { var viewPoint = new Vector3(screenPoint.x / Screen.width, screenPoint.y / Screen.height, 0f); return ViewPointToUiPoint(viewPoint, relativeToCenter); } #endregion #region ui anim public Image _BackImg; public Animator _AnimLeft; public Animator _AnimRight; public Animator _AnimPopUI; public float _AnimTime = 0.01f; private bool _IsBaseUIShow = true; public void ShowBaseUI() { //_AnimLeft.Play("ShowLeft"); //_AnimRight.Play("ShowRight"); //_IsBaseUIShow = true; } public void HideBaseUI() { //_AnimLeft.Play("HideLeft"); //_AnimRight.Play("HideRight"); //_IsBaseUIShow = false; } public IEnumerator ShowPopAnimUI(GameObject popUI) { //Animator anim = popUI.GetComponent(); //if (anim != null) //{ // anim.Play("Show"); //} //if (_IsBaseUIShow) //{ // HideBaseUI(); //} yield return new WaitForSeconds(_AnimTime); //_BackImg.enabled = true; } public void HidePopAnimUI(GameObject popUI) { //Animator anim = popUI.GetComponent(); //if (anim != null) //{ // anim.Play("Hide"); //} //if (!_IsBaseUIShow) //{ // ShowBaseUI(); //} } #endregion #region rayCast //防死循环 private bool _RayCasting; public void RayCastBebind(PointerEventData eventData) { //if (_RayCasting) // return; _RayCasting = true; var raycastResults = new List(); EventSystem.current.RaycastAll(eventData, raycastResults); if (raycastResults.Count > 0) { var pointClick = raycastResults[0].gameObject.GetComponent(); //if (ExecuteEvents.CanHandleEvent(raycastResults[0].gameObject)) { LogModule.DebugLog("RayCastItem:" + raycastResults[0].gameObject.name); ExecuteEvents.ExecuteHierarchy(raycastResults[0].gameObject, eventData, (x, y) => x.OnPointerClick(eventData)); } } _RayCasting = false; } #endregion #region ui background blur public UICameraCopy _UIBackGround; private readonly List _ShowBlurUIs = new List(); public void ShowBlurBackGround(string showUIPath) { if (_ShowBlurUIs.Count == 0) { _UIBackGround.gameObject.SetActive(true); _UIBackGround.ShowBlur(); } if (!_ShowBlurUIs.Contains(showUIPath)) _ShowBlurUIs.Add(showUIPath); } public void HideBlurBackGround(string showUIPath) { if (_ShowBlurUIs.Contains(showUIPath)) _ShowBlurUIs.Remove(showUIPath); if (_ShowBlurUIs.Count == 0) _UIBackGround.HideBlur(); } #endregion }