This question already has answers here:
How to pass data between scenes in Unity

(5个答案)


3年前关闭。




我在Unity3d中创建了一个小游戏。该游戏有4个场景。我创建了小型声音服务来播放/静音/取消静音游戏中的声音和音乐。我的大多数声音是通过游戏中的某些方法播放的,而一种声音是通过单击按钮播放的。

这是我的SoundSerice:
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。

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

最佳答案

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

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

关于c# - 如何在每个场景上使声音/音乐静音? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42704990/

10-13 08:11
查看更多