Unity C#:通过读取void Update()中的回调布尔值来更改场景
这里有一件特别的事情。校准成功后,我需要更改场景。为此,我可以使用:Unity C#:通过读取void Update()中的回调布尔值来更改场景,c#,unity3d,C#,Unity3d,这里有一件特别的事情。校准成功后,我需要更改场景。为此,我可以使用: if (someVariable == true) { SceneManager.LoadScene("MainScene"); } 调用校准开始的代码如下: private void Update() { if (Input.GetKeyDown(_startKey)) { var calibrationStartResu
if (someVariable == true)
{
SceneManager.LoadScene("MainScene");
}
调用校准开始的代码如下:
private void Update()
{
if (Input.GetKeyDown(_startKey))
{
var calibrationStartResult = StartCalibration(
resultCallback: (calibrationResult) =>
Debug.Log("Calibration was " + (calibrationResult ? "successful" : "unsuccessful"))
);
Debug.Log("Calibration " + (calibrationStartResult ? "" : "not ") + "started");
}
}
我需要一种在私有void Update()中使用resultCallback的方法来更改场景,因为如果我的VR系统校准成功,resultCallback将返回true或false。另外,以下是StartCalibration()返回的内容:
public bool StartCalibration(Vector3[] points = null, System.Action<bool> resultCallback = null)
您几乎做到了--关键是定义第二个类级别的属性来持久化本地值。注意:为了便于使用,我将calibrationResult
重命名为calibrated
。尝试:
bool calibrated = false;
bool startedCalibration = false;
private void Update()
{
if (Input.GetKeyDown(_startKey) && !startedCalibration)
{
startedCalibration = true;
StartCalibration(
resultCallback: (calibrated) =>
{
startedCalibration = false;
print("Calibration was " + (calibrated ? "successful" : "unsuccessful");
// Assign the local scope variable to the class property:
this.calibrated = calibrated;
}
);
}
if (calibrated)
{
calibrated = false;
SceneManager.LoadScene("MainScene");
}
}
实现这一点的另一种方法是从resultCallback范围内调用
SceneManager.LoadScene
。这也将在更新内部对校准的
进行重复布尔检查时保存。虽然单个更新检查并不滞后,但您通常希望跟踪您在这些永恒循环中所做的需要和不需要的事情。祝你好运 “它不起作用”到底是什么意思?有错吗?嗨,菲利普。很抱歉反应太晚。这100%奏效。非常感谢。
bool calibrated = false;
bool startedCalibration = false;
private void Update()
{
if (Input.GetKeyDown(_startKey) && !startedCalibration)
{
startedCalibration = true;
StartCalibration(
resultCallback: (calibrated) =>
{
startedCalibration = false;
print("Calibration was " + (calibrated ? "successful" : "unsuccessful");
// Assign the local scope variable to the class property:
this.calibrated = calibrated;
}
);
}
if (calibrated)
{
calibrated = false;
SceneManager.LoadScene("MainScene");
}
}