简体   繁体   English

如何停止代码的执行,直到我从 Unity 中的 PlayerPref 读取值

[英]How to stop the execution of the code until I read a value from PlayerPref in Unity

Currently I am using Toggles to select a list of images and rather ran into an interesting problem.目前我正在使用 Toggles 来选择图像列表,但遇到了一个有趣的问题。

I am using a Toggle Group called Radio Group and have 3 toggles under it.我正在使用一个名为 Radio Group 的 Toggle Group,它下面有 3 个开关。 Each time when a toggle is selected the command每次选择切换时,命令

 PlayerPrefs.SetInt("SaveToggleId", id);

is run.正在运行。 In this the id number is 0 for toggle 1, 1 for toggle 2 and so on.在此,切换 1 的 id 编号为 0,切换 2 为 1,依此类推。

So when I try to read this data the next time , the following set of code always reads 0 when used in Start and the correct value when used in Awake因此,当我下次尝试读取此数据时,以下代码集在 Start 中使用时始终读取为 0,而在 Awake 中使用时始终读取正确值

toggleGroupId = PlayerPrefs.GetInt("SaveToggleId");
toggleGroupObject = GetComponent<ToggleGroup>();
SelectStartingToggle(toggleGroupId);

When I used this code in conjuction with the Debug.log() statements in various places what I found is when used in Start , it first reads from the function associated when the first toggle is selected and therby stores 0 .当我在各个地方将此代码与 Debug.log() 语句结合使用时,我发现在 Start 中使用时,它首先从选择第一个切换时关联的函数中读取,然后存储 0 。 But when I use it in Awake it reads the right value stored in PlayerPrefs and selects the correct initial value但是当我在 Awake 中使用它时,它会读取存储在 PlayerPrefs 中的正确值并选择正确的初始值

My explanation would be that because Awake is executed before Start , it has ample time to read from PlayerPrefs which gives the correct value.我的解释是,因为 Awake 在 Start 之前执行,所以它有足够的时间从 PlayerPrefs 读取给出正确的值。 Also when I used only the number in the Start() as follows此外,当我仅使用 Start() 中的数字时,如下所示

SelectStartingToggle(3);

it correctly selected the right toggle whereas when I used PlayerPref instead of number ,it chose the wrong value.它正确选择了正确的切换,而当我使用 PlayerPref 而不是 number 时,它选择了错误的值。

Is my explanation correct or am I missing something?Also how to make sure the code execution is halted until the data from PlayerPref is read.我的解释正确还是我遗漏了什么?还有如何确保代码执行停止,直到读取来自 PlayerPref 的数据。 Here is the full code:这是完整的代码:

public class RadioButtonSystem : MonoBehaviour
{
    ToggleGroup toggleGroupObject;
    private int toggleGroupId;

    // Start is called before the first frame update
    private void Awake()
    {
        toggleGroupId = PlayerPrefs.GetInt("SaveToggleId");
        toggleGroupObject = GetComponent<ToggleGroup>();
        Debug.Log("SaveToggleId........" + toggleGroupId);
        SelectStartingToggle(toggleGroupId);

    }
    void Start()
    {
       
    }

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

    }
    public void onSelectedToggle1()
    {
        SaveToggleId(0);
    }
    public void onSelectedToggle2()
    {
        SaveToggleId(1);
    }
    public void onSelectedToggle3()
    {
        SaveToggleId(2);
    }

    public void SelectStartingToggle(int id)
    {
        Toggle[] toggles = GetComponentsInChildren<Toggle>();
        toggles[id].isOn = true;
    }

    public void SaveToggleId(int id)
    {
        PlayerPrefs.SetInt("SaveToggleId", id);
        Debug.Log("SaveToggleId........saving..........." + PlayerPrefs.GetInt("SaveToggleId"));
    }

    /*  Toggle GetSelectedToggle()
      {
          Toggle[] toggles = GetComponentsInChildren<Toggle>();
          foreach (var t in toggles)
              if (t.isOn) return t;  //returns selected toggle
          return null;           // if nothing is selected return null
      }*/

}

Playerprefs are saved upon OnApplicationQuit() . Playerprefs 保存在OnApplicationQuit() If you want to save it immediately, call PlayerPrefs.Save() .如果您想立即保存它,请调用PlayerPrefs.Save() After PlayerPrefs.SetInt() .PlayerPrefs.SetInt()

Btw, from the unity scripting api:顺便说一句,来自统一脚本api:

This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay.此函数会写入磁盘,可能会导致小问题,因此不建议在实际游戏过程中调用。

In the edit/project settings there is a tab called script execution order, where you can set the order in which your scripts will be executed.在编辑/项目设置中有一个名为脚本执行顺序的选项卡,您可以在其中设置脚本的执行顺序。 For example, you have a "LoadManager" script, you set its priority to 1, and you set everything that relys on it to a greater number like 10.例如,您有一个“LoadManager”脚本,您将其优先级设置为 1,并将依赖它的所有内容设置为更大的数字,例如 10。

If you fo this, nothing eill start executing until your manager script finished.如果您这样做,则在您的管理器脚本完成之前不会开始执行任何操作。

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

相关问题 [已解决]Firebase 数据库 Unity 无法使用我从数据库中读取的值设置 playerpref 浮点数 - [SOLVED]Firebase Database Unity can't set playerpref float with the value I read from the Database unity playerpref终身得分 - unity playerpref lifetime score 我如何正确地阻止代码执行直到回调 - How do I properly block code execution until callback 停止所有代码执行,直到IFrame完全加载 - Stop All Code Execution Until IFrame Completely Loads 如何从Unity中的firebase读取值? - How to read value from firebase in Unity? 当在调试控制台中突出显示某些内容时,执行会暂停,直到它未突出显示......我该如何停止这种行为? - When something is highlighted in a debug console, execution just pauses until it's unhighlighted ... how do I stop this behaviour? 停止所有代码执行直到满足条件(Unity C#) - Stopping all code execution until condition is met (Unity C#) xUnit:从代码停止测试执行 - xUnit: stop test execution from code 如何修复 Unity 中的“无法从复合材料中读取 Vector2 类型的值”错误? - How can I fix “Cannot read value of type Vector2 from composite” error in Unity? 找到玩家所在场景的“编号”以将其保存在 PlayerPref Unity 中 - Find the 'Number' of the Scene the Player is in to Save it in PlayerPref Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM