2025-09-05 19:46:30 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
using AlicizaX.Resource.Runtime;
|
|
|
|
|
|
using AlicizaX;
|
|
|
|
|
|
using AlicizaX.Timer.Runtime;
|
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
|
|
|
|
|
|
|
namespace AlicizaX.UI.Runtime
|
|
|
|
|
|
{
|
|
|
|
|
|
[DisallowMultipleComponent]
|
|
|
|
|
|
[AddComponentMenu("Game Framework/UI")]
|
|
|
|
|
|
[UnityEngine.Scripting.Preserve]
|
|
|
|
|
|
public sealed partial class UIComponent : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
[SerializeField] private GameObject uiRoot = null;
|
|
|
|
|
|
[SerializeField] private bool _isOrthographic = true;
|
|
|
|
|
|
private Transform _instanceRoot = null;
|
|
|
|
|
|
|
2026-03-26 16:14:05 +08:00
|
|
|
|
private IUIService _uiService;
|
2025-09-05 19:46:30 +08:00
|
|
|
|
|
|
|
|
|
|
public const int UIHideLayer = 2; // Ignore Raycast
|
|
|
|
|
|
public const int UIShowLayer = 5; // UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
|
_uiService = AppServices.App.Register(new UIService());
|
2025-09-05 19:46:30 +08:00
|
|
|
|
if (uiRoot == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new GameFrameworkException("UIRoot Prefab is invalid.");
|
|
|
|
|
|
}
|
2026-03-31 17:25:20 +08:00
|
|
|
|
|
2025-09-05 19:46:30 +08:00
|
|
|
|
GameObject obj = Instantiate(uiRoot, Vector3.zero, Quaternion.identity);
|
|
|
|
|
|
obj.name = "------UI Root------";
|
|
|
|
|
|
_instanceRoot = obj.transform;
|
|
|
|
|
|
Object.DontDestroyOnLoad(_instanceRoot);
|
2026-03-31 17:25:20 +08:00
|
|
|
|
_uiService.Initialize(_instanceRoot, _isOrthographic);
|
2025-09-05 19:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
|
_uiService.SetTimerService(AppServices.Require<ITimerService>());
|
2025-09-05 19:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 设置安全区域
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-31 17:25:20 +08:00
|
|
|
|
/// 应用屏幕安全区域,适配刘海屏等显示区域。
|
2025-09-05 19:46:30 +08:00
|
|
|
|
/// </summary>
|
2026-03-31 17:25:20 +08:00
|
|
|
|
/// <param name="safeRect">安全区域。</param>
|
2025-09-05 19:46:30 +08:00
|
|
|
|
public void ApplyScreenSafeRect(Rect safeRect)
|
|
|
|
|
|
{
|
2026-03-26 16:14:05 +08:00
|
|
|
|
CanvasScaler scaler = _uiService.UICanvasRoot.GetComponent<CanvasScaler>();
|
2025-09-05 19:46:30 +08:00
|
|
|
|
if (scaler == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.Error($"Not found {nameof(CanvasScaler)} !");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convert safe area rectangle from absolute pixels to UGUI coordinates
|
|
|
|
|
|
float rateX = scaler.referenceResolution.x / Screen.width;
|
|
|
|
|
|
float rateY = scaler.referenceResolution.y / Screen.height;
|
|
|
|
|
|
float posX = (int)(safeRect.position.x * rateX);
|
|
|
|
|
|
float posY = (int)(safeRect.position.y * rateY);
|
|
|
|
|
|
float width = (int)(safeRect.size.x * rateX);
|
|
|
|
|
|
float height = (int)(safeRect.size.y * rateY);
|
|
|
|
|
|
|
|
|
|
|
|
float offsetMaxX = scaler.referenceResolution.x - width - posX;
|
|
|
|
|
|
float offsetMaxY = scaler.referenceResolution.y - height - posY;
|
|
|
|
|
|
|
2026-03-31 17:25:20 +08:00
|
|
|
|
// 注意:安全区域坐标系原点为左下角。
|
2026-03-26 16:14:05 +08:00
|
|
|
|
var rectTrans = _uiService.UICanvasRoot.transform as RectTransform;
|
2025-09-05 19:46:30 +08:00
|
|
|
|
if (rectTrans != null)
|
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
|
rectTrans.offsetMin = new Vector2(posX, posY); // 锚点状态下的屏幕左下角偏移量。
|
|
|
|
|
|
rectTrans.offsetMax = new Vector2(-offsetMaxX, -offsetMaxY); // 锚点状态下的屏幕右上角偏移量。
|
2025-09-05 19:46:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 17:25:38 +08:00
|
|
|
|
|
2025-09-05 19:46:30 +08:00
|
|
|
|
public void SimulateIPhoneXNotchScreen()
|
|
|
|
|
|
{
|
|
|
|
|
|
Rect rect;
|
|
|
|
|
|
if (Screen.height > Screen.width)
|
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
|
// 竖屏
|
2025-09-05 19:46:30 +08:00
|
|
|
|
float deviceWidth = 1125;
|
|
|
|
|
|
float deviceHeight = 2436;
|
|
|
|
|
|
rect = new Rect(0f / deviceWidth, 102f / deviceHeight, 1125f / deviceWidth, 2202f / deviceHeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-03-31 17:25:20 +08:00
|
|
|
|
// 横屏
|
2025-09-05 19:46:30 +08:00
|
|
|
|
float deviceWidth = 2436;
|
|
|
|
|
|
float deviceHeight = 1125;
|
|
|
|
|
|
rect = new Rect(132f / deviceWidth, 63f / deviceHeight, 2172f / deviceWidth, 1062f / deviceHeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Rect safeArea = new Rect(Screen.width * rect.x, Screen.height * rect.y, Screen.width * rect.width, Screen.height * rect.height);
|
|
|
|
|
|
ApplyScreenSafeRect(safeArea);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|