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 pools = new List(); [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); objectPoolObj.name = "ObjectPool"; MasterObjectPooler masterObjectPooler = objectPoolObj.GetComponent(); 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(); poolCount = 0; } public override void Destroy() { } public static ObjectPoolModule JSONToObjectPoolModule(string data) { try { return JsonUtility.FromJson(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(); for (int i = 0; i < poolCount; i++) { pools.Add((ObjectPool)serializer.GetObject($"ObjectPool_pool_{i}")); } } } }