[英]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.