Browse Source

Added module config for object pooling

James Peret 2 years ago
parent
commit
8c29a25f0d

+ 21 - 0
Resources/SoundEmitterPool.asset

@@ -0,0 +1,21 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!114 &11400000
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 0}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 5ad40add42ecce646952aaa9475056c8, type: 3}
+  m_Name: SoundEmitterPool
+  m_EditorClassIdentifier: 
+  _name: SoundEmitter
+  _template: {fileID: 8894808049857426241, guid: 11f3d36bb078e034aa99a08d5a801014, type: 3}
+  _defaultSize: 12
+  _maxSize: -1
+  _incrementalInstanceNames: 0
+  _autoInitialize: 0
+  _repopulateOnSceneChange: 0

+ 8 - 0
Resources/SoundEmitterPool.asset.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: a3180164173a6d443a72d82f006807cc
+NativeFormatImporter:
+  externalObjects: {}
+  mainObjectFileID: 11400000
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 45 - 1
Runtime/SFXModule.cs

@@ -5,6 +5,7 @@ using UnityEngine;
 using KairoEngine.Core;
 using KairoEngine.Core.ModuleSystem;
 using Sirenix.OdinInspector;
+using QFSW.MOP2;
 
 namespace KairoEngine.SFX
 {
@@ -16,7 +17,12 @@ namespace KairoEngine.SFX
         [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)
         {
@@ -30,7 +36,7 @@ namespace KairoEngine.SFX
             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>();
+            soundController = soundControllerObj.GetComponent<SoundController>();
             if(soundController == null)
             {
                 Debug.LogError($"SoundControllerPrefab has no SoundController component", soundControllerObj);
@@ -41,6 +47,42 @@ namespace KairoEngine.SFX
             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()
         {
             
@@ -67,11 +109,13 @@ namespace KairoEngine.SFX
         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");
         }
     }
 }

+ 5 - 0
Runtime/SoundController.cs

@@ -78,6 +78,11 @@ namespace KairoEngine.SFX
 
         public static void EmmitSound(SFXClip clip, Vector3 position)
         {
+            if(SoundController.instance == null)
+            {
+                Debug.LogError("Missing SoundController component in scene.");
+                return;
+            }
             if(clip.sfxType == SFXClipType.Steps)
             {
                 SoundController ctrl = SoundController.instance;

+ 2 - 1
Runtime/SoundEmitter.cs

@@ -152,6 +152,7 @@ namespace KairoEngine.SFX
             // Destroy this Gameobject when the sound is done playing
             if(audioSource.isPlaying == false)
             {
+                if(sfx == null) return;
                 if(sfx.loop && sfx.sfxType == SFXClipType.RandomList)
                 {
                     Initialize(sfx, transform.position);
@@ -170,7 +171,7 @@ namespace KairoEngine.SFX
             // ! Enable when dependencies are resolved
             //if(sfx.removeOnSceneChange) TransitionEvents.OnChangeScene -= OnSceneChanged;
             sfx = null;
-            if(MasterObjectPooler.Instance != null) MasterObjectPooler.Instance.GetPool(soundEmitterPrefabPool).Release(this.gameObject);
+            if(MasterObjectPooler.Instance != null && soundEmitterPrefabPool != "") MasterObjectPooler.Instance.GetPool(soundEmitterPrefabPool).Release(this.gameObject);
             else Destroy(this.gameObject);
         }