[英]Unity - Background Music through scenes with UI slider
I created a game in Unity with 4 different scenes (start, login, options, game itself). 我在Unity中创建了一个具有4个不同场景的游戏(开始,登录,选项,游戏本身)。
With an empty game object (in the start scene) and the DontDestroyOnLoad
function I have managed to have the music played throughout all scenes without stopping or loading new in every scene. 有了一个空的游戏对象(在开始的场景中)和
DontDestroyOnLoad
函数,我设法使音乐在所有场景中播放而不会在每个场景中停止或加载新的音乐。
In the options scene there is a slider hooked to an master audio mixer, which works so far. 在选项场景中,有一个滑块钩接到主音频混音器上,到目前为止,该混音器仍然有效。
The only problem for me is that the slider can "interfere" with the gameobject
in the start scene (background music, which should be triggered through the slider). 对我来说,唯一的问题是,滑块可以“干扰”的
gameobject
的开始场景(背景音乐,这应该通过滑块来触发)。
It would be awesome if someone could help me! 如果有人可以帮助我,那将太好了! :)
:)
Here some extracts: 这里有一些摘录:
ChangeVolume
class: ChangeVolume
类:
public AudioMixer audioMixer;
public void setVolume(float volume){
audioMixer.SetFloat ("volume", volume);
}
and 和
MusicBehaviour
class: 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 () {
}
I'm excited for your help/solutions, maybe there is a simpler one! 我很高兴您的帮助/解决方案,也许有一个更简单的解决方案! :-)
:-)
The easiest approach is to use PlayerPrefs
and save the sound value there. 最简单的方法是使用
PlayerPrefs
并将声音值保存在那里。 Every time you start the game in Awake()
you will setup the value and when slider is triggered you change the value in PlayerPrefs
and set it to the MusicBehaviour
instance. 每次在
Awake()
启动游戏时,都将设置该值,并在触发滑块时更改PlayerPrefs
的值并将其设置为MusicBehaviour
实例。
I fixed my problem through editing my scripts in the following way: 我通过以下方式编辑脚本来解决问题:
Music Behaviour
class: Music Behaviour
课程:
void Update () {
float vol = ChangeVolume.vol;
this.gameObject.GetComponent<AudioSource> ().volume = vol;
}
Change Volume
class: Change Volume
等级:
public void setVolume(float volume){
vol = volume;
}
public static float vol = 1.0f;
I also deleted my audiomixer due to not needing it anymore. 由于不再需要混音器,因此我也删除了它。 And it works just fine!
而且效果很好! :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.