SFXModule.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using KairoEngine.Core;
  6. using KairoEngine.Core.ModuleSystem;
  7. using Sirenix.OdinInspector;
  8. namespace KairoEngine.SFX
  9. {
  10. [Serializable, HideReferenceObjectPicker]
  11. public class SFXModule : GameModuleBase
  12. {
  13. public override string name => "SFX Module";
  14. [Tooltip("Use a custom sound emitter prefab or load the default prefab")]
  15. [HorizontalGroup("@name/emitter", 0.015f), LabelText("Custom Sound Emitter")] public bool useCustomSoundEmitter;
  16. [HorizontalGroup("@name/emitter", 0.985f), HideLabel, ShowIf("@useCustomSoundEmitter")] public GameObject soundEmitterPrefab;
  17. [FoldoutGroup("@name"), Tooltip("Optional sound library asset")] public SFXLibrary soundLibrary;
  18. public SFXModule(GameConfig config) : base(config)
  19. {
  20. this.gameConfig = config;
  21. this.className = this.GetType().AssemblyQualifiedName;
  22. this.typeName = "SFXModule";
  23. }
  24. public override void Load(Transform parent)
  25. {
  26. if(!useCustomSoundEmitter) soundEmitterPrefab = Resources.Load("SoundEmmiterPrefab") as GameObject;
  27. GameObject soundControllerPrefab = Resources.Load("SoundControllerPrefab") as GameObject;
  28. GameObject soundControllerObj = GameObject.Instantiate(soundControllerPrefab, parent);
  29. SoundController soundController = soundControllerObj.GetComponent<SoundController>();
  30. if(soundController == null)
  31. {
  32. Debug.LogError($"SoundControllerPrefab has no SoundController component", soundControllerObj);
  33. return;
  34. }
  35. soundController.soundEmitterPrefab = soundEmitterPrefab;
  36. if(soundLibrary != null) soundController.soundLibrary = soundLibrary;
  37. soundControllerObj.name = "SoundController";
  38. }
  39. public override void Reset()
  40. {
  41. }
  42. public override void Destroy()
  43. {
  44. }
  45. public static SFXModule JSONToSFXModule(string data)
  46. {
  47. try
  48. {
  49. return JsonUtility.FromJson<SFXModule>(data);
  50. }
  51. catch (System.Exception e)
  52. {
  53. Debug.LogError($"Could not deserialize SFXModule: \n{e}");
  54. return new SFXModule(null);
  55. }
  56. }
  57. public override void OnBeforeSerialize(ObjectSerializer serializer)
  58. {
  59. if(soundEmitterPrefab != null) serializer.AddGameObject("SFXModule_soundEmitterPrefab", soundEmitterPrefab);
  60. }
  61. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  62. {
  63. soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab");
  64. }
  65. }
  66. }