当前位置:网站首页>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
边栏推荐
- Milvus 2.0 détails du système d'assurance de la qualité
- Installing labellmg tutorial in Windows
- 网络安全之渗透靶场实战详解
- Copy constructor shallow copy and deep copy
- 【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
- 线性代数感悟之2
- 【题解】[SHOI2012] 随机树
- Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
- 1-5 nodejs commonjs specification
- Clickhouse SQL operation
猜你喜欢
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
groutine
Milvus 2.0 质量保障系统详解
Detailed explanation of Milvus 2.0 quality assurance system
Lock锁
1-1 NodeJS
The new MySQL table has a self increasing ID of 20 bits. The reason is
Milvus 2.0 détails du système d'assurance de la qualité
C语言函数详解
Solution architect's small bag - 5 types of architecture diagrams
随机推荐
Expression "func" tSource, object "to expression" func "tSource, object" []
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
Detailed explanation of the penetration of network security in the shooting range
JSON deserialize anonymous array / object
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Detailed explanation of Niuke - Gloves
Lock lock
Smart doc + Torna generate interface document
ClickHouse-SQL 操作
Idea of batch manufacturing test data, with source code
SQL database
Promise (II)
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
PostgreSQL column storage and row storage
Copy constructor shallow copy and deep copy
Milvus 2.0 質量保障系統詳解
Scope and scope chain in JS
Lock锁
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Redis docker installation