简体   繁体   中英

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. 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:

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:

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. Thank you for your help.

Calling PlayerPrefs.SetInt(Music, value) is not enough. You should call PlayerPrefs.Save() after that. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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