当前位置:网站首页>Simple implementation of unity object pool
Simple implementation of unity object pool
2022-06-30 01:44:00 【caoDanYao】
unity Simple implementation of object pool , You can use it directly
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
public float DeadTime = 60f;
private static ObjectPool instance;
private ObjectPool()
{
pool = new Dictionary<string, List<GameObject>>();
prefabs = new Dictionary<string, GameObject>();
}
public static ObjectPool Instance
{
get
{
if (instance == null)
{
instance = new ObjectPool();
}
return instance;
}
}
/// <summary>
/// Object pool
/// </summary>
private Dictionary<string, List<GameObject>> pool;
/// <summary>
/// Presupposition
/// </summary>
private Dictionary<string, GameObject> prefabs;
/// <summary>
/// Get objects from the object pool
/// </summary>
/// <param name="objName"></param>
/// <returns></returns>
public GameObject GetObj(string objName, string groupName = "ObjectPool")
{
// Result object
GameObject result = null;
// Determine whether there is an object pool with this name
if (pool.ContainsKey(objName))
{
// There are objects in the object pool
if (pool[objName].Count > 0)
{
// To get the results
result = pool[objName][0];
// Activate the object
result.SetActive(true);
// Remove the object from the pool
pool[objName].Remove(result);
// Return results
return result;
}
}
// If there is no named object pool or the named object pool has no objects
GameObject prefab = null;
// If the preset has already been loaded
if (prefabs.ContainsKey(objName))
{
prefab = prefabs[objName];
}
else // If the preset has not been loaded
{
// Load preset
prefab = Resources.Load<GameObject>("Prefabs/" + objName);
// Update Dictionary
prefabs.Add(objName, prefab);
}
// Generate
result = UnityEngine.Object.Instantiate(prefab);
// Change of name ( Remove Clone)
result.name = objName;
// Increase sublease
var parentGo = GameObject.Find(groupName);
if (parentGo == null)
{
parentGo = new GameObject(groupName);
}
result.transform.SetParent(parentGo.transform);
// return
return result;
}
/// <summary>
/// Recycle objects to the object pool
/// </summary>
/// <param name="objName"></param>
public void RecycleObj(GameObject obj)
{
// Set to inactive
obj.SetActive(false);
// Determine whether there is an object pool for this object
if (pool.ContainsKey(obj.name))
{
// Put into the object pool
pool[obj.name].Add(obj);
}
else
{
// Create a pool of this type , And put the object into
pool.Add(obj.name, new List<GameObject>() {
obj });
}
}
}
Usage method :
Place the preform in Assets\Resources\Prefabs Under the table of contents ,
You need to create objects ObjectPool.GetObj(objName,ObjectPool);
You need to recycle objects RecycleObj(obj);(ps: You can do it yourself GameObject Implement the extension method );
边栏推荐
- (1) Basic learning - figure out the difference between pin, pad, port, IO and net
- OpenCV和Image之间的转换(亲测有效)
- C语言 成绩排名
- Who can use redis expired monitoring to close orders and get out of here!
- Varnish foundation overview 7
- [machine learning Q & A] accuracy, accuracy, recall, ROC and AUC
- 当大学毕业感到迷茫怎么办?
- Derivation of univariate polynomial in C language
- 对深度网络模型量化工作的总结
- 搞透AQS原理(流程圖及同步隊列圖解)
猜你喜欢
随机推荐
Interface Association of postman
C language continues (3n+1) conjecture
Mysql 监控5
js Array.from()的5个便捷应用
Ansible ad-hoc temporary command
Cookie加密10
对深度网络模型量化工作的总结
Tools and life services
js内容混淆,返回内容加密
Cookie encryption 13
Mysql 监控3
Cookie encryption 8
[MRCTF2020]Ezpop-1|php序列化
Conjecture of prime pairs in C language
ctfshow 大赛原题 680-695
Cookie encryption 10
Varnish foundation overview 4
How to seamlessly transition from traditional microservice framework to service grid ASM
cookie加密8
JS returned content is encoded by Unicode








