using System; using GAS.General; using Sirenix.OdinInspector; using UnityEngine; using Object = UnityEngine.Object; namespace GAS.Runtime { public class CuePlaySound : GameplayCueDurational { [BoxGroup] [LabelText(GASTextDefine.CUE_SOUND_EFFECT)] public AudioClip soundEffect; [BoxGroup] [LabelText(GASTextDefine.CUE_ATTACH_TO_OWNER)] public bool isAttachToOwner = true; public override GameplayCueDurationalSpec CreateSpec(GameplayCueParameters parameters) { return new CuePlaySoundSpec(this, parameters); } } public class CuePlaySoundSpec : GameplayCueDurationalSpec { private AudioSource _audioSource; public CuePlaySoundSpec(CuePlaySound cue, GameplayCueParameters parameters) : base(cue, parameters) { if (cue.isAttachToOwner) { _audioSource = Owner.GetView()?.gameObject.GetComponent(); if (_audioSource == null) { _audioSource = Owner.GetView()?.gameObject.AddComponent(); } } else { var soundRoot = new GameObject("SoundRoot"); if (Owner.GetView() is not null) { soundRoot.transform.position = Owner.GetView().transform.position; } _audioSource = soundRoot.AddComponent(); } } public override void OnAdd(int frame,int startFrame,int endFrame) { _audioSource.clip = cue.soundEffect; _audioSource.Play(); } public override void OnRemove(int frame,int startFrame,int endFrame) { if (!cue.isAttachToOwner) { Object.Destroy(_audioSource.gameObject); }else { _audioSource.Stop(); _audioSource.clip = null; } } public override void OnGameplayEffectActivate() { } public override void OnGameplayEffectDeactivate() { } public override void OnTick(int frame,int startFrame,int endFrame) { } } }