我正在尝试切换音乐打开和关闭脚本,由于某种原因,值不改变我做错了什么?
[SerializeField] Toggle toggle;
[SerializeField] AudioSource audioSource;
public int Music;
private void Update()
{
Music = PlayerPrefs.GetInt("Music");
if (Music == 0) audioSource.enabled = false;
else if (Music == 1) audioSource.enabled = true;
PlayerPrefs.SetInt("Music", Music);
MakeTextRed();
}
public void MakeTextRed()
{
if (toggle.isOn == true) Music = 1;
else if (toggle.isOn == false) Music = 0;
}
当我玩游戏并点击切换按钮时,检查器中的音乐值不会改变。
我不确定你的代码是错的还是真的。您可以使用这个简单的代码来代替int或其他
[SerializeField] Toggle toggle;
[SerializeField] AudioSource audioSource;
private void Update(){
// CheckToggler();
// More Simple One is
audioSource.enabled = toggle.isOn;
}
private void CheckToggler(){
if (toggle.isOn) // On
audioSource.enabled = true;
else //off
audioSource.enabled = false;
}
你的逻辑不正确,这就是原因。常用的做法是在Visual Studio中启动调试会话。它将显示您要设置的值。
private void Update()
{
Music = PlayerPrefs.GetInt("Music"); // Read value from Prefs
if (Music == 0) audioSource.enabled = false;
else if (Music == 1) audioSource.enabled = true;
PlayerPrefs.SetInt("Music", Music); // Set value to Prefs (It's the same)
MakeTextRed(); // This must be one line higher
}