Files
2026-05-26 16:15:54 +08:00

91 lines
2.1 KiB
C#

using asap.core;
using Game;
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
/// <summary>
/// 播放当前主场景的BGM 钓场、建造、无尽建造
/// </summary>
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<AudioClip> 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($"<color=#57965c>[Audio] {assetReference} Played on {gameObject}.</color>");
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();
}
}
}
}