123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Core;
- using KairoEngine.Core.ModuleSystem;
- using Sirenix.OdinInspector;
- using QFSW.MOP2;
- namespace KairoEngine.SFX
- {
- [Serializable, HideReferenceObjectPicker]
- public class SFXModule : GameModuleBase
- {
- public override string name => "SFX Module";
- [Tooltip("Use a custom sound emitter prefab or load the default prefab")]
- [HorizontalGroup("@name/emitter", 0.015f), LabelText("Custom Sound Emitter")] public bool useCustomSoundEmitter;
- [HorizontalGroup("@name/emitter", 0.985f), HideLabel, ShowIf("@useCustomSoundEmitter")] public GameObject soundEmitterPrefab;
- [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.")]
- public bool useObjectPooling = false;
- [HorizontalGroup("@name/pool", 0.015f), LabelText("Custom Object Pool"), ShowIf("@useObjectPooling")] public bool useCustomPool;
- [HorizontalGroup("@name/pool", 0.985f), HideLabel, ShowIf("@useCustomPool"), NonSerialized, ShowInInspector] public ObjectPool soundEmitterPool;
- [FoldoutGroup("@name"), Tooltip("Optional sound library asset")] public SFXLibrary soundLibrary;
- private SoundController soundController;
- public SFXModule(GameConfig config) : base(config)
- {
- this.gameConfig = config;
- this.className = this.GetType().AssemblyQualifiedName;
- this.typeName = "SFXModule";
- }
- public override void Load(Transform parent)
- {
- if(!useCustomSoundEmitter) soundEmitterPrefab = Resources.Load("SoundEmmiterPrefab") as GameObject;
- GameObject soundControllerPrefab = Resources.Load("SoundControllerPrefab") as GameObject;
- GameObject soundControllerObj = GameObject.Instantiate(soundControllerPrefab, parent);
- soundController = soundControllerObj.GetComponent<SoundController>();
- if(soundController == null)
- {
- Debug.LogError($"SoundControllerPrefab has no SoundController component", soundControllerObj);
- return;
- }
- soundController.soundEmitterPrefab = soundEmitterPrefab;
- if(soundLibrary != null) soundController.soundLibrary = soundLibrary;
- soundControllerObj.name = "SoundController";
- }
- public override void Start()
- {
-
- if(!useObjectPooling)
- {
- soundController._soundEmitterPool = "";
- SoundController.soundEmitterPool = "";
- soundEmitterPrefab.GetComponent<SoundEmitter>().soundEmitterPrefabPool = "";
- return;
- }
- if(soundController == null) return;
- soundController.soundEmitterPrefab = null;
- if(soundEmitterPool == null) soundEmitterPool = Resources.Load("SoundEmitterPool") as ObjectPool;
- if(soundEmitterPool == null)
- {
- Debug.LogError("Missing soundEmitterPool in SFX Module");
- return;
- }
- if(MasterObjectPooler.Instance == null)
- {
- Debug.LogError("The MasterObjectPooler component is not loaded. Add the Object Pool module in the game config to load it.");
- return;
- }
- soundEmitterPrefab.GetComponent<SoundEmitter>().soundEmitterPrefabPool = soundEmitterPool.PoolName;
- MasterObjectPooler.Instance.AddPool(soundEmitterPool.PoolName, soundEmitterPool);
- soundController._soundEmitterPool = soundEmitterPool.PoolName;
- SoundController.soundEmitterPool = soundEmitterPool.PoolName;
- var pool = MasterObjectPooler.Instance.GetPool(soundEmitterPool.PoolName);
- if(pool == null) Debug.LogError("Error configuring SFX pool.");
- else
- {
- soundController._soundsParent = pool.ObjectParent.gameObject;
- SoundController.soundsParent = pool.ObjectParent.gameObject;
- }
- }
- public override void Reset()
- {
-
- }
- public override void Destroy()
- {
-
- }
- public static SFXModule JSONToSFXModule(string data)
- {
- try
- {
- return JsonUtility.FromJson<SFXModule>(data);
- }
- catch (System.Exception e)
- {
- Debug.LogError($"Could not deserialize SFXModule: \n{e}");
- return new SFXModule(null);
- }
- }
- public override void OnBeforeSerialize(ObjectSerializer serializer)
- {
- if(soundEmitterPrefab != null) serializer.AddGameObject("SFXModule_soundEmitterPrefab", soundEmitterPrefab);
- if(soundEmitterPool != null) serializer.AddObject("SFXModule_soundEmitterPool", soundEmitterPool);
- }
- public override void OnBeforeDeserialize(ObjectSerializer serializer)
- {
- soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab");
- soundEmitterPool = (ObjectPool)serializer.GetObject("SFXModule_soundEmitterPool");
- }
- }
- }
|