Created
December 31, 2013 08:44
-
-
Save cnsoft/8194160 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class SingletonBehaviour<Interface> : MonoBehaviour | |
where Interface : class | |
{ | |
private static Interface _instance = null; | |
public static Interface instance { get { return _instance; } } | |
void Awake() | |
{ | |
System.Diagnostics.Debug.Assert(this is Interface); | |
if (_instance == null) | |
{ | |
_instance = this as Interface; | |
DontDestroyOnLoad(gameObject); | |
OnAwake(); | |
} | |
else | |
{ | |
DestroyImmediate(this); | |
} | |
} | |
protected virtual void OnAwake() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define interface for the singleton (simplifies intellisense and access | |
// when writing scripts that use the singletons since they won't see all | |
// the standard Unity behaviour methods and properties) | |
public interface IAudioManager | |
{ | |
bool audioEnabled { get; set; } | |
float musicPlayTime { get; } | |
void PlaySfx(string name); | |
void ToggleAudio(); | |
} | |
// subclass the SingletonBehaviour using the interface as the type parameter. | |
public class AudioManager : SingletonBehaviour<IAudioManager>, IAudioManager | |
{ | |
private const string audioEnabledKey = "IsAudioEnabled"; | |
[SerializeField] | |
private AudioSource _musicSource = null; | |
[SerializeField] | |
private AudioSource _sfxSource = null; | |
[SerializeField] | |
private AudioClip[] _soundEffects = null; | |
public bool audioEnabled | |
{ | |
get { return !_musicSource.mute; } | |
set | |
{ | |
if (_musicSource.mute == value) | |
{ | |
_musicSource.mute = !value; | |
_sfxSource.mute = !value; | |
PlayerPrefs.SetInt(audioEnabledKey, value ? 1 : 0); | |
if (!value) | |
{ | |
_musicSource.Stop(); | |
} | |
else | |
{ | |
_musicSource.Play(); | |
} | |
} | |
} | |
} | |
public float musicPlayTime | |
{ | |
get { return _musicSource.time; } | |
} | |
protected override void OnAwake() | |
{ | |
audioEnabled = !PlayerPrefs.HasKey(audioEnabledKey) || PlayerPrefs.GetInt(audioEnabledKey) == 1; | |
} | |
public void PlaySfx(string name) | |
{ | |
foreach (var clip in _soundEffects) | |
{ | |
if (clip.name == name) | |
{ | |
_sfxSource.PlayOneShot(clip); | |
break; | |
} | |
} | |
} | |
public void ToggleAudio() | |
{ | |
audioEnabled = !audioEnabled; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from somewhere else in your game you now just access the interface | |
// through the type and .instance property | |
AudioManager.instance.PlaySfx("MenuSelect"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment