123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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<SoundtrackManager>();
- manager.playlist = playlist;
- manager.audioSource1 = obj.AddComponent<AudioSource>();
- manager.audioSource2 = obj.AddComponent<AudioSource>();
- 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<SoundtrackModule>(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");
- }
- }
- }
|