#if INVECTOR_PLAYABLESANIMATOR using System.Collections.Generic; using InvectorCore.Playables; using UnityEngine; namespace Invector { /// /// PlayablesAnimator counterpart of . Plays one of /// when the state crosses , re-arming per loop. /// public class vTriggerSoundByStatePDM : PlayablesStateBehaviour { public GameObject audioSource; public List sounds; public float triggerTime; [System.NonSerialized] private vFisherYatesRandom _random; [System.NonSerialized] private bool isTrigger; public override void OnStateEnter(in PlayablesStateContext context) { isTrigger = false; } public override void OnStateUpdate(in PlayablesStateContext context) { float normalizedTime = context.normalizedTime % 1f; if (normalizedTime >= triggerTime && !isTrigger) TriggerSound(context); else if (normalizedTime < triggerTime && isTrigger) isTrigger = false; } private void TriggerSound(in PlayablesStateContext context) { if (context.Animator == null || sounds == null || sounds.Count == 0) return; _random ??= new vFisherYatesRandom(); isTrigger = true; Vector3 position = context.Animator.transform.position; GameObject audioObject; if (audioSource != null) { audioObject = Instantiate(audioSource, position, Quaternion.identity); } else { audioObject = new GameObject("audioObject"); audioObject.transform.position = position; } AudioSource source = audioObject.GetComponent(); if (source == null) return; source.PlayOneShot(sounds[_random.Next(sounds.Count)]); } } } #endif