using UnityEngine; using System; using System.Collections.Generic; using System.IO; using Cysharp.Threading.Tasks; using UnityEngine.UI; using Object = UnityEngine.Object; using PhxhSDK; public partial class UIRoot : SingletonMono { public Canvas RootCanvas; // 所有UI的根节点 public GameObject Root; public Camera UICamera; public Camera UITopCamera; public GameObject UITopRoot; /// /// UI层级根节点字典(私有,通过方法访问) /// private Dictionary _rootDic = new(6); // [Range(-5, 5)] // public float offsetX = 1.32f; // [Range(-5, 5)] // public float offsetY = 1.26f; // [Range(-5, 5)] // public float offsetZ = 0.83f; // Loading节点 //public GameObject mUILoading; // 尝试修复花屏的问题 bool isDirty = false; Font dirtyFont = null; public void Awake() { Object.DontDestroyOnLoad(this.gameObject); // Font.textureRebuilt += delegate (Font font1) // { // isDirty = true; // dirtyFont = font1; // }; // var assemblies = AppDomain.CurrentDomain.GetAssemblies(); //mUILoading.AddComponent(typeof(SliderZero)); var backGround = transform.Find("UIBackGround"); var normal = transform.Find("UINormal"); var foreGround = transform.Find("UIForeGround"); var tips = transform.Find("UITips"); var guide = transform.Find("UIGuide"); var system = transform.Find("UISystem"); if (backGround) { _rootDic.Add(UICanvasLayer.BackGround, backGround.gameObject); } if (normal) { _rootDic.Add(UICanvasLayer.Normal, normal.gameObject); } if (foreGround) { _rootDic.Add(UICanvasLayer.ForeGround, foreGround.gameObject); } if (tips) { _rootDic.Add(UICanvasLayer.Tips, tips.gameObject); } if (guide) { _rootDic.Add(UICanvasLayer.Guide, guide.gameObject); } if (system) { _rootDic.Add(UICanvasLayer.System, system.gameObject); } } // private void LateUpdate() // { // if (isDirty) // { // isDirty = false; // foreach (Text text in FindObjectsOfType()) // { // if (text.font == dirtyFont) // { // text.FontTextureChanged(); // } // } // dirtyFont = null; // } // } /// /// Creates the window. /// /// The window. /// UI预设名 /// UI类型 /// UI层级 public async UniTask CreateWindow(string windowName, UICanvasLayer layer = UICanvasLayer.Normal) { GameObject uiPrefab = await LoadPrefab(windowName); if (uiPrefab is null) { DebugUtil.LogError("{0}.CreateWindow, cannot find window resource : {1}", GetType(), windowName); return null; } UIWindow window = null; var root = _rootDic[layer]; GameObject obj = Instantiate(uiPrefab, root.transform, false) as GameObject; string[] dirs = windowName.Split('/'); string className = dirs[dirs.Length - 1] + "Controller"; //window = obj.AddComponent(CommonUtilsFramework.GamePlayAssembly.GetType(className)) as UIWindow; window = CommonUtilsFramework.GamePlayAssembly.CreateInstance(className) as UIWindow; UIManager.BindWindowObj(window, obj); if (window == null) { DebugUtil.LogError("Cant find UIWindow: {0}, check the name or remove any outter namespace.", className); return null; } return window; } async UniTask LoadObj(string windowName) { GameObject uiPrefab = null; DebugUtil.Log($"LoadWindow:{windowName}"); var path = string.Format(UIManager.UI_PREFAB_PATH, windowName); uiPrefab = await AssetManager.Instance.LoadAssetAsync(path); return uiPrefab; } private async UniTask LoadPrefab(string wName) { string windowName = wName; GameObject uiPrefab = null; // if (CommonUtils.IsLE_16_10()) // { // uiPrefab = LoadObj(windowName + "_Pad"); // if (uiPrefab == null)// 增加一层兼容。如果ab包里加载不到,就去Resources目录里加载 // { // uiPrefab = Resources.Load(Path.Combine("Loading",windowName + "_Pad")); // } // } if (uiPrefab is null) { uiPrefab = await LoadObj(windowName); if (uiPrefab is null) // 增加一层兼容。如果ab包里加载不到,就去Resources目录里加载 { uiPrefab = Resources.Load(Path.Combine("Loading",windowName)); } } return uiPrefab; } public Vector2 GetScreenCanvasScale() { var rectTransform = Root.GetComponent(); var screenSize = new Vector2(Screen.width, Screen.height); var rect = rectTransform.rect; return new Vector2(screenSize.x / rect.width, screenSize.y / rect.height); } public void EnableTouch(bool b) { var cg = RootCanvas.GetComponent(); cg.interactable = b; cg.blocksRaycasts = b; } /// /// 是否激活射线检测 /// /// public void IsEnableGraphicRaycaster(bool isEnable) { GraphicRaycaster graphicRaycaster = Root.GetComponent(); if (graphicRaycaster is null) return; graphicRaycaster.enabled = isEnable; } /// /// 新手引导是否激活射线检测(新手引导用) /// /// public void IsEnableGraphicRaycasterByGuide(bool isEnable) { foreach (var kv in UIManager.Instance.mMemoryWindows) { if (UINameConst.UIFightScene == kv.Key) continue; if (UINameConst.UINoticeOne == kv.Key) continue; if (UINameConst.UINoticeTwo == kv.Key) continue; if (UINameConst.UI_GuideTip == kv.Key) continue; if (UINameConst.UI_Guide == kv.Key) continue; kv.Value.GraphicRaycasterManager.IsEnableGraphicRaycaster = isEnable; } } public void EnterUITopState() { UITopCamera.gameObject.SetActive(true); } public void ExitUITopState() { //UITopCamera.gameObject.SetActive(false); } public void AddUI2UITopRoot(GameObject obj, bool isFirst = false) { if (UITopRoot) { obj.transform.SetParent(UITopRoot.transform); if (isFirst) obj.transform.SetAsFirstSibling(); } } /// /// 设置UI窗口的层级 /// /// UI窗口 /// 目标层级 public void SetLayer(UIWindow window, UICanvasLayer layer) { if (window == null) { DebugUtil.LogWarning("[UIRoot] UIWindow is null"); return; } SetLayer(window.gameObject, layer); } /// /// 设置UI对象的层级(用于非窗口UI元素,如血条、行动条等动态UI元素) /// /// UI对象 /// 目标层级 public void SetLayer(GameObject go, UICanvasLayer layer) { if (go == null) { DebugUtil.LogWarning("[UIRoot] GameObject is null"); return; } if (!_rootDic.TryGetValue(layer, out GameObject goRoot)) { DebugUtil.LogWarning($"[UIRoot] Cannot find canvas for layer: {layer}"); return; } go.transform.SetParent(goRoot.transform, false); } /// /// 获取指定层级的根节点 /// /// UI层级 /// 对应的根节点GameObject,如果不存在返回null public GameObject GetLayerRoot(UICanvasLayer layer) { return _rootDic.TryGetValue(layer, out var root) ? root : null; } /// /// 获取所有根节点的只读字典(用于初始化等场景) /// /// 只读的根节点字典 public IReadOnlyDictionary GetAllLayerRoots() { return _rootDic; } }