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

标签 unity3d user-interface resize window-resize autoresize

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

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

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),然后在每个样本上比较它们(更新),如果存在差异(屏幕变化)则触发调整脚本。

当然可以。我认为这是非常标准的编码逻辑。对于每个对象每个样本运行一个/两个额外的 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

关于unity3d - Unity 是否具有在 Screen.height 或 Screen.width 更改时触发的内置函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69753218/

相关文章:

unity3d - Unity 如何在图像周围流动 TextMesh Pro 文本?

ios - 调整 UIView 的 subview

c# - TableLayoutPanel 中的列大小在设计器中不断变化

user-interface - 在JUCE中将GUI编辑器用于音频插件

c# Form.Hide() 与 Form.Opacity = 0

CS50 pset 4 resize 垂直拉伸(stretch)给出奇怪的输出

unity3d - 如何加载同名的 Assets ,但放置在assetbundle的不同文件夹中?

c# - DllNotFoundException : TMPro_Plugin, 在 Linux 上使用 TextMesh Pro

unity3d - 在Unity3D中订阅youtube

Java 图形用户界面设计 : Use object or use method?