24 changed files with 193 additions and 119 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,139 @@ |
|||
using System.Collections; |
|||
using UnityEngine; |
|||
using UnityEngine.UI; |
|||
|
|||
namespace Core.Audio |
|||
{ |
|||
public sealed class PlayAudioFile : MonoBehaviour |
|||
{ |
|||
public enum SourceType |
|||
{ |
|||
Flat = 0, |
|||
Surround = 1, |
|||
} |
|||
|
|||
public enum AudioFileType |
|||
{ |
|||
Music = 0, |
|||
Sound = 1, |
|||
Voice = 2, |
|||
} |
|||
|
|||
public enum PlayType |
|||
{ |
|||
OnStart = 0, |
|||
OnClickButton = 1, |
|||
OnClickCollider = 2, |
|||
OnPlayParticle = 3, |
|||
} |
|||
|
|||
[SerializeField] private SourceType _sourceType = SourceType.Flat; |
|||
[SerializeField] private AudioFileType _audioFileType = AudioFileType.Music; |
|||
[SerializeField] private PlayType _playType = PlayType.OnStart; |
|||
[SerializeField] private float _startDelay = 0f; |
|||
[SerializeField] private bool _loop = false; |
|||
[SerializeField] private float _minDistance = 0f; |
|||
[SerializeField] private float _maxDistance = 100f; |
|||
[SerializeField] private string[] _audioFileNames = new string[0]; |
|||
|
|||
private ParticleSystem _particleSystem = null; |
|||
private Button _button = null; |
|||
|
|||
private bool _isPlaying = false; |
|||
|
|||
private void Awake() |
|||
{ |
|||
_button = GetComponent<Button>(); |
|||
|
|||
_particleSystem = GetComponent<ParticleSystem>(); |
|||
|
|||
if (_particleSystem == null) |
|||
_particleSystem = GetComponentInChildren<ParticleSystem>(); |
|||
} |
|||
|
|||
private IEnumerator Start() |
|||
{ |
|||
while (true) |
|||
{ |
|||
switch (_playType) |
|||
{ |
|||
case PlayType.OnStart: |
|||
StartCoroutine(CallPlay()); |
|||
yield break; |
|||
|
|||
case PlayType.OnPlayParticle: |
|||
|
|||
while (!_particleSystem.isPlaying) |
|||
yield return null; |
|||
|
|||
yield return StartCoroutine(CallPlay()); |
|||
|
|||
while (_particleSystem.isPlaying) |
|||
yield return null; |
|||
|
|||
break; |
|||
|
|||
case PlayType.OnClickButton: |
|||
|
|||
_button.onClick.AddListener(() => |
|||
{ |
|||
StartCoroutine(CallPlay()); |
|||
}); |
|||
|
|||
yield break; |
|||
|
|||
case PlayType.OnClickCollider: |
|||
|
|||
yield break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void OnMouseDown() |
|||
{ |
|||
if (_isPlaying || _playType != PlayType.OnClickCollider) |
|||
return; |
|||
|
|||
StartCoroutine(CallPlay()); |
|||
} |
|||
|
|||
private IEnumerator CallPlay() |
|||
{ |
|||
float time = 0f; |
|||
|
|||
if (_startDelay != 0f) |
|||
yield return new WaitForSeconds(_startDelay); |
|||
|
|||
switch (_audioFileType) |
|||
{ |
|||
case AudioFileType.Music: |
|||
|
|||
time = AudioController.PlayMusic(_audioFileNames[Random.Range(0, _audioFileNames.Length)], _loop); |
|||
break; |
|||
|
|||
case AudioFileType.Sound: |
|||
|
|||
if (_sourceType == SourceType.Flat) |
|||
time = AudioController.PlaySound(_audioFileNames[Random.Range(0, _audioFileNames.Length)], _loop); |
|||
else |
|||
time = AudioController.PlaySound(_audioFileNames[Random.Range(0, _audioFileNames.Length)], transform, _minDistance, _maxDistance, _loop); |
|||
break; |
|||
|
|||
case AudioFileType.Voice: |
|||
|
|||
if (_sourceType == SourceType.Flat) |
|||
time = AudioController.PlayVoice(_audioFileNames[Random.Range(0, _audioFileNames.Length)]); |
|||
else |
|||
time = AudioController.PlayVoice(_audioFileNames[Random.Range(0, _audioFileNames.Length)], transform, _minDistance, _maxDistance); |
|||
break; |
|||
|
|||
} |
|||
|
|||
_isPlaying = true; |
|||
|
|||
yield return new WaitForSeconds(time); |
|||
|
|||
_isPlaying = false; |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
@ -1 +1 @@ |
|||
7fd1797d3498be18a922a7bf796a7fa6c67322c0 |
|||
9862dcf43a2537ba94419b6dd7bcafa73d8c1851 |
|||
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue