While testing in the editor using Unity 5.0.0b12 on Mac.
I’m trying to implement an audio mute / toggle button to set a particular snapshot / state in Start() based on a playerPrefs. The default selected snapshot in the audio mixer is ‘volume on’. refreshMixer is called as expected by the ValueChanged event.
With hack == 0, if I run the app once and mute the audio, then re-run the app the audio will not be muted because the TransitionTo call fails (or is ignored).
If hack == 2, it works as expected.
I’ve submitted a bug report on this. I was wondering if anyone has encountered this issue and if they have a better work around?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Audio;
public class musicManager : MonoBehaviour {
public AudioMixerSnapshot musicOn;
public AudioMixerSnapshot musicOff;
public Toggle musicToggle;
int hack;
// Use this for initialization
void Start () {
int musicEnabled = PlayerPrefs.GetInt("MusicEnabled", 1);
if (musicEnabled == 1) {
musicToggle.isOn = true;
} else {
musicToggle.isOn = false;
}
hack = 2;
}
void Update() {
if (hack > 0) {
hack--;
if (hack == 0) {
refreshMixer();
}
}
}
public void toggleChanged() {
int musicEnabled;
if (musicToggle.isOn) {
musicEnabled = 1;
} else {
musicEnabled = 0;
}
PlayerPrefs.SetInt("MusicEnabled", musicEnabled);
PlayerPrefs.Save();
refreshMixer();
}
void refreshMixer() {
int musicEnabled = PlayerPrefs.GetInt("MusicEnabled", 1);
if (musicEnabled == 1) {
musicOn.TransitionTo(0.2f);
} else {
musicOff.TransitionTo(0.2f);
}
}
}