首页 文章

Unity通过缩放相机保持屏幕上的GameObject大小(以像素为单位)

提问于
浏览
0

我在场景中有一个自上而下的透视摄像机视图 .

我正在为WebGL导出,我想以对象的自上而下的视图开始场景,但我需要它在屏幕像素中保持相同的大小,无论Unity播放器的宽度/高度如何网页 .

我想通过上/下(沿y轴)移动相机来实现这一点,但我正在努力计算相机的正确y位置所需的数学 .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainCamera : MonoBehaviour {

    private Camera cam;
    public Transform target;
    public float targetPixelHeight = 150;

    void Start () {
        cam = GetComponent<Camera>();
    }

    void Update () {

        float newY = /*unknown calculation involving camera position and targetPixelHeight*/;
        transform.position = new Vector3(transform.position.x, newY, transform.position.z);
    }

}

1 回答

  • 0

    你应该研究一下Unity的RenderTexture的用法 . 您可以在场景中的任何位置渲染相机,而不是向右渲染到屏幕 .

    首先创建纹理资源(Assets> Create> RenderTexture) . 有了这个,您可以设置一个 Canvas (GameObject> UI> Canvas),其中包含's the exact size you'(游戏对象> UI> RawImage)(像素或屏幕空间或您想要的任何参数) . 在场景中的任何位置指定 RenderTexture 作为 RawImage 's texture. Then you can setup a 1817615 which is essentially the camera and object you' d喜欢渲染 . 将相机的targetTexture设置为 RenderTexture 并且你已经完成了!

相关问题