using System;
using UnityEngine;
namespace Framework {
/// <summary>
/// 音效对象
/// </summary>
[RequireComponent (typeof (AudioSource))]
public class SoundObject : MonoBehaviour {
/// <summary>
/// 停止事件
/// </summary>
public event Action<SoundObject> OnStopEvent;
/// <summary>
/// 音效资源
/// </summary>
private AudioSource m_AudioSource;
private void Awake () {
m_AudioSource = GetComponent<AudioSource> ();
m_AudioSource.playOnAwake = false;
}
private void Update () {
if (!m_AudioSource.isPlaying) {
Stop ();
}
}
/// <summary>
/// 播放音效
/// </summary>
/// <param name="audioClip">音效剪辑</param>
/// <param name="loop">是否循环</param>
public void Play (AudioClip audioClip, bool loop = false) {
gameObject.name = audioClip.name;
gameObject.SetActive (true);
m_AudioSource.enabled = true;
m_AudioSource.clip = audioClip;
m_AudioSource.loop = loop;
m_AudioSource.Play ();
}
/// <summary>
/// 重新播放
/// </summary>
public void Replay () {
if (m_AudioSource.clip == null) {
return;
}
gameObject.SetActive (true);
m_AudioSource.enabled = true;
m_AudioSource.time = 0;
m_AudioSource.Play ();
}
/// <summary>
/// 停止音效
/// </summary>
/// <param name="destroy">是否销毁</param>
public void Stop (bool destroy = false) {
gameObject.SetActive (false);
m_AudioSource.Stop ();
m_AudioSource.clip = null;
m_AudioSource.enabled = false;
if (OnStopEvent != null) {
OnStopEvent.Invoke (this);
OnStopEvent = null;
}
if (destroy) {
Destroy (gameObject);
}
}
/// <summary>
/// 暂停音效
/// </summary>
public void Pause () {
gameObject.SetActive (false);
m_AudioSource.Pause ();
}
/// <summary>
/// 取消暂停
/// </summary>
public void UnPause () {
gameObject.SetActive (true);
m_AudioSource.UnPause ();
}
/// <summary>
/// 设置音乐音量
/// </summary>
/// <param name="val"></param>
public void SetVolume (float val) {
if (m_AudioSource != null) {
m_AudioSource.volume = val;
}
}
/// <summary>
/// 获取音乐音量
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public float GetVolume (float val) {
if (m_AudioSource != null) {
return m_AudioSource.volume;
}
return 0;
}
}
}