Zek*_*man 0 c# unity-game-engine
我正在尝试为谷歌纸板制作 VR 游戏,并且我正在尝试在 2 秒后设置相机的 FOV,但是我收到错误:
“NullReferenceException:未将对象引用设置为对象 CameraFOV.Start 的实例”
using UnityEngine;
using System.Collections;
public class CameraFOV : MonoBehaviour
{
// Use this for initialization
void Start()
{
System.Threading.Thread.Sleep(2000);
Camera.current.fieldOfView = 60;
}
// Update is called once per frame
void Update()
{
}
}
Run Code Online (Sandbox Code Playgroud)
使用Camera.main而不是Camera.current. 此外,Unity API 不是线程安全的。你不能像这样暂停主线程。如果您想等待两秒钟,然后将所有摄像机设置为相同的 FOV,那么您可以使用:
void Start()
{
//This starts the coroutine.
StartCoroutine(PauseAndSetFOV());
}
// This is a coroutine.
private IEnumerator PauseAndSetFOV()
{
// This waits for a specified amount of seconds
yield return new WaitForSeconds(2f);
// This sets all the cameras FOV's after waiting two seconds.
for(int i = 0; i < Camera.allCamerasCount; i++)
{
Camera.allCameras[i].fieldOfView = 60;
}
}
Run Code Online (Sandbox Code Playgroud)
返回的函数IEnumerator是一个协程。这就是在 Unity 中同时执行多项操作的方法。但它不是线程。
| 归档时间: |
|
| 查看次数: |
2767 次 |
| 最近记录: |