using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio; using KairoEngine.Core; using KairoEngine.Core.ModuleSystem; using Sirenix.OdinInspector; namespace KairoEngine.SoundtrackSystem { [Serializable, HideReferenceObjectPicker] public class SoundtrackModule : GameModuleBase { public override string name => "Soundtrack Module"; [NonSerialized, ShowInInspector] public AudioMixerGroup audioMixerChannel; [NonSerialized, ShowInInspector] public Playlist playlist; public int currentTrackIndex = 0; public bool playOnStart = true; public bool showDebug = false; public SoundtrackModule(GameConfig config) : base(config) { this.gameConfig = config; this.className = this.GetType().AssemblyQualifiedName; this.typeName = "SoundtrackModule"; } public override void Load(Transform parent) { GameObject obj = new GameObject(); obj.transform.parent = parent; obj.transform.position = new Vector3(); obj.transform.rotation = Quaternion.identity; obj.name = "SoundtrackManager"; SoundtrackManager manager = obj.AddComponent(); manager.playlist = playlist; manager.audioSource1 = obj.AddComponent(); manager.audioSource2 = obj.AddComponent(); manager.audioSource1.playOnAwake = false; manager.audioSource2.playOnAwake = false; manager.audioSource1.outputAudioMixerGroup = audioMixerChannel; manager.audioSource2.outputAudioMixerGroup = audioMixerChannel; manager.playOnStart = this.playOnStart; manager.showDebug = this.showDebug; manager.currentTrackIndex = this.currentTrackIndex; } public override void Start() { } public override void Reset() { this.playlist = null; this.currentTrackIndex = 0; this.playOnStart = true; this.showDebug = false; } public override void Destroy() { } public static SoundtrackModule JSONToSoundtrackModule(string data) { try { return JsonUtility.FromJson(data); } catch (System.Exception e) { Debug.LogError($"Could not deserialize SoundtrackModule: \n{e}"); return new SoundtrackModule(null); } } public override void OnBeforeSerialize(ObjectSerializer serializer) { if(playlist != null) serializer.AddScriptableObject("SoundtrackModule_Playlist", playlist); if(audioMixerChannel != null) serializer.AddUnityObject("SoundtrackModule_audioMixerChannel", audioMixerChannel); } public override void OnBeforeDeserialize(ObjectSerializer serializer) { playlist = (Playlist)serializer.GetScriptableObject("SoundtrackModule_Playlist"); audioMixerChannel = (AudioMixerGroup)serializer.GetUnityObject("SoundtrackModule_audioMixerChannel"); } } }