using System.Collections.Generic; using asap.core; using Game; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.Serialization; namespace UIExtend { public class AnimatorWithSoundStateMachine : StateMachineBehaviour { [SerializeField] private List> UISounds; [SerializeField] private List> UILoopSounds; private Dictionary _sounds; private void SetEffectObjState(Animator animator,bool isActive) { _sounds ??= new Dictionary(); if (UISounds is { Count: > 0 }) foreach (var audioClip in UISounds) PlaySound(isActive, audioClip,SoundMainType.UI); if (UILoopSounds is { Count: > 0 }) foreach (var audioClip in UILoopSounds) PlaySound(isActive, audioClip,SoundMainType.UILoop); } private void PlaySound(bool isActive, AssetReferenceT soundName,SoundMainType mainType) { if (isActive) { if (!_sounds.TryGetValue(soundName.AssetGUID, out var soundEvent)) { soundEvent = new AssetReferenceAudioClip(soundName) { mainType = mainType, }; _sounds.TryAdd(soundName.AssetGUID,soundEvent); } GContext.Publish(soundEvent); } else { if(_sounds.TryGetValue(soundName.AssetGUID, out var soundEvent)) GContext.Publish(new FadeOutEvent(soundEvent.Sound,0)); } } public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { SetEffectObjState(animator,true); } public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { SetEffectObjState(animator,false); } } }