| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using UnityEngine;
- using System.Collections;
- public class AudioAttach : MonoBehaviour
- {
- public static bool ProxyMode = false;
- public string clipName;
- public string clipPackageName;
- public bool repeat = false;
- public float delayTime = 0f;
- public float minInterval = 0f;
- public float maxInterval = 0f;
- public int countLimit = 0;
- /// <summary>
- /// 是否Awake时就播放
- /// </summary>
- public bool playOnAwake = false;
- /// <summary>
- /// 脚本删除之后声音继续播放
- /// </summary>
- public bool continuePlayAfterDestroy = false;
- float timeFromEnable = 0f;
- float nextInterval = 0f;
- int playCount = 0;
- /// <summary>
- /// 请求播放的声音序列号
- /// </summary>
- private ulong mClipOrder = 0;
- void Awake()
- {
- if (string.IsNullOrEmpty(clipName))
- {
- enabled = false;
- return;
- }
- clipName = clipName.Trim();
- clipPackageName = clipPackageName.Trim();
- if (playOnAwake)
- {
- CheckSound();
- }
- }
- void OnEnable()
- {
- timeFromEnable = 0f;
- timeFromEnable = 0f;
- nextInterval = 0f;
- playCount = 0;
- }
- void OnDisable()
- {
- ReleaseSound();
- }
- bool MultiPlay
- {
- get
- {
- return maxInterval > 0f || minInterval > 0f;
- }
- }
- // Update is called once per frame
- void Update()
- {
- CheckSound();
- }
- void CheckSound()
- {
- if (string.IsNullOrEmpty(clipName))
- return;
- if (timeFromEnable < delayTime)
- {
- timeFromEnable += Time.unscaledDeltaTime;
- return;
- }
- if (!MultiPlay)
- {
- if (playCount > 0)
- return;
- }
- else
- {
- if (countLimit > 0 && playCount >= countLimit)
- return;
- }
- if (nextInterval <= 0f)
- {
- PlaySound();
- playCount++;
- if (minInterval < maxInterval)
- nextInterval = Random.Range(minInterval, maxInterval);
- else if (minInterval > 0)
- nextInterval = minInterval;
- }
- else
- nextInterval -= Time.unscaledDeltaTime;
- }
- void PlaySound()
- {
- mClipOrder = AudioProxy.Instance.Add(clipName, clipPackageName, repeat && !MultiPlay);
- }
- void OnDestroy()
- {
- ReleaseSound();
- }
- void ReleaseSound()
- {
- if (!continuePlayAfterDestroy)
- {
- AudioProxy.Instance.Cancel(clipName);
- }
- }
- }
|