当前位置:网站首页>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
边栏推荐
- Production environment——
- Detailed explanation of the penetration of network security in the shooting range
- Log4j output log information to file
- Quick install mongodb
- Preliminary understanding of promse
- Installing labellmg tutorial in Windows
- [pimf] openharmony paper Club - what is the experience of wandering in ACM survey
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- [registration] tf54: engineer growth map and excellent R & D organization building
- Paging SQL
猜你喜欢

Nacos + aspnetcore + Ocelot actual combat code

C语言函数详解

Document operation II (5000 word summary)

EF core in ASP Generate core priority database based on net entity model

自定义my_strcpy与库strcpy【模拟实现字符串相关函数】

TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘

Bytevcharts visual chart library, I have everything you want

文件操作《二》(5000字总结篇)

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

JS, entries(), keys(), values(), some(), object Assign() traversal array usage
随机推荐
Use of Shell sort command
Lock锁
Conversion between hexadecimal numbers
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
◰ GL shadow map core steps
Grpc gateway based on Ocelot
1-4 configuration executable script of nodejs installation
matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
文件操作《二》(5000字总结篇)
Baidu Map 3D rotation and tilt angle adjustment
Linux MySQL data timing dump
手写事件发布订阅框架
An essay on the classical "tear down the wall in thinking"
【题解】[SHOI2012] 随机树
1-2 characteristics of nodejs
PostgreSQL列存与行存
ASP. Net core dependency injection service life cycle
Detailed explanation of C webpai route
. net cross platform principle (Part I)
Installing labellmg tutorial in Windows