gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:42:52 28 4
gpt4 key购买 nike

我为 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com