简体   繁体   English

当场景重新加载时音乐不会停止

[英]Music is not getting stop when scene reload in unity

I am using a toggle button to play and stop music and I am using playerPrefs to save the state. 我正在使用切换按钮播放和停止音乐,并且正在使用playerPrefs保存状态。 Toggle button is playing or stopping the music but when I reload the scene It gets messed up. 切换按钮正在播放或停止音乐,但是当我重新加载场景时,它变得混乱了。 Music keeps playing even if the toggle button is on(Means set the music off). 即使打开切换按钮,音乐仍会继续播放(意味着将音乐设置为关闭)。

For the player Prefs. 对于玩家偏好。 I am using Player prefs Manager script: 我正在使用Player prefs Manager脚本:

public static void SetMusicOnOFF(int value)
{
    PlayerPrefs.SetInt(Music, value);      
}

public static int GetMusicOnOff()
{
    return PlayerPrefs.GetInt(Music, 5);
}

For the toggle button I am using: 对于切换按钮,我正在使用:

class MusicToggleButton : MonoBehaviour
{
    Toggle t;
    // Use this for initialization
    void Start ()
    {
        t = GetComponent<Toggle>();

        if (PlayerPrefsManager.GetMusicOnOff() == 1)
        { //ERROR CALLING THE METHOD TO CHANGE MUSIC TO ON
            t.isOn = true;
        }          
    }

When the toggle button is pressed I am calling StartStopMusicPlayer method: 当按下切换按钮时,我正在调用StartStopMusicPlayer方法:

class MusicPlayer : MonoBehaviour
{
    public static bool stopPlayer;

    // Use this for initialization
    void Awake ()
    {
        if (PlayerPrefsManager.GetMusicOnOff() == 2)
        {  
            audioSource.Play();
            audioSource.playOnAwake = true;
            audioSource.loop = true;
        }
        else
        {
            audioSource.Stop();
        }
    }

    public void StartStopMusicPlayer()
    {
        stopPlayer = !stopPlayer;
        if (stopPlayer)
        {
            PlayerPrefsManager.SetMusicOnOFF(1);
            audioSource.Stop();
        }
        else
        {
            PlayerPrefsManager.SetMusicOnOFF(2);
            audioSource.Play();
        }
    } 
}

Music Player Class is not destroying on reload. 音乐播放器类在重新加载时不会销毁。 But the MusicToggleButton class is destroyed on reload. 但是MusicToggleButton类在重新加载时被破坏。 Thank you for your help. 谢谢您的帮助。

Calling PlayerPrefs.SetInt(Music, value) is not enough. 仅仅调用PlayerPrefs.SetInt(Music,value)是不够的。 You should call PlayerPrefs.Save() after that. 之后,您应该调用PlayerPrefs.Save()。 Or your changes will not take effect after restart. 否则您的更改将在重启后生效。

public static void SetMusicOnOFF(int value)
{
    PlayerPrefs.SetInt(Music, value);
    PlayerPrefs.Save();
}

Solution was so simple. 解决方法是如此简单。 If anyone want to know. 如果有人想知道。 Just change the toggle button with normal button. 只需将切换按钮更改为普通按钮即可。 and Inside the MusicToggleButton class. 和在MusicToggleButton类中。

public class MusicToggleButton : MonoBehaviour
{
    public Image offOnImage;
    public bool changeValue;
    public Color TargetColor;
    public Color originalColor;
    // Use this for initialization
    void Start ()
    {
        changeValue = !(PlayerPrefsManager.GetMusicOnOff() == 1);
        if (PlayerPrefsManager.GetMusicOnOff() == 1)
        {
            Debug.Log("stop Music");
            offOnImage.color = TargetColor;
        }
        else
        {
            Debug.Log("Playing Music");
            offOnImage.color = originalColor;
        }
    }

    public void musicButtonClick()
    {
        changeValue = !changeValue;
        if (changeValue)
        {
            //Debug.Log("changing to target color");
            //offOnImage.CrossFadeColor(TargetColor, 0.5f, false, false);
            offOnImage.color = originalColor;
            Debug.Log("Playing");
        }
        else
        {
            //Debug.Log("changing to original color");
            //offOnImage.CrossFadeColor(Color.white, 0.5f, false, false);
            Debug.Log("pause");
            offOnImage.color = TargetColor;
        }
    }
}

This will do Play and pause music. 这将播放和暂停音乐。 Act like a toggle button. 就像切换按钮一样。

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

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