当前位置:网站首页>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 );
边栏推荐
- Varnish 基础概览4
- cookie加密11
- Circular right shift of array elements in C language
- The 8th "Internet +" competition - cloud native track invites you to challenge
- Varnish foundation overview 1
- Cookie加密13
- [pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars
- Comprendre le principe AQS (organigramme et schéma de file d'attente synchrone)
- MySQL monitoring 3
- Cookie encryption 13
猜你喜欢
随机推荐
Cookie encryption 11
Varnish 基础概览7
AI landing manufacturing: intelligent robots should have these four abilities
Conversion between opencv and image (valid for pro test)
js逆向请求参数加密:
What is idempotency? Detailed explanation of four interface idempotence schemes!
[mrctf2020]ezpop-1 | PHP serialization
App test related tools
Mysql 监控1
Where can I find a pre training model for pytoch model training?
Varnish 基础概览10
cookie加密8
DTW learning (dynamic time warping) -- Thought and code implementation
C language I want to pass
Mysql 监控5
[pytorch actual combat] generate confrontation network Gan: generate cartoon character avatars
关于c语言main函数中int argc,char **argv的理解
ES6 synchronous asynchronous execution and block level scope
Sorting out the usage of transforms in pytoch
Module import reload method




![【图神经网络】图分类学习研究综述[3]:图分类方法评价及未来研究方向](/img/b1/2afa73a14b2f41b7a65c4c2d261e6a.png)




