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; public Dictionary RootDic = new(5); // [Range(-5, 5)] // public float offsetX = 1.32f; // [Range(-5, 5)] // public float offsetY = 1.26f; // [Range(-5, 5)] // public float offsetZ = 0.83f; public enum ECanvasScaler { S1365_768, S1334_750, } // 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 dynamicRoot0 = transform.Find("UIDynamicRoot0"); var staticRoot1 = transform.Find("UIRoot"); var dynamicRoot2 = transform.Find("UIDynamicRoot2"); var staticRoot3 = transform.Find("UIStaticRoot3"); var dynamicRoot4 = transform.Find("UIDynamicRoot4"); if (dynamicRoot0) { RootDic.Add(UIManager.ECanvasRoot.Dynamic0, dynamicRoot0.gameObject); } if (staticRoot1) { RootDic.Add(UIManager.ECanvasRoot.Static1, staticRoot1.gameObject); } if (dynamicRoot2) { RootDic.Add(UIManager.ECanvasRoot.Dynamic2, dynamicRoot2.gameObject); } if (staticRoot3) { RootDic.Add(UIManager.ECanvasRoot.Static3, staticRoot3.gameObject); } if (dynamicRoot4) { RootDic.Add(UIManager.ECanvasRoot.Dynamic4, dynamicRoot4.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, UIManager.ECanvasRoot eCanvasRoot = UIManager.ECanvasRoot.Static1, UIWindowLayer layer = UIWindowLayer.Normal) { GameObject uiPrefab = await LoadPrefab(windowName); if (uiPrefab == null) { DebugUtil.LogError("{0}.CreateWindow, cannot find window resource : {1}", GetType(), windowName); return null; } UIWindow window = null; var root = RootDic[eCanvasRoot]; 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 == null) { uiPrefab = await LoadObj(windowName); if (uiPrefab == 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 (null == graphicRaycaster) 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 (!kv.Value.gameObject.TryGetComponent(out GraphicRaycaster graphicRaycaster)) graphicRaycaster = kv.Value.gameObject.AddComponent(); GraphicRaycaster[] graphicRaycasters = kv.Value.gameObject.GetComponentsInChildren(); foreach (GraphicRaycaster gr in graphicRaycasters) { gr.enabled = isEnable; } } } public void SwitchCanvasScaler(ECanvasScaler scaler) { CanvasScaler cs = RootCanvas.transform.GetComponent(); if (cs != null) { switch (scaler) { case ECanvasScaler.S1365_768: cs.referenceResolution = new Vector2(1365, 768); break; case ECanvasScaler.S1334_750: default: cs.referenceResolution = new Vector2(1334, 750); break; } } } public void EnterUITopState() { UITopCamera.gameObject.SetActive(true); } public void ExitUITopState() { UITopCamera.gameObject.SetActive(false); } public void AddUI2UITopRoot(GameObject obj) { if (UITopRoot) { obj.transform.SetParent(UITopRoot.transform); } } }