gpt4 book ai didi

c# - 如何让Unity3D Camera聚焦在一个区域?

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:36 24 4
gpt4 key购买 nike

在我的 Unity3D 项目中,我有一个动态创建的板(使用 c#)并位于屏幕上,如下所示:

0 < x < 12
0 < z < 12
y = -1

如何让摄像头聚焦在电路板上,并针对不同的屏幕分辨率和不同的平台使其居中?

最佳答案

这是一个例子:

    using UnityEngine;
using System.Collections;

/**
* This class attempts to force VERT- Field of view scaling.
* By default, Unity uses the HOR+ technique.
*
* http://en.wikipedia.org/wiki/Field_of_view_in_video_games#Scaling_methods
*/

[ExecuteInEditMode]
[RequireComponent (typeof(Camera))]
public class VertMinusCameraFOV : MonoBehaviour {

public float designTimeVerticalFieldOfView = 60;
public int designTimeWidth = 1280; // default screen width
public int designTimeHeight = 720; // default screen height

private float hFOVInRads;

private int prevWidth;
private int prevHeight;

void Start () {

prevWidth = designTimeWidth;
prevHeight = designTimeHeight;

float aspectRatio = (float) designTimeWidth / (float) designTimeHeight;
float vFOVInRads = designTimeVerticalFieldOfView * Mathf.Deg2Rad;
hFOVInRads = 2f * Mathf.Atan( Mathf.Tan(vFOVInRads/2f) * aspectRatio);

}

void Update () {

if (Screen.width != prevWidth || Screen.height != prevHeight) { // capture screen ratio changes

float aspectRatio = (float) Screen.width / (float) Screen.height;

float vFOVInRads = 2f * Mathf.Atan( Mathf.Tan(hFOVInRads/2f) / aspectRatio );

Debug.Log("Screen resolution change. Recomputing aspect ratio (" + aspectRatio + ") and field of view (" + vFOVInRads*Mathf.Rad2Deg + ")");

foreach (Camera cam in GameObject.FindObjectsOfType(typeof(Camera))) {
cam.fieldOfView = vFOVInRads * Mathf.Rad2Deg;
}
}

}

}

关于c# - 如何让Unity3D Camera聚焦在一个区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29566583/

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