切换场景时,静音按钮不起作用

时间:2018-08-08 19:51:00

标签: unity3d

AudioManager和声音效果在场景开始时工作正常,但是在切换场景时不起作用。甚至是同一场景。

如果能为您解决有关PlayerPrefs的问题,我将不胜感激。我一直在论坛上搜索有关PlayerPrefs的所有内容,但不确定输入什么内容。

非常感谢您。

  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using System;


public class AudioManager : MonoBehaviour {

    bool mutebutton = false;

    public Sound[] sounds;
    public static AudioManager instance;

    // Use this for initialization
    void Awake () {

        if (instance == null)
            instance = this;
        else
        {
            Destroy(gameObject);
            return;

        }

        DontDestroyOnLoad(gameObject);

        foreach (Sound s in sounds)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;
            s.source.volume = s.volume;

        }
    }

   void Start()
    {
        Play("Theme");
        PlayerPrefs.SetFloat("volume", AudioListener.volume);
        PlayerPrefs.Save();
    }

    // Update is called once per frame
    public void Play (string name)
    {
        Sound s = Array.Find(sounds, sound => sound.name == name);
        s.source.Play();
    }

    public void Mute()
    {
        if (!mutebutton)
        {
            mutebutton = true;
            AudioListener.volume = 0;

        }
        else
        {
            mutebutton = false;
            AudioListener.volume = 1;

        }

}


}

1 个答案:

答案 0 :(得分:0)

如果仅在场景加载时错过"Theme"声音: Start()方法不会在场景加载时再次调用。 加载场景后,您必须主动致电Play("Theme")