You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			89 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
			
		
		
	
	
			89 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace Game
 | |
| {
 | |
|     public delegate void PoolItemInitHandler<in TItem>(TItem item);
 | |
| 
 | |
|     public class ObjectPoolItemBase : MetaDataHandler
 | |
|     {
 | |
|         public bool IsAlive;
 | |
| 
 | |
|         public void Create()
 | |
|         {
 | |
|             if (IsAlive) return;
 | |
|             IsAlive = true;
 | |
|             OnCreate();
 | |
|             CreateBind();
 | |
|         }
 | |
| 
 | |
|         public void Destroy()
 | |
|         {
 | |
|             if (!IsAlive) return;
 | |
|             IsAlive = false;
 | |
|             OnDestroy();
 | |
|             ClearBind();
 | |
|         }
 | |
| 
 | |
|         protected virtual void OnCreate()
 | |
|         {
 | |
|         }
 | |
| 
 | |
|         protected virtual void OnDestroy()
 | |
|         {
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public class ObjectPoolBase<TManager> : ManagerBase<TManager> where TManager : new()
 | |
|     {
 | |
|         public GameObject Root;
 | |
|         protected Dictionary<object, List<ObjectPoolItemBase>> ObjectPool;
 | |
| 
 | |
|         public override void OnInit()
 | |
|         {
 | |
|             ObjectPool = new Dictionary<object, List<ObjectPoolItemBase>>();
 | |
|             CreateRoot(typeof(TManager).Name);
 | |
|         }
 | |
| 
 | |
|         private void CreateRoot(string rootName)
 | |
|         {
 | |
|             Root = new GameObject(rootName);
 | |
|             Object.DontDestroyOnLoad(Root);
 | |
|         }
 | |
| 
 | |
|         protected T Create<T>(object objType, PoolItemInitHandler<T> callback)
 | |
|             where T : ObjectPoolItemBase, new()
 | |
|         {
 | |
|             var pool = EnSureGetPool(objType);
 | |
|             foreach (var obj in from obj in pool where !obj.IsAlive select obj as T)
 | |
|             {
 | |
|                 obj.Create();
 | |
|                 return obj;
 | |
|             }
 | |
| 
 | |
|             var newObj = new T();
 | |
|             pool.Add(newObj);
 | |
|             callback?.Invoke(newObj);
 | |
|             newObj.Create();
 | |
|             return newObj;
 | |
|         }
 | |
| 
 | |
|         protected void PreCreate<T>(object objType, PoolItemInitHandler<T> callback, int num)
 | |
|             where T : ObjectPoolItemBase, new()
 | |
|         {
 | |
|             var pool = EnSureGetPool(objType);
 | |
|             for (var _ = 0; _ < num; _++)
 | |
|             {
 | |
|                 var newObj = Create(objType, callback);
 | |
|                 newObj.Destroy();
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         protected List<ObjectPoolItemBase> EnSureGetPool(object objType)
 | |
|         {
 | |
|             if (!ObjectPool.ContainsKey(objType)) ObjectPool[objType] = new List<ObjectPoolItemBase>();
 | |
|             return ObjectPool[objType];
 | |
|         }
 | |
|     }
 | |
| } |