160 lines
4.6 KiB
C#
160 lines
4.6 KiB
C#
|
||
using UnityEngine;
|
||
using System;
|
||
using System.IO;
|
||
using Cysharp.Threading.Tasks;
|
||
using UnityEngine.UI;
|
||
using Object = UnityEngine.Object;
|
||
using System.Reflection;
|
||
using System.Linq;
|
||
using PhxhSDK;
|
||
|
||
public partial class UIRoot : SingletonMono<UIRoot>
|
||
{
|
||
public Canvas RootCanvas;
|
||
// 所有UI的根节点
|
||
public GameObject Root;
|
||
public Camera UICamera;
|
||
|
||
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));
|
||
|
||
}
|
||
|
||
private void LateUpdate()
|
||
{
|
||
if (isDirty)
|
||
{
|
||
isDirty = false;
|
||
foreach (Text text in FindObjectsOfType<Text>())
|
||
{
|
||
if (text.font == dirtyFont)
|
||
{
|
||
text.FontTextureChanged();
|
||
}
|
||
}
|
||
dirtyFont = null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Creates the window.
|
||
/// </summary>
|
||
/// <returns>The window.</returns>
|
||
/// <param name="windowName">UI预设名</param>
|
||
/// <param name="type">UI类型</param>
|
||
/// <param name="layer">UI层级</param>
|
||
public async UniTask<UIWindow> CreateWindow(string windowName, 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;
|
||
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;
|
||
if (window == null)
|
||
{
|
||
DebugUtil.LogError("Cant find UIWindow: {0}, check the name or remove any outter namespace.", className);
|
||
return null;
|
||
}
|
||
|
||
return window;
|
||
}
|
||
|
||
async UniTask<GameObject> LoadObj(string windowName)
|
||
{
|
||
GameObject uiPrefab = null;
|
||
var path = string.Format(UIManager.UI_PREFAB_PATH, windowName);
|
||
uiPrefab = await AssetManager.Instance.LoadAssetAsync<GameObject>(path);
|
||
return uiPrefab;
|
||
}
|
||
|
||
private async UniTask<GameObject> 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<GameObject>(Path.Combine("Loading",windowName + "_Pad"));
|
||
// }
|
||
// }
|
||
|
||
if (uiPrefab == null)
|
||
{
|
||
uiPrefab = await LoadObj(windowName);
|
||
if (uiPrefab == null) // 增加一层兼容。如果ab包里加载不到,就去Resources目录里加载
|
||
{
|
||
uiPrefab = Resources.Load<GameObject>(Path.Combine("Loading",windowName));
|
||
}
|
||
}
|
||
|
||
return uiPrefab;
|
||
}
|
||
|
||
|
||
public Vector2 GetScreenCanvasScale()
|
||
{
|
||
var rectTransform = Root.GetComponent<RectTransform>();
|
||
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<CanvasGroup>();
|
||
cg.interactable = b;
|
||
cg.blocksRaycasts = b;
|
||
}
|
||
|
||
public void SwitchCanvasScaler(ECanvasScaler scaler)
|
||
{
|
||
CanvasScaler cs = RootCanvas.transform.GetComponent<CanvasScaler>();
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
} |