当前位置:网站首页>Understanding and small examples of unity3d object pool
Understanding and small examples of unity3d object pool
2022-04-23 17:10:00 【Tomato Warrior】
Studying recently Unity3D, Making a Parkour Demo When , Find and constantly initialize obstacles and delete obstacles comparison card , Later, I studied the object pool , It's been a long time , I finally got it . Now write it down , I hope I can help novices like me , If there's something wrong , Please correct me !
Most of this article draws on this article
unity3D First knowledge of object pool technology
The concept of object pool : When you activate an object , It's extracted from the pool . When deactivating an object , It goes back into the pool , Waiting for the next request .( From Baidu );
background : Parkour , There are obstacles on the road , The obstacle behind the character disappears , Randomly generate obstacles in front of the character
The three most basic things you need :
1、 A pool : Used to hold what you need , And recycled items ;
2、 A way to get things ;
3、 A way to put things ;
With this 3 things , You can build an object pool
The following is the complete code section , There are detailed notes
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameObjectPool : MonoBehaviour {
// Use this for initialization
// The singleton pattern , If you don't know, you can baidu , There is no explanation here , Because the level is limited
public static GameObjectPool instance;
// Use a dictionary to construct your pool , In the dictionary String It's the name of the pit , Each pit corresponds to one GameObject list
Dictionary<string, List<GameObject>> pool = new Dictionary<string, List<GameObject>>() { };
void Start () {
instance = this;// The singleton pattern
}
// The way to get objects from the pool , Pass two parameters , What you need to get , And where you need to put it
// The object you need should be preset
public GameObject GetPool(GameObject go,Vector3 position)
{
string key = go.name+"(Clone)";// The name of the pit to get something
GameObject rongqi; // The container you use to take objects ;
// The following is divided into three cases to analyze
if (pool.ContainsKey(key) && pool[key].Count > 0)// If the pit exists , There's something in the pit
{
// Just take the first one in the pit
rongqi = pool[key][0];
pool[key].RemoveAt(0);// Release the first position ;
}
else if (pool.ContainsKey(key) && pool[key].Count <= 0)// Pit existence , There's nothing in the pit
{
// Then initialize one directly
rongqi = Instantiate(go,position,Quaternion.identity) as GameObject;
}
else // No pit
{
// Not only to initialize , And add... To the pit
rongqi = Instantiate(go, position, Quaternion.identity) as GameObject;
pool.Add(key, new List<GameObject>() { });
}
// Adjust the initial state of the object
rongqi.SetActive(true);
// Here I add a code that also displays sub objects , You don't have to add
foreach (Transform child in rongqi.transform)
{
child.gameObject.SetActive(true);
}
// Position initialization
rongqi.transform.position = position;
return rongqi;
}
// The way to put it in the pool
public void IntoPool(GameObject go)
{
// In theory, all our things are taken out of the pit , So there must be a pit when you put the object in , It can be put directly into , Don't divide the situation
string key = go.name;
pool[key].Add(go);
go.SetActive(false);
}
}
</pre><pre name="code" class="csharp">
版权声明
本文为[Tomato Warrior]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553458277.html
边栏推荐
- [registration] tf54: engineer growth map and excellent R & D organization building
- VsCode-Go
- CentOS MySQL multi instance deployment
- Website_ Collection
- matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
- PHP高效读大文件处理数据
- Clickhouse table engine
- Node access to Alipay open platform sandbox to achieve payment function
- . net cross platform principle (Part I)
- ASP. Net core configuration options (Part 1)
猜你喜欢

Detailed explanation of C webpai route

Lock lock

Shell script -- shell programming specification and variables
![[PROJECT] small hat takeout (8)](/img/54/0187eeb637f4dcd4ad3969b00e2b77.png)
[PROJECT] small hat takeout (8)

. net cross platform principle (Part I)

Rtklib 2.4.3 source code Notes

Shell脚本——Shell编程规范及变量

JS, entries(), keys(), values(), some(), object Assign() traversal array usage

【WPF绑定3】 ListView基础绑定和数据模板绑定

VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
随机推荐
Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
PostgreSQL column storage and row storage
STM32__ 03 - beginner timer
Quick install mongodb
El date picker limits the selection range from the current time to two months ago
线性代数感悟之1
Copy constructor shallow copy and deep copy
Talk about browser cache control
AIOT产业技术全景结构-数字化架构设计(8)
. net cross platform principle (Part I)
PHP efficiently reads large files and processes data
Lock lock
Net standard
Get the column name list of the table quickly in Oracle
Milvus 2.0 質量保障系統詳解
手写事件发布订阅框架
Baidu Map Case - modify map style
Decimal format decimal / datetime conversion processing
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Rtklib 2.4.3 source code Notes