【问题标题】:How to mute the sounds/music on every scene? [duplicate]如何使每个场景的声音/音乐静音? [复制]
【发布时间】:2017-03-09 20:41:25
【问题描述】:

我正在 Unity3d 中创建一个小游戏。这场比赛有4个场景。我创建了小型声音服务来播放/静音/取消静音我的游戏中的声音和音乐。我的大部分声音是通过游戏中的一些方法播放的,只有一个声音是通过点击按钮播放的。

这是我的声音服务:

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

public class SoundService : MonoBehaviour
{

public AudioSource efxSource;
public AudioSource musicSource;

public static SoundService instance = null;

public AudioClip dieSound;
public AudioClip pointSound;
public AudioClip newRecordSound;
public AudioClip flapSound;

public void MuteSounds()
{

    if (efxSource.mute)
        efxSource.mute = false;
    else
        efxSource.mute = true;

}



public void MuteMusic()
{
    if (musicSource.mute)
        musicSource.mute = false;
    else
        musicSource.mute = true;
}


void Awake()
{
    if (instance == null)
        instance = this;
    else if(instance != this)
        Destroy(gameObject);

    DontDestroyOnLoad(gameObject);
}

public void PlaySingle(AudioClip clip)
{
        efxSource.clip = clip;
        efxSource.Play();
}

public void PlayLoseSound()
{
    efxSource.clip = dieSound;
    efxSource.Play();
}

public void PlayPointSound()
{
    efxSource.clip = pointSound;
    efxSource.Play();
}

public void PlayFlapSound()
{
    efxSource.clip = flapSound;
    efxSource.Play();
}

public void PlayNewRecordSound()
{
    efxSource.clip = newRecordSound;
    efxSource.Play();
}
}

问题是:当我在场景之间移动时,efxSource.mute 和 musicSource.mute 总是切换为 false。

我想在主菜单静音,然后开始新游戏时,音乐仍然静音。

【问题讨论】:

    标签: c# visual-studio audio unity3d


    【解决方案1】:

    您可以做的是将状态保存在 PlayerPrefs.setInt 中。您可以在静音时使用值 0,在未静音时使用值 1。

    这样做的好处是,即使用户关闭应用程序,它们也会保留这些值。

    【讨论】:

      【解决方案2】:

      您可以使用 SceneManager 并获取事件 SceneManager.sceneLoaded https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html

      然后:

      SceneManager.sceneLoaded -= OnSceneWasLoaded;
      SceneManager.sceneLoaded += OnSceneWasLoaded;
      
      public virtual void OnSceneWasLoaded(Scene scene, LoadSceneMode mode)
      {
                  // TODO
      }
      

      【讨论】:

        猜你喜欢
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多