Browse Source

Added object pool module

James Peret 2 years ago
parent
commit
8de3af5bfd

+ 8 - 0
Resources.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 3290d7300f17f9b45a24848911b4accf
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 47 - 0
Resources/ObjectPoolPrefab.prefab

@@ -0,0 +1,47 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!1 &7927722991527368531
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 7927722991527368528}
+  - component: {fileID: 7927722991527368529}
+  m_Layer: 0
+  m_Name: ObjectPoolPrefab
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!4 &7927722991527368528
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 7927722991527368531}
+  m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_RootOrder: 0
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!114 &7927722991527368529
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 7927722991527368531}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: ae7d75d4e54c16b42b11bb89b17dc809, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  _singletonMode: 1
+  _pools: []

+ 9 - 0
Resources/ObjectPoolPrefab.prefab.meta

@@ -0,0 +1,9 @@
+fileFormatVersion: 2
+guid: 42a7afa14851012429a5cffbf9808e3f
+labels:
+- Manager
+PrefabImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 0
Runtime/ModuleSystem/GameConfig.cs

@@ -103,6 +103,7 @@ namespace KairoEngine.Core.ModuleSystem
                 }
                 if(module == null) continue;
                 module = GameModule.InvokeStringMethod(module.className, module.typeName, serializedModules[i]);
+                if(module == null) continue;
                 module.gameConfig = this;
                 module.OnBeforeDeserialize(serializer);
                 modules.Add(module);

+ 4 - 0
Runtime/ModuleSystem/GameInstaller.cs

@@ -23,6 +23,10 @@ namespace KairoEngine.Core.ModuleSystem
 
         public override void Start()
         {
+            for (int i = 0; i < gameConfig.modules.Count; i++)
+            {
+                if(gameConfig.modules[i].enableModule) gameConfig.modules[i].Start();
+            }
             for (int i = 0; i < gameConfig.configOptions.list.Count; i++)
             {
                 gameConfig.configOptions.list[i].LoadValue(gameConfig.configOptions.debug);

+ 5 - 0
Runtime/ModuleSystem/GameModule.cs

@@ -42,6 +42,11 @@ namespace KairoEngine.Core.ModuleSystem
             
         }
 
+        public virtual void Start()
+        {
+            
+        }
+
         [FoldoutGroup("@name")]
         public virtual void Remove()
         {

+ 93 - 0
Runtime/ObjectPoolModule.cs

@@ -0,0 +1,93 @@
+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.Core.ObjectPooling
+{
+    public class ObjectPoolModule : GameModuleBase
+    {
+        public override string name => "Object Pool Module";
+
+        [FoldoutGroup("@name")] public List<ObjectPool> pools = new List<ObjectPool>();
+        [SerializeField, HideInInspector] private int poolCount;
+
+        public ObjectPoolModule(GameConfig config) : base(config)
+        {
+            this.gameConfig = config;
+            this.className = this.GetType().AssemblyQualifiedName;
+            this.typeName = "ObjectPoolModule";
+        }
+
+        public override void Load(Transform parent)
+        {
+            GameObject ObjectPoolPrefab = Resources.Load("ObjectPoolPrefab") as GameObject;
+            if(ObjectPoolPrefab == null) 
+            {
+                Debug.LogError("Could not find ObjectPoolPrefab in ObjectPoolModule");
+                return;
+            }
+            GameObject objectPoolObj = GameObject.Instantiate(ObjectPoolPrefab, parent);
+            MasterObjectPooler masterObjectPooler = objectPoolObj.GetComponent<MasterObjectPooler>();
+            if(masterObjectPooler == null)
+            {
+                Debug.LogError("Could not find MasterObjectPooler component", objectPoolObj);
+                return;
+            }
+            for (int i = 0; i < pools.Count; i++)
+            {
+                masterObjectPooler.AddPool(pools[i]);
+            }
+
+        }
+
+        public override void Reset()
+        {
+            pools = new List<ObjectPool>();
+            poolCount = 0;
+        }
+
+        public override void Destroy()
+        {
+            
+        }
+
+        public static ObjectPoolModule JSONToObjectPoolModule(string data)
+        {
+            try
+            {
+                return JsonUtility.FromJson<ObjectPoolModule>(data);
+            }
+            catch (System.Exception e)
+            {
+                Debug.LogError($"Could not deserialize ObjectPoolModule: \n{e}");
+            }
+            return new ObjectPoolModule(null);
+        }
+
+        public override void OnBeforeSerialize(ObjectSerializer serializer) 
+        { 
+            if(pools != null)
+            {
+                poolCount = pools.Count;
+                for (int i = 0; i < pools.Count; i++)
+                {
+                    serializer.AddObject($"ObjectPool_pool_{i}", pools[i]);
+                }
+            }
+        }
+
+        public override void OnBeforeDeserialize(ObjectSerializer serializer) 
+        { 
+            pools = new List<ObjectPool>();
+            for (int i = 0; i < poolCount; i++)
+            {
+                pools.Add((ObjectPool)serializer.GetObject($"ObjectPool_pool_{i}"));
+            }
+        }
+    }
+}

+ 11 - 0
Runtime/ObjectPoolModule.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 0a612ba994b20b048a9a63db8af8b76c
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 2 - 0
Runtime/ObjectSerializer.cs

@@ -50,6 +50,7 @@ namespace KairoEngine.Core
         {
             int index = GetObjectIndex(key);
             if(index == -1) return null;
+            else if(index >= objectList.Count) return null;
             else return objectList[index];
         }
 
@@ -84,6 +85,7 @@ namespace KairoEngine.Core
         {
             int index = GetGameObjectIndex(key);
             if(index == -1) return null;
+            else if(index >= gameObjectList.Count) return null;
             else return gameObjectList[index];
         }