【问题标题】:Unity - Background Music through scenes with UI sliderUnity - 带有 UI 滑块的场景背景音乐
【发布时间】:2018-12-06 05:52:03
【问题描述】:

我在 Unity 中创建了一个包含 4 个不同场景(开始、登录、选项、游戏本身)的游戏。

使用一个空的游戏对象(在开始场景中)和DontDestroyOnLoad 函数,我设法在所有场景中播放音乐,而无需在每个场景中停止或加载新的。

在选项场景中,有一个连接到主混音器的滑块,目前为止可以正常工作。

对我来说唯一的问题是滑块可以“干扰”开始场景中的gameobject(背景音乐,应该通过滑块触发)。
如果有人可以帮助我,那就太棒了! :)

这里有一些摘录:

ChangeVolume类:

public AudioMixer audioMixer;

public void setVolume(float volume){
    audioMixer.SetFloat ("volume", volume);
}

MusicBehaviour类:

//Play global
private static MusicBehaviour instance = null;
    public static MusicBehaviour Instance {
        get {
              return instance;
        }
    }

void Awake()
{
    if (instance != null && instance != this) {
        Destroy (this.gameObject);
        return;
    } else {
        instance = this;
    }
    DontDestroyOnLoad (this.gameObject);
}

//Play Global End

//Update is called once per frame
void Update () {
}

我很高兴您的帮助/解决方案,也许有一个更简单的! :-)

【问题讨论】:

    标签: c# unity3d audio uislider mixer


    【解决方案1】:

    最简单的方法是使用PlayerPrefs 并将声音值保存在那里。每次您在Awake() 中启动游戏时,您都会设置该值,并在触发滑块时更改PlayerPrefs 中的值并将其设置为MusicBehaviour 实例。

    【讨论】:

    • 我从没想过 PlayerPrefs,但我想我明白了。也许它比我的解决方案更好,但是:如果有人感兴趣,我会在下面发布我的解决方案。 :-)
    【解决方案2】:

    我通过以下方式编辑脚本解决了我的问题:

    Music Behaviour类:

     void Update () {
             float vol = ChangeVolume.vol;
             this.gameObject.GetComponent<AudioSource> ().volume = vol;
         }
    

    Change Volume类:

    public void setVolume(float volume){
        vol = volume;
    }
    
    public static float vol = 1.0f;
    

    由于不再需要我的混音器,我也删除了它。它工作得很好! :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多