63 lines
1.4 KiB
C#
63 lines
1.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class SoundPlayer : MonoBehaviour
|
|
{
|
|
private AudioSource source;
|
|
|
|
private Coroutine _coroutine;
|
|
|
|
private float _muteVolume;
|
|
|
|
private void Awake()
|
|
{
|
|
source = gameObject.AddComponent<AudioSource>();
|
|
}
|
|
|
|
public void Play(Sound sound)
|
|
{
|
|
_muteVolume = sound.MuteVolume;
|
|
|
|
source.clip = sound.Clip;
|
|
source.volume = sound.Volume;
|
|
source.pitch = sound.Pitch;
|
|
source.priority = sound.Priority;
|
|
source.playOnAwake = sound.PlayOnAwake;
|
|
source.loop = sound.Loop;
|
|
source.spatialBlend = sound.Zone;
|
|
source.Play();
|
|
|
|
if (!sound.Loop)
|
|
Destroy(gameObject, 40);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
source.Stop();
|
|
if (_coroutine != null)
|
|
StopCoroutine(_coroutine);
|
|
}
|
|
|
|
public void Mute(float time)
|
|
{
|
|
if (_coroutine != null)
|
|
StopCoroutine(_coroutine);
|
|
|
|
_coroutine = StartCoroutine(Mute_Coroutine(time));
|
|
}
|
|
|
|
private IEnumerator Mute_Coroutine(float time)
|
|
{
|
|
Debug.Log("Mute started");
|
|
float oldVolume = source.volume;
|
|
source.volume = _muteVolume;
|
|
|
|
yield return new WaitForSeconds(time + 1);
|
|
|
|
Debug.Log("Mute ended");
|
|
source.volume = oldVolume;
|
|
_coroutine = null;
|
|
}
|
|
}
|