场景切换后如何保持设置
how to keep setting after scene change
我在 1 个场景上有一个选项菜单,但是当您加载到游戏中(切换场景)然后返回(切换场景)时,它会丢失所有设置。我试图用 DontDestroyOnLoad 来做,但无法让它工作,我不知道如何读写文本文件。保留所有设置的最佳方法是什么?
图片:Here
在 Unity 中,您可以通过 PlayerPrefs class 保存和加载设置。这是一个静态 class,这意味着您可以访问 Unity 脚本文件中任何位置的设置。
用法示例:
// Set the player name preference
PlayerPrefs.SetString("player_name", "Darian Benam");
// Save all the preferences
PlayerPrefs.Save();
// Load the player name from the preferences
string playerName = PlayerPrefs.GetString("player_name"); // According to this example, the value of the string will be "Darian Benam"
重新加载菜单场景中的设置需要在脚本的 Start()
方法中完成。您只需将 GUI 组件的值设置为 PlayerPrefs
class.
中 getter 方法的 return 值
我在 1 个场景上有一个选项菜单,但是当您加载到游戏中(切换场景)然后返回(切换场景)时,它会丢失所有设置。我试图用 DontDestroyOnLoad 来做,但无法让它工作,我不知道如何读写文本文件。保留所有设置的最佳方法是什么? 图片:Here
在 Unity 中,您可以通过 PlayerPrefs class 保存和加载设置。这是一个静态 class,这意味着您可以访问 Unity 脚本文件中任何位置的设置。
用法示例:
// Set the player name preference
PlayerPrefs.SetString("player_name", "Darian Benam");
// Save all the preferences
PlayerPrefs.Save();
// Load the player name from the preferences
string playerName = PlayerPrefs.GetString("player_name"); // According to this example, the value of the string will be "Darian Benam"
重新加载菜单场景中的设置需要在脚本的 Start()
方法中完成。您只需将 GUI 组件的值设置为 PlayerPrefs
class.