当前位置:网站首页>Use of bullets in object pool mode in aircraft war
Use of bullets in object pool mode in aircraft war
2022-07-22 18:08:00 【Zero if】
stay B Stand up and learn a Yan tutorial , Record the learning of object pool pattern
From the figure , There are four steps :
1 stay Pool Generate preparatory objects in the script . Specific include Initialize() Initialize queue ,Copy() Copy the prepared object and initialize the disabled state , Put in queue .
2 Players fire , from PoolManager Script Release() Find the corresponding Pool.prefab( Because there will not be only one object pool in the game ), Through the dictionary in the object pool manager script < prefab, Pool >, It corresponds to which pool the currently used object is in . Take it out of it .
3 Get the corresponding pool , In the pool Pool Script PreparedObject() Return to an available object in the team and set the status to enabled .
4 Pool Script , Available objects AvailableObject() Take it out from the head of the queue , Put it back at the end of the team immediately , For next use .
1:Pool.cs
public void Initialize(Transform parent)
{
queue = new Queue<GameObject>();
this.parent = parent;
for (var i = 0; i < size; i++)
{
queue.Enqueue(Copy());
}
}
GameObject Copy()
{
var copy = GameObject.Instantiate(prefab, parent);
copy.SetActive(false);
return copy;
}
2:
Player.cs
PoolManager.Release(projectile1, muzzleMiddle.position);
PoolManager.cs
static Dictionary<GameObject, Pool> dictionary;
public static GameObject Release(GameObject prefab)
{
if(!dictionary.ContainsKey(prefab))
{
return null;
}
return dictionary[prefab].PreparedObject();
}
3:
Pool.cs
public GameObject PreparedObject()
{
GameObject preparedObject = AvailableObject();
preparedObject.SetActive(true);
return preparedObject;
}
4:Pool.cs
GameObject AvailableObject()
{
GameObject availableObject = null;
availableObject = queue.Dequeue();
queue.Enqueue(availableObject);
return availableObject;
}
How to judge the size of the object pool ?
stay PoolManager Script , Write a CheckPoolSize(), stay Ondestroy Called when the . The main content is to judge whether the size of the current queue is consistent with our preset size , Do not print out , Adjust preset size .
private void OnDestroy()
{
CheckPoolSize(playProjectilePools);
}
void CheckPoolSize(Pool[] pools)
{
foreach(var pool in pools)
{
if(pool.RuntimeSize > pool.Size)
{
Debug.Log(
string.Format("Pool : {0} Actual size {1} Larger than the reserved size {2}",
pool.Prefab.name,
pool.RuntimeSize,
pool.Size));
}
}
}
边栏推荐
- MySQL和MariaDB区别
- MySQL constraints
- [micro Service ~ remote call] integrate resttemplate, webclient, feign
- Dokcer运行Nacos容器自动退出问题
- Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
- GBase8s数据库约束模式的保持
- Arrays类的理解学习
- Unity textmeshpro namespace reference and component acquisition
- Methodology (I): how to quickly learn a programming language
- MySQL数据库结合项目实战SQL优化总结
猜你喜欢
学IT,你后悔了么?
【OpenCV入门实战】利用电脑前置摄像头进行人脸检测
数据高效治理如何开展,指标管理与数据溯源来帮忙!
地图找房的实例
Deep learning (II) takes you to understand neural networks and activation functions
如何破解IT中年危机下的惨淡人生
Conference OA project
测试员职场最大的危机不是30岁,而是中年被裁
JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
离线安装vscode
随机推荐
力扣练习——26 分割数组为连续子序列
Pytoch implements word2vec
数据高效治理如何开展,指标管理与数据溯源来帮忙!
飞机大战中对象池模式子弹使用
(11) 51 Single Chip Microcomputer -- realize the storage of stopwatch data with AT24C02 (attached with achievement display)
web3分享
Deep learning (II) takes you to understand neural networks and activation functions
How does boss direct hire write an excellent resume?
计算存款利息
How to carry out efficient data governance? Index management and data traceability help!
以SPI的仿真文件学习TB写法
1.4.1 好多好多道符(cin/cout解绑,O3优化)
力扣练习——24 去除重复字母
Oracle statement adjustment
【案例分享】配置IS-IS的路由渗透功能
Methods of downloading literature from IEEE
测试员职场最大的危机不是30岁,而是中年被裁
GBase8s数据库使当前的连接成为休眠的连接
Bigder:37/100 a misoperation
[case sharing] configure the routing penetration function of IS-IS