Created
September 3, 2016 05:29
-
-
Save dimmduh/c26e37a71f8bb3bbd96fc1386e603cf3 to your computer and use it in GitHub Desktop.
simple music manager for unity
This file contains hidden or 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
using com.ootii.Messages; | |
using UnityEngine; | |
using DG.Tweening; | |
namespace IOGames.Utils | |
{ | |
[RequireComponent(typeof(AudioSource))] | |
public class MusicManger : SingletonPrefab<MusicManger> | |
{ | |
private AudioSource _audioSource; | |
private float _volume = 1.0f; | |
public float volume | |
{ | |
get | |
{ | |
return _volume; | |
} | |
set | |
{ | |
_volume = value; | |
_audioSource.volume = _volume; | |
} | |
} | |
protected override void Awake() | |
{ | |
base.Awake(); | |
_audioSource = GetComponent<AudioSource>(); | |
MessageDispatcher.AddListener(GameSettings.MESSAGE_TYPE_MUSIC_VOLUME_CHANGED, OnMaxVolumeChanged); | |
} | |
public void FadeIn(float targetVolume, float fadeTime) | |
{ | |
Play(); | |
DOTween.To(() => volume, x => volume = x, targetVolume, fadeTime); | |
} | |
public void FadeOut(float fadeTime) | |
{ | |
DOTween | |
.To(() => volume, x => volume = x, 0, fadeTime) | |
.OnComplete(Pause); | |
} | |
public void Pause() | |
{ | |
_audioSource.Pause(); | |
} | |
public void Play(AudioClip audioClip) | |
{ | |
_audioSource.clip = audioClip; | |
Play(); | |
} | |
public void Play() | |
{ | |
_audioSource.Play(); | |
} | |
public void Stop() | |
{ | |
_audioSource.Stop(); | |
} | |
private void OnMaxVolumeChanged(IMessage rMessage) | |
{ | |
volume = (float)rMessage.Data; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment