gpt4 book ai didi

unity3d - 为什么我不能缩放我的 Unity2D 正交游戏?

转载 作者:行者123 更新时间:2023-12-04 10:43:44 24 4
gpt4 key购买 nike

我看过几个视频和帖子,表明我应该能够在游戏过程中通过更改 MainCamera(Unity 2D 游戏,正交投影)的 Size 参数来缩放摄像机 View 。这样做会放大和缩小场景 View ,但我在检查器中的任何地方所做的任何更改都不会改变游戏 View 。

谁能给个建议?谢谢。

enter image description here

最佳答案

正交尺寸定义了相机的观察体积,它只会放大由它渲染的元素。看起来您正在尝试放大 Canvas,但 UI 元素呈现在整个场景的顶部。

为了放大和缩小,您可以:

  1. 将 Canvas Render Mode 设置为World Space,然后将它放在相机前面
  2. 使用可让您放大 Canvas 的脚本,请查看此处:Pinch zoom on UI

     public class PinchZoom : MonoBehaviour
    {
    public Canvas canvas; // The canvas
    public float zoomSpeed = 0.5f; // The rate of change of the canvas scale factor

    void Update()
    {
    // If there are two touches on the device...
    if (Input.touchCount == 2)
    {
    // Store both touches.
    Touch touchZero = Input.GetTouch(0);
    Touch touchOne = Input.GetTouch(1);

    // Find the position in the previous frame of each touch.
    Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

    // Find the magnitude of the vector (the distance) between the touches in each frame.
    float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
    float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

    // Find the difference in the distances between each frame.
    float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

    // ... change the canvas size based on the change in distance between the touches.
    canvas.scaleFactor -= deltaMagnitudeDiff * zoomSpeed;

    // Make sure the canvas size never drops below 0.1
    canvas.scaleFactor = Mathf.Max(canvas.scaleFactor, 0.1f);
    }
    }
    }

希望对您有所帮助!

关于unity3d - 为什么我不能缩放我的 Unity2D 正交游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59815034/

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