using System; using System.Linq; using AK.Wwise; using UnityEngine; using Wwise.Wooduan.Core; using Wwise.Wooduan.Tools; namespace Wwise.Wooduan.Components { public enum AudioEventAction { Play, Stop } // Place to save audio event and frame mapping [Serializable] public class LegacyAnimationAudioEvent { public string clipName; public int frame; public AK.Wwise.Event audioEvent; public AudioEventAction action; } /// /// Post or stop Wwise events with legacy animation component. /// [AddComponentMenu("Wwise/LegacyAnimationSound")] [RequireComponent(typeof(Animation))] [DisallowMultipleComponent] public class LegacyAnimationSound : AkComponent { public int frameRate = 60; public GameObject emitter; public LegacyAnimationAudioEvent[] audioEvents = new LegacyAnimationAudioEvent[0]; private void Start() { if (IsValid()) RegisterEvents(); if (!emitter) emitter = gameObject; } // temporarily add events to animation clip when initialized private void RegisterEvents() { var anim = GetComponent(); if (!anim) return; foreach (var evt in audioEvents) { var clip = anim.GetClip(evt.clipName); if (!clip) continue; var existingEvent = clip.events.FirstOrDefault(e => e.stringParameter == evt.audioEvent.Name); if (existingEvent != null) continue; var newEvent = new AnimationEvent { time = evt.frame * 1f / frameRate, stringParameter = evt.audioEvent.Name }; switch (evt.action) { case AudioEventAction.Play: newEvent.functionName = "Play"; break; case AudioEventAction.Stop: newEvent.functionName = "Stop"; break; } clip.AddEvent(newEvent); } } public void Play(string eventName) { if (eventName.StartsWith("Music_")) AudioManager.PlayMusic(eventName, emitter, AudioTriggerSource.AnimationSound); else if (eventName.StartsWith("Vo_")) AudioManager.PlayVoice(eventName, emitter, AudioTriggerSource.AnimationSound); else AudioManager.PlaySound(eventName, emitter, AudioTriggerSource.AnimationSound); } public void Stop(string eventName) { if (eventName.StartsWith("Music_")) AudioManager.StopMusic(emitter, AudioTriggerSource.AnimationSound); else AudioManager.StopEvent(eventName, emitter, 0.1f, AudioTriggerSource.AnimationSound); } public override bool IsValid() { return audioEvents.Any(e => e.audioEvent.IsValid()); } } }