SoundtrackModule.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Audio;
  6. using KairoEngine.Core;
  7. using KairoEngine.Core.ModuleSystem;
  8. using Sirenix.OdinInspector;
  9. namespace KairoEngine.SoundtrackSystem
  10. {
  11. [Serializable, HideReferenceObjectPicker]
  12. public class SoundtrackModule : GameModuleBase
  13. {
  14. public override string name => "Soundtrack Module";
  15. [NonSerialized, ShowInInspector] public AudioMixerGroup audioMixerChannel;
  16. [NonSerialized, ShowInInspector]
  17. public Playlist playlist;
  18. public int currentTrackIndex = 0;
  19. public bool playOnStart = true;
  20. public bool showDebug = false;
  21. public SoundtrackModule(GameConfig config) : base(config)
  22. {
  23. this.gameConfig = config;
  24. this.className = this.GetType().AssemblyQualifiedName;
  25. this.typeName = "SoundtrackModule";
  26. }
  27. public override void Load(Transform parent)
  28. {
  29. GameObject obj = new GameObject();
  30. obj.transform.parent = parent;
  31. obj.transform.position = new Vector3();
  32. obj.transform.rotation = Quaternion.identity;
  33. obj.name = "SoundtrackManager";
  34. SoundtrackManager manager = obj.AddComponent<SoundtrackManager>();
  35. manager.playlist = playlist;
  36. manager.audioSource1 = obj.AddComponent<AudioSource>();
  37. manager.audioSource2 = obj.AddComponent<AudioSource>();
  38. manager.audioSource1.playOnAwake = false;
  39. manager.audioSource2.playOnAwake = false;
  40. manager.audioSource1.outputAudioMixerGroup = audioMixerChannel;
  41. manager.audioSource2.outputAudioMixerGroup = audioMixerChannel;
  42. manager.playOnStart = this.playOnStart;
  43. manager.showDebug = this.showDebug;
  44. manager.currentTrackIndex = this.currentTrackIndex;
  45. }
  46. public override void Start()
  47. {
  48. }
  49. public override void Reset()
  50. {
  51. this.playlist = null;
  52. this.currentTrackIndex = 0;
  53. this.playOnStart = true;
  54. this.showDebug = false;
  55. }
  56. public override void Destroy()
  57. {
  58. }
  59. public static SoundtrackModule JSONToSoundtrackModule(string data)
  60. {
  61. try
  62. {
  63. return JsonUtility.FromJson<SoundtrackModule>(data);
  64. }
  65. catch (System.Exception e)
  66. {
  67. Debug.LogError($"Could not deserialize SoundtrackModule: \n{e}");
  68. return new SoundtrackModule(null);
  69. }
  70. }
  71. public override void OnBeforeSerialize(ObjectSerializer serializer)
  72. {
  73. if(playlist != null) serializer.AddScriptableObject("SoundtrackModule_Playlist", playlist);
  74. if(audioMixerChannel != null) serializer.AddUnityObject("SoundtrackModule_audioMixerChannel", audioMixerChannel);
  75. }
  76. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  77. {
  78. playlist = (Playlist)serializer.GetScriptableObject("SoundtrackModule_Playlist");
  79. audioMixerChannel = (AudioMixerGroup)serializer.GetUnityObject("SoundtrackModule_audioMixerChannel");
  80. }
  81. }
  82. }