Files
JJBB/Assets/Project/Script/GUI/Base/UISaveArea.cs

193 lines
6.0 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
public class UISaveArea : MonoBehaviour
{
public static UnityEvent onOrientationChange = new UnityEvent();
public static UnityEvent onResolutionChange = new UnityEvent();
public static bool isLandscape { get; private set; }
private static List<UISaveArea> helpers = new List<UISaveArea>();
private static bool screenChangeVarsInitialized = false;
private static ScreenOrientation lastOrientation = ScreenOrientation.Portrait;
private static Vector2 lastResolution = Vector2.zero;
private static Rect lastSafeArea = Rect.zero;
private Canvas canvas;
private RectTransform rectTransform;
private RectTransform safeAreaTransform;
void Awake()
{
if (!helpers.Contains(this))
helpers.Add(this);
canvas = GetComponent<Canvas>();
rectTransform = GetComponent<RectTransform>();
safeAreaTransform = transform as RectTransform;
if (!screenChangeVarsInitialized)
{
lastOrientation = Screen.orientation;
lastResolution.x = Screen.width;
lastResolution.y = Screen.height;
lastSafeArea = Screen.safeArea;
screenChangeVarsInitialized = true;
}
}
void Start()
{
ApplySafeArea();
}
void Update()
{
if (helpers[0] != this)
return;
if (Application.isMobilePlatform)
{
if (Screen.orientation != lastOrientation)
{
OrientationChanged();
SafeAreaChanged();
}
if (Screen.safeArea != lastSafeArea)
SafeAreaChanged();
}
else
{
//resolution of mobile devices should stay the same always, right?
// so this check should only happen everywhere else
if (Screen.width != lastResolution.x || Screen.height != lastResolution.y)
ResolutionChanged();
}
}
void ApplySafeArea()
{
if (safeAreaTransform == null)
return;
var safeArea = Screen.safeArea;
var anchorMin = safeArea.position;
var anchorMax = safeArea.position + safeArea.size;
// bool IsIphoneXDevice = false;
// string modelStr = SystemInfo.deviceModel;
//#if UNITY_IOS
// // iPhoneX:"iPhone10,3","iPhone10,6" iPhoneXR:"iPhone11,8" iPhoneXS:"iPhone11,2" iPhoneXS Max:"iPhone11,6"
// IsIphoneXDevice = modelStr.Equals("iPhone10,3") || modelStr.Equals("iPhone10,6") || modelStr.Equals("iPhone11,8") || modelStr.Equals("iPhone11,2") || modelStr.Equals("iPhone11,6");
//#endif
// if (IsIphoneXDevice)
// {
// if (Screen.orientation == ScreenOrientation.LandscapeLeft)
// {
// anchorMin = Vector2.zero;
// }
// else if (Screen.orientation == ScreenOrientation.LandscapeLeft)
// {
// anchorMax = new Vector2(canvas.pixelRect.width, canvas.pixelRect.height);
// }
// }
anchorMin.x /= canvas.pixelRect.width;
//anchorMin.y /= canvas.pixelRect.height;
anchorMin.y = 0;
anchorMax.x /= canvas.pixelRect.width;
//anchorMax.y /= canvas.pixelRect.height;
anchorMax.y = 1;
safeAreaTransform.anchorMin = anchorMin;
safeAreaTransform.anchorMax = anchorMax;
// Debug.Log(
// "ApplySafeArea:" +
// "\n Screen.orientation: " + Screen.orientation +
// #if UNITY_IOS
// "\n Device.generation: " + UnityEngine.iOS.Device.generation.ToString() +
// #endif
// "\n Screen.safeArea.position: " + Screen.safeArea.position.ToString() +
// "\n Screen.safeArea.size: " + Screen.safeArea.size.ToString() +
// "\n Screen.width / height: (" + Screen.width.ToString() + ", " + Screen.height.ToString() + ")" +
// "\n canvas.pixelRect.size: " + canvas.pixelRect.size.ToString() +
// "\n anchorMin: " + anchorMin.ToString() +
// "\n anchorMax: " + anchorMax.ToString());
}
void OnDestroy()
{
if (helpers != null && helpers.Contains(this))
helpers.Remove(this);
}
private static void OrientationChanged()
{
//Debug.Log("Orientation changed from " + lastOrientation + " to " + Screen.orientation + " at " + Time.time);
lastOrientation = Screen.orientation;
lastResolution.x = Screen.width;
lastResolution.y = Screen.height;
isLandscape = lastOrientation == ScreenOrientation.LandscapeLeft || lastOrientation == ScreenOrientation.LandscapeRight || lastOrientation == ScreenOrientation.Landscape;
onOrientationChange.Invoke();
}
private static void ResolutionChanged()
{
if (lastResolution.x == Screen.width && lastResolution.y == Screen.height)
return;
//Debug.Log("Resolution changed from " + lastResolution + " to (" + Screen.width + ", " + Screen.height + ") at " + Time.time);
lastResolution.x = Screen.width;
lastResolution.y = Screen.height;
isLandscape = Screen.width > Screen.height;
onResolutionChange.Invoke();
}
private static void SafeAreaChanged()
{
if (lastSafeArea == Screen.safeArea)
return;
//Debug.Log("Safe Area changed from " + lastSafeArea + " to " + Screen.safeArea.size + " at " + Time.time);
lastSafeArea = Screen.safeArea;
for (int i = 0; i < helpers.Count; i++)
{
helpers[i].ApplySafeArea();
}
}
public static Vector2 GetCanvasSize()
{
return helpers[0].rectTransform.sizeDelta;
}
public static Vector2 GetSafeAreaSize()
{
for (int i = 0; i < helpers.Count; i++)
{
if (helpers[i].safeAreaTransform != null)
{
return helpers[i].safeAreaTransform.sizeDelta;
}
}
return GetCanvasSize();
}
}