using asap.core; using Game; using System.Collections; using UnityEngine; using UnityEngine.AddressableAssets; /// /// 播放当前主场景的BGM 钓场、建造、无尽建造 /// public struct EventMainBGM { } public class OutPlaySound : MonoBehaviour { public enum PlayerType { None, OnEnable, OnAwake, OnDisable, } public PlayerType playerType = PlayerType.OnEnable; public SoundMainType mainType = SoundMainType.UI; public bool isOutDisable = false; public AssetReferenceT assetReference; public float delay = 0f; public float startTimer = 0f; AssetReferenceAudioClip assetReferenceAudioClip; void Awake() { if (playerType == PlayerType.OnAwake) { PlaySound(); } } private void OnEnable() { if (playerType == PlayerType.OnEnable) { PlaySound(); } } private void OnDisable() { if (playerType == PlayerType.OnDisable) { PlaySound(); } if (isOutDisable) { if (mainType != SoundMainType.BGM) { GContext.Publish(new FadeOutEvent(assetReferenceAudioClip.Sound)); assetReferenceAudioClip.mainType = SoundMainType.None; } else { GContext.Publish(new EventMainBGM()); } } } IEnumerator DelayPlaySound() { yield return new WaitForSeconds(delay); Play(); } void Play() { // Debug.Log($"[Audio] {assetReference} Played on {gameObject}."); assetReferenceAudioClip = new AssetReferenceAudioClip(assetReference, isOutDisable, mainType, startTimer); GContext.Publish(assetReferenceAudioClip); } public void PlaySound() { if (assetReference != null && assetReference.RuntimeKeyIsValid()) { if (delay > 0.001f) { StartCoroutine(DelayPlaySound()); } else { Play(); } } } }