|
@@ -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}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|