SFXModule.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. using QFSW.MOP2;
  9. namespace KairoEngine.SFX
  10. {
  11. [Serializable, HideReferenceObjectPicker]
  12. public class SFXModule : GameModuleBase
  13. {
  14. public override string name => "SFX Module";
  15. [Tooltip("Use a custom sound emitter prefab or load the default prefab")]
  16. [HorizontalGroup("@name/emitter", 0.015f), LabelText("Custom Sound Emitter")] public bool useCustomSoundEmitter;
  17. [HorizontalGroup("@name/emitter", 0.985f), HideLabel, ShowIf("@useCustomSoundEmitter")] public GameObject soundEmitterPrefab;
  18. [FoldoutGroup("@name"), Tooltip("Use the ObjectPool system so that sound emitters can be reused. This results in better overral performance. Requires the Object Pool Module to be installed.")]
  19. public bool useObjectPooling = false;
  20. [HorizontalGroup("@name/pool", 0.015f), LabelText("Custom Object Pool"), ShowIf("@useObjectPooling")] public bool useCustomPool;
  21. [HorizontalGroup("@name/pool", 0.985f), HideLabel, ShowIf("@useCustomPool"), NonSerialized, ShowInInspector] public ObjectPool soundEmitterPool;
  22. [FoldoutGroup("@name"), Tooltip("Optional sound library asset")] public SFXLibrary soundLibrary;
  23. private SoundController soundController;
  24. public SFXModule(GameConfig config) : base(config)
  25. {
  26. this.gameConfig = config;
  27. this.className = this.GetType().AssemblyQualifiedName;
  28. this.typeName = "SFXModule";
  29. }
  30. public override void Load(Transform parent)
  31. {
  32. if(!useCustomSoundEmitter) soundEmitterPrefab = Resources.Load("SoundEmmiterPrefab") as GameObject;
  33. GameObject soundControllerPrefab = Resources.Load("SoundControllerPrefab") as GameObject;
  34. GameObject soundControllerObj = GameObject.Instantiate(soundControllerPrefab, parent);
  35. soundController = soundControllerObj.GetComponent<SoundController>();
  36. if(soundController == null)
  37. {
  38. Debug.LogError($"SoundControllerPrefab has no SoundController component", soundControllerObj);
  39. return;
  40. }
  41. soundController.soundEmitterPrefab = soundEmitterPrefab;
  42. if(soundLibrary != null) soundController.soundLibrary = soundLibrary;
  43. soundControllerObj.name = "SoundController";
  44. }
  45. public override void Start()
  46. {
  47. if(!useObjectPooling)
  48. {
  49. soundController._soundEmitterPool = "";
  50. SoundController.soundEmitterPool = "";
  51. soundEmitterPrefab.GetComponent<SoundEmitter>().soundEmitterPrefabPool = "";
  52. return;
  53. }
  54. if(soundController == null) return;
  55. soundController.soundEmitterPrefab = null;
  56. if(soundEmitterPool == null) soundEmitterPool = Resources.Load("SoundEmitterPool") as ObjectPool;
  57. if(soundEmitterPool == null)
  58. {
  59. Debug.LogError("Missing soundEmitterPool in SFX Module");
  60. return;
  61. }
  62. if(MasterObjectPooler.Instance == null)
  63. {
  64. Debug.LogError("The MasterObjectPooler component is not loaded. Add the Object Pool module in the game config to load it.");
  65. return;
  66. }
  67. soundEmitterPrefab.GetComponent<SoundEmitter>().soundEmitterPrefabPool = soundEmitterPool.PoolName;
  68. MasterObjectPooler.Instance.AddPool(soundEmitterPool.PoolName, soundEmitterPool);
  69. soundController._soundEmitterPool = soundEmitterPool.PoolName;
  70. SoundController.soundEmitterPool = soundEmitterPool.PoolName;
  71. var pool = MasterObjectPooler.Instance.GetPool(soundEmitterPool.PoolName);
  72. if(pool == null) Debug.LogError("Error configuring SFX pool.");
  73. else
  74. {
  75. soundController._soundsParent = pool.ObjectParent.gameObject;
  76. SoundController.soundsParent = pool.ObjectParent.gameObject;
  77. }
  78. }
  79. public override void Reset()
  80. {
  81. }
  82. public override void Destroy()
  83. {
  84. }
  85. public static SFXModule JSONToSFXModule(string data)
  86. {
  87. try
  88. {
  89. return JsonUtility.FromJson<SFXModule>(data);
  90. }
  91. catch (System.Exception e)
  92. {
  93. Debug.LogError($"Could not deserialize SFXModule: \n{e}");
  94. return new SFXModule(null);
  95. }
  96. }
  97. public override void OnBeforeSerialize(ObjectSerializer serializer)
  98. {
  99. if(soundEmitterPrefab != null) serializer.AddGameObject("SFXModule_soundEmitterPrefab", soundEmitterPrefab);
  100. if(soundEmitterPool != null) serializer.AddObject("SFXModule_soundEmitterPool", soundEmitterPool);
  101. }
  102. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  103. {
  104. soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab");
  105. soundEmitterPool = (ObjectPool)serializer.GetObject("SFXModule_soundEmitterPool");
  106. }
  107. }
  108. }