当前位置:网站首页>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
边栏推荐
- freeCodeCamp----prob_ Calculator exercise
- 自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
- VsCode-Go
- . net cross platform principle (Part I)
- [C#] 彻底搞明白深拷贝
- Your brain expands and shrinks over time — these charts show how
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- Production environment——
- 1-1 NodeJS
- 文件操作《二》(5000字总结篇)
猜你喜欢
Detailed explanation of Niuke - Gloves
ASP. Net core dependency injection service life cycle
Devexpress GridView add select all columns
oracle 中快速获取表的列名列表
C# Task. Delay and thread The difference between sleep
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
RPC核心概念理解
How much do you know about the process of the interview
Get the column name list of the table quickly in Oracle
随机推荐
Shell-入门、变量、以及基本的语法
. net type transfer
PHP efficiently reads large files and processes data
[registration] tf54: engineer growth map and excellent R & D organization building
groutine
El cascade and El select click elsewhere to make the drop-down box disappear
Use of shell cut command
杂文 谈谈古典的《拆掉思维里的墙》
AIOT产业技术全景结构-数字化架构设计(8)
Use of shell sed command
How vscode compares the similarities and differences between two files
Kingdee Cloud Star API calling practice
PostgreSQL column storage and row storage
Clickhouse - data type
Promise (II)
Expression "func" tSource, object "to expression" func "tSource, object" []
MySQL modify master database
Summary of common websites
Clickhouse table engine
Smart doc + Torna generate interface document