如何根据unity2D 2021.3.15f1中的分辨率拉伸游戏对象

问题描述 投票:0回答:1

所以我有一个游戏,我需要游戏对象从屏幕边缘反弹并检测反弹何时发生,现在我在相机外面有一些灰色矩形,我检测它们的圆圈何时反弹,但我有它设置为固定宽高比(16:9),我希望它可以在任何屏幕尺寸下播放,我该怎么做?

我向chatgpt询问了它,它给了我很多答案,但没有一个起作用,我在这里搜索但没有找到它,我看到了一些youtube教程,但也没有起作用

unity-game-engine game-development screen-resolution
1个回答
0
投票

将以下脚本放在空游戏对象上

using UnityEngine;

[RequireComponent(typeof(EdgeCollider2D))]
public class Screenbounds : MonoBehaviour
{
    private EdgeCollider2D col;
    private Camera mainCam;

    void Start()
    {
        col = GetComponent<EdgeCollider2D>();
        mainCam = Camera.main;
        UpdateScreenEdge();
    }

    public void UpdateScreenEdge()
    {
        Vector2[] screenEdgePoints = new Vector2[5];
        screenEdgePoints[0] = mainCam.ScreenToWorldPoint(new Vector3(0, 0));
        screenEdgePoints[1] = mainCam.ScreenToWorldPoint(new Vector3(0, Screen.height));
        screenEdgePoints[2] = mainCam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height));
        screenEdgePoints[3] = mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0));
        screenEdgePoints[4] = screenEdgePoints[0];
        col.points = screenEdgePoints;
    }
}

此脚本将自动添加边缘碰撞器组件并在运行时对其进行设置,使其覆盖屏幕的边缘。如果调整屏幕分辨率大小,您应该调用

UpdateScreenEdge
函数来更新边缘点。不要过度调整大小,因为弹跳的物体很容易出现超出范围的情况。

© www.soinside.com 2019 - 2024. All rights reserved.