当前位置:网站首页>[unity learning 023] object pool Pro
[unity learning 023] object pool Pro
2022-07-17 23:39:00 【ThursdayGame】
Design an object pool manager , Used to control different object pools , Get objects from different pools through parameters , The corresponding pool returns the required object according to its own situation .
The script is as follows :
1. The design of the pool . Include new / Delete / Recycling and other methods .
public class Pool
{
private GameObject ori;
public Pool(GameObject ori)
{
this.ori = ori;
}
public List<GameObject> store = new List<GameObject>();
public List<GameObject> uesd = new List<GameObject>();
public GameObject GetObj()
{
GameObject temp;
if (store.Count > 0)
{
temp = store[0];
store.RemoveAt(0);
}
else
{
temp = GameObject.Instantiate(ori);
var key = temp.AddComponent<PoolKey>();
key.key = ori.name;
}
uesd.Add(temp);
return temp;
}
public void RestoreObj(GameObject temp)
{
if (uesd.Contains(temp))
{
uesd.Remove(temp);
}
store.Add(temp);
}
public void DestoryObj(GameObject obj)
{
if (Application.isPlaying)
{
UnityEngine.GameObject.Destroy(obj);
}
else
{
UnityEngine.Object.DestroyImmediate(obj);
}
}
public void Clear()
{
int length = store.Count;
for (int i = length - 1; i >= 0; i--)
{
DestoryObj(store[i]);
}
length = uesd.Count;
for (int i = length - 1; i >= 0; i--)
{
DestoryObj(uesd[i]);
}
store.Clear();
uesd.Clear();
}
}2. Controller design .
/// <summary>
/// Universal object pool controller , It is used to control different pools to corresponding objects from different pools
/// </summary>
public class ObjController : MonoBehaviour
{
[ShowInInspector]
public static Dictionary<string, Pool> poolManager = new Dictionary<string, Pool>();
// Generate a game object
public static GameObject CreateObj(GameObject ori)
{
GameObject temp;
if (poolManager.ContainsKey(ori.name) == false)
{
poolManager[ori.name] = new Pool(ori);
}
temp = poolManager[ori.name].GetObj();
return temp;
}
// Recycle a game object
public static void Restore(GameObject obj)
{
var poolKey = obj.GetComponent<PoolKey>();
if (poolKey && poolManager.ContainsKey(poolKey.key))
{
poolManager[poolKey.key].RestoreObj(obj);
}
else
{
Debug.Log($"<color=red>{obj.name} This object cannot be recycled , speculation 1:poolKey==null({poolKey == null}),2: Without this pool </color>");
}
}
// Empty the object pool of a game object , By the way, delete all objects in the pool
public static void ClearObjPool(GameObject obj)
{
if (poolManager.ContainsKey(obj.name))
{
poolManager[obj.name].Clear();
poolManager.Remove(obj.name);
}
else
{
Debug.Log("<color=red> Clearing failed </color>");
}
}
public static void ClearAll()
{
foreach (var item in poolManager)
{
item.Value.Clear();
}
poolManager.Clear();
}
}3. For the convenience of finding , Each object has a key.
public class PoolKey : MonoBehaviour
{
public string key;
[ShowInInspector]
public int id { get => GetInstanceID(); }
}There's room for optimization . Use... For the time being .
边栏推荐
- Byte encountered a wave of resignation!
- Vs2019 Community Edition Download tutorial (detailed)
- 七大排序(一)
- 速卖通商品详情API接口(item_get-获得aliexpress商品详情接口)
- Unity judges whether the object is in front of the camera and the UI follows the 3D object
- Using docker to build grafana+prometheus monitoring database resources (II)
- mysql库使用中卡死或者死锁事件
- 关键字搜索苏宁商品API接口(苏宁商品列表API接口)
- 1688 API interface for all goods in the store (API interface for querying all goods in the whole store)
- dlvm-netcore 开源框架
猜你喜欢

树状数组详解

京东店铺所有商品API接口(JD整店商品查询API接口)

Using docker to build grafana+prometheus monitoring database resources (II)

什么是Sectigo?

七大排序(二)

Problems and solutions of wechat applet related knowledge points and cloud music project production

京东店铺的所有商品API接口(item_search_shop-获得店铺的所有商品API接口),整店商品API接口

小工具(读取Excel数据并存入数据库表)

API interface of all products in JD store (JD whole store product query API interface)

Today, I went to oppo for an interview and got numb...
随机推荐
JMeter regular expression gets login token
Package and built-in module and software development directory specification
R language uses circular statements to draw multiple pictures at one time
Enterprise naming conventions
剑指 Offer 06. 从尾到头打印链表
微信公众号-服务器配置(token验证)
nVisual API接口使用说明
七大排序(二)
拼多多店铺所有商品API接口(整店商品列表查询接口)
. Net core uses imagesharp to generate pictures
【有用的SQL】查Greenplum的数据字典
Today, I went to oppo for an interview and got numb...
API interface for all products in pinduoduo store (whole store product list query interface)
Super sub query implements join query gracefully
Tree array explanation
流程测试
A few lines of assembly, a few lines of C to achieve the simplest kernel
1688店铺所有商品API接口(整店所有商品查询API接口)
Vs2019 Community Edition Download tutorial (detailed)
数据湖(十七):Flink与Iceberg整合DataStream API操作