Unity 是否具有在 Screen.height 或 Screen.width 更改时触发的内置函数?

Does Unity have a built in function that is triggered when Screen.height or Screen.width change?

我为 Unity 内置的移动应用程序构建了一些自定义 UI 缩放功能,以补偿各种 phone 屏幕比例。我还想防止屏幕尺寸变化,随着可折叠 phones 的兴起,我认为这是一个风险。

我已经实施的初步解决方案是将脚本附加到必须可能调整大小的对象,结构如下:

public class ScreenElementSizer : MonoBehaviour {

    private int screenWidth_1 = Screen.width;
    private int screenHeight_1 = Screen.height;

    // Start is called before the first frame update
    void Start() {
        ScreenResized();
    }

    // Resizing functions here
    private void ScreenResized() {
    }

    // On every screen refresh
    void Update()
    {
        if ((Screen.width != screenWidth_1) || (Screen.height != screenHeight_1)) {
            ScreenResized();
            screenWidth_1 = Screen.width;
            screenHeight_1 = Screen.height;
        }
    }

如您所见,这是一个非常简单的解决方案,我将先前样本的 Screen.widthScreen.height 存储在变量 (screenWidth_1 & screenHeight_1) 中,然后在每个示例(更新)上比较它们,如果存在差异(屏幕更改)则触发调整器脚本。

当然可以。我认为这是非常标准的编码逻辑。 运行 one/two 额外的 if 像这样的每个样本每个对象的语句可能并不昂贵。

我是 Unity 的新手,想知道是否有更好的、内置的或更有效的方法来完成这项任务。

需要说明的是,我知道内置的 canvas 缩放器工具可以根据宽度和高度进行缩放。我特别指的是当屏幕尺寸发生变化时,您想应用一些基于脚本的特殊调整大小逻辑的情况。

感谢任何想法。

没有内置事件,但您可以创建自己的事件。

DeviceChange.cs -

using System;
using System.Collections;
using UnityEngine;
 
public class DeviceChange : MonoBehaviour 
{
    public static event Action<Vector2> OnResolutionChange;
    public static event Action<DeviceOrientation> OnOrientationChange;
    public static float CheckDelay = 0.5f; // How long to wait until we check again.
 
    static Vector2 resolution;                    // Current Resolution
    static DeviceOrientation orientation;        // Current Device Orientation
    static bool isAlive = true;                    // Keep this script running?
 
    void Start() {
        StartCoroutine(CheckForChange());
    }
 
    IEnumerator CheckForChange(){
        resolution = new Vector2(Screen.width, Screen.height);
        orientation = Input.deviceOrientation;
 
        while (isAlive) {
 
            // Check for a Resolution Change
            if (resolution.x != Screen.width || resolution.y != Screen.height ) {
                resolution = new Vector2(Screen.width, Screen.height);
                if (OnResolutionChange != null) OnResolutionChange(resolution);
            }
 
            // Check for an Orientation Change
            switch (Input.deviceOrientation) {
                case DeviceOrientation.Unknown:            // Ignore
                case DeviceOrientation.FaceUp:            // Ignore
                case DeviceOrientation.FaceDown:        // Ignore
                    break;
                default:
                    if (orientation != Input.deviceOrientation) {
                        orientation = Input.deviceOrientation;
                        if (OnOrientationChange != null) OnOrientationChange(orientation);
                    }
                    break;
            }
 
            yield return new WaitForSeconds(CheckDelay);
        }
    }
 
    void OnDestroy(){
        isAlive = false;
    }
 
}

实施 -

DeviceChange.OnOrientationChange += MyOrientationChangeCode;
DeviceChange.OnResolutionChange += MyResolutionChangeCode;
DeviceChange.OnResolutionChange += MyOtherResolutionChangeCode;
 
void MyOrientationChangeCode(DeviceOrientation orientation) 
{
}
 
void MyResolutionChangeCode(Vector2 resolution) 
{
}

信用 - DougMcFarlane