83 lines
2.3 KiB
C#
Raw Normal View History

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<CuePlaySound>
{
private AudioSource _audioSource;
public CuePlaySoundSpec(CuePlaySound cue, GameplayCueParameters parameters) : base(cue,
parameters)
{
if (cue.isAttachToOwner)
{
2024-10-18 17:48:59 +08:00
_audioSource = Owner.GetView()?.gameObject.GetComponent<AudioSource>();
if (_audioSource == null)
{
2024-10-18 17:48:59 +08:00
_audioSource = Owner.GetView()?.gameObject.AddComponent<AudioSource>();
}
}
else
{
var soundRoot = new GameObject("SoundRoot");
2024-10-18 17:48:59 +08:00
if (Owner.GetView() is not null)
{
soundRoot.transform.position = Owner.GetView().transform.position;
}
_audioSource = soundRoot.AddComponent<AudioSource>();
}
}
2024-10-18 17:48:59 +08:00
public override void OnAdd(int frame,int startFrame,int endFrame)
{
_audioSource.clip = cue.soundEffect;
_audioSource.Play();
}
2024-10-18 17:48:59 +08:00
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()
{
}
2024-10-18 17:48:59 +08:00
public override void OnTick(int frame,int startFrame,int endFrame)
{
}
}
}