1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using KairoEngine.Core;
- using KairoEngine.Core.ModuleSystem;
- using Sirenix.OdinInspector;
- 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("Optional sound library asset")] public SFXLibrary soundLibrary;
- 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 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 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);
- }
- public override void OnBeforeDeserialize(ObjectSerializer serializer)
- {
- soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab");
- }
- }
- }
|