在Unity VR应用程序中更改FOV

时间:2016-10-31 09:09:18

标签: android unity3d virtual-reality

我正在尝试在Unity中更改VR相机的FOV值。在编辑器工作正常,但当我把应用程序放在手机上时,相机具有默认的FOV值。有人可以告诉我如何有效地改变手机上的FOV值? 感谢

1 个答案:

答案 0 :(得分:0)

您可以通过代码完成。这是一个脚本,可让您点按以实时调整FOV。

using UnityEngine;
using System.Collections;

using UnityEngine.VR; // you always need this to use special VR functions

public class VRUtility : MonoBehaviour {
public GameObject camGroup;
public Camera leftCam;
public Camera rightCam;

public float fov;
private float fovMin = 40;
private float fovMax = 100;

private float camGroupX = 0;
private float camGroupXMin = 0;
private float camGroupXMax = 100;
private float camGroupXStep = 20;


// Use this for initialization
public void Start () {
    // set render quality to 50%, sacrificing speed for visual quality
    // this is pretty important on laptops, where the framerate is often quite low
    // 50% quality actually isn't that bad
    VRSettings.renderScale = 3.0f;

    OVRTouchpad.Create();
    OVRTouchpad.TouchHandler += HandleTouchHandler;

    leftCam.fieldOfView = fov;
    rightCam.fieldOfView = fov;
}

// Update is called once per frame
void Update () {

    if ( Input.GetKeyDown(KeyCode.R) ) {
        InputTracking.Recenter(); // recenter "North" for VR, so that you don't have to twist around randomlys
    }

    // dynamically adjust VR visual quality in-game
    if ( Input.GetKeyDown(KeyCode.RightBracket) ) { // increase visual quality
        VRSettings.renderScale = Mathf.Clamp01( VRSettings.renderScale + 0.1f);
    }
    if ( Input.GetKeyDown(KeyCode.LeftBracket) ) { // decrease visual quality
        VRSettings.renderScale = Mathf.Clamp01( VRSettings.renderScale - 0.1f);
    }
}

private void HandleTouchHandler(object sender, System.EventArgs e){

    var touchArgs = (OVRTouchpad.TouchArgs)e;
    if (touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap) {

        fov += 10;
        if (fov > fovMax) {
            fov = fovMin;
        }

        leftCam.fieldOfView = fov;
        rightCam.fieldOfView = fov;
    }

    if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Left || touchArgs.TouchType == OVRTouchpad.TouchEvent.Right){

        camGroupX += camGroupXStep;
        if (camGroupX > camGroupXMax){
            camGroupX = camGroupXMin;
        }

        camGroup.transform.position = new Vector3(camGroupX, 0, 0);
    }
}

}

我也在不同地点之间移动相机,但忽略它。