简体   繁体   English

Unity C#:通过读取 void Update() 内部的回调布尔值来改变场景

[英]Unity C#: change scenes by reading callback Boolean inside voide Update()

Struggling with a specific thing here.在这里挣扎着一个特定的东西。 I need to change scenes when the calibration is successful.校准成功后需要更换场景。 For that, I can use:为此,我可以使用:

if (someVariable == true)
{
   SceneManager.LoadScene("MainScene");
}

The code that calls the calibration start is this:调用校准开始的代码是这样的:

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");
            }
        }

I need a way to use that resultCallback inside the private void Update() in order to change scenes, because resultCallback returns either true or false, if the calibration of my VR system was successful.我需要一种在私有 void Update()中使用resultCallback的方法来改变场景,因为如果我的 VR 系统校准成功, resultCallback返回 true 或 false。 Also, here's what StartCalibration() returns:此外,这是StartCalibration()返回的内容:

public bool StartCalibration(Vector3[] points = null, System.Action<bool> resultCallback = null)

Q: How do I code this to use the boolean value of resultCallback to change the scene?问:我该如何编码以使用 resultCallback 的布尔值来更改场景? So far it writes to the console whatever the result was, but I don't really need that output, if I can make it change scene automatically.到目前为止,无论结果如何,它都会写入控制台,但我真的不需要那个输出,如果我可以让它自动改变场景。

I'm not very experienced in C#.我在 C# 方面不是很有经验。

I've tried the following, but, of course, it doesn't work:我已经尝试了以下方法,但是,当然,它不起作用:

private void Update()
        {
            if (Input.GetKeyDown(_startKey))
            {
                var calibrationStartResult = StartCalibration(
                    resultCallback: (calibrationResult) =>
                        Debug.Log("Calibration was " + (calibrationResult ? "successful" : "unsuccessful"))
            if (calibrationResult == true)
            {
                SceneManager.LoadScene("MainScene");
            }
                );

                Debug.Log("Calibration " + (calibrationStartResult ? "" : "not ") + "started");
            }
        }

You almost had it -- the key is to define a second class-level property that persists the local value.您几乎拥有它——关键是定义第二个类级别的属性来持久化本地值。 Note I'm renaming calibrationResult to calibrated for ease of use.注意我将calibrationResult重命名为calibrated以便于使用。 Try:尝试:

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");
    }
}

Another way to achieve this would be to just call SceneManager.LoadScene from within the resultCallback scope.实现此目的的另一种方法是从 resultCallback 范围内调用SceneManager.LoadScene This would also save on the repeat bool check of calibrated inside the Update.这也将节省更新内部calibrated的重复布尔检查。 While a single Update check isn't lagging, you want to generally keep track of how much needed and unneeded things you do in these eternal loops.虽然单个更新检查不会滞后,但您通常希望跟踪您在这些永恒循环中做了多少需要和不需要的事情。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM