CanvasHelper resizes a RectTransform to iPhone X's safe area - Unity Forum
UI适配可以拿手机的安全区域对Canvas进行等比修改
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; [RequireComponent(typeof(Canvas))] public class CanvasHelper : MonoBehaviour { private static List<CanvasHelper> helpers = new List<CanvasHelper>(); public static UnityEvent OnResolutionOrOrientationChanged = new UnityEvent(); private static bool screenChangeVarsInitialized = false; private static ScreenOrientation lastOrientation = ScreenOrientation.Landscape; 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.Find("SafeArea") as RectTransform; if (!screenChangeVarsInitialized) { lastOrientation = Screen.orientation; lastResolution.x = Screen.width; lastResolution.y = Screen.height; lastSafeArea = Screen.safeArea; screenChangeVarsInitialized = true; } ApplySafeArea(); } void Update() { if (helpers[0] != this) return; if (Application.isMobilePlatform && Screen.orientation != lastOrientation) OrientationChanged(); if (Screen.safeArea != lastSafeArea) SafeAreaChanged(); 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; Debug.Log(canvas.pixelRect); anchorMin.x /= canvas.pixelRect.width; anchorMin.y /= canvas.pixelRect.height; anchorMax.x /= canvas.pixelRect.width; anchorMax.y /= canvas.pixelRect.height; safeAreaTransform.anchorMin = anchorMin; safeAreaTransform.anchorMax = anchorMax; } 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; OnResolutionOrOrientationChanged.Invoke(); } private static void ResolutionChanged() { //Debug.Log("Resolution changed from " + lastResolution + " to (" + Screen.width + ", " + Screen.height + ") at " + Time.time); lastResolution.x = Screen.width; lastResolution.y = Screen.height; OnResolutionOrOrientationChanged.Invoke(); } private static void SafeAreaChanged() { // 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(); } } }
可以在Unity的插件下载里面下载预览插件Device Simulator,能模拟在手机上上到的效果,这里建议下载2.2.4版本,最新的3.0.3版本没看到预览设置。
这里是有hi地区,一位独立游戏业余开发者