当前位置:网站首页>D41_buffer pool
D41_buffer pool
2022-08-05 06:44:00 【Not so simple GG】
Why use the buffer pool and how to use the buffer pool?In the previous study, special effects, bullets, etc. were always created and destroyed frequently, which undoubtedly increased the burden on the CPU. In order to save the amount of calculation, we can use the buffer pool.
The buffer pool can be imagined as a storage box, and the game object can be imagined as disposable chopsticks. When we eat, we need to use disposable chopsticks, and throw them away after eating, which is undoubtedly consumedResources, so we use household chopsticks, wash them after use, and put them in the storage box to wait for the next meal to be used again. When the chopsticks bought this time are not enough, we will buy a new batch.Chopsticks, this undoubtedly saves some resources, but also wastes a little storage space, because chopsticks are always placed in the storage box when they are not in use, so the buffer pool trades space for time.
The principle of the code is to create a collection (storage box). Before instantiating a game object, check whether there is a game object (chopstick) in the collection. If not, instantiate (buy) one.If so, take out the game object and use it - activate it; when you need to delete a game object (chopsticks), just deactivate it directly.
Let's show the code:
public class ObjectPool : MonoBehaviour{public static ObjectPool Instance;//First come a singletonprivate void Awake(){Instance = this;}List pool = new List();public GameObject InstantiatePool(string gameObjectName){GameObject go = null;if (pool.Count <= 0)//If there is no such game object in the collection, create one{go = GameObject.Instantiate(Resources.Load("Prefabs/" + gameObjectName) as GameObject);go.name = gameObjectName;//Because the cloned objects all have (clone), so you need to unify the name}else{go = pool[0];//If there is this game object in the collection, then you don't need to create it again, just use thispool.RemoveAt(0);//Remove this game object from the collection}go.SetActive(true);go.transform.SetParent(null);return go;}public GameObject InstantiatePool(string gameObjectName, Vector3 position, Quaternion quaternion){//Reload it to facilitate the call of other classesGameObject go = InstantiatePool(gameObjectName);go.transform.position = position;go.transform.rotation = quaternion;return go;}public void RemovePool(GameObject gameObject){pool.Add(gameObject);//Add the game object to the collectiongameObject.SetActive(false);gameObject.transform.SetParent(transform);}} The dynamic loading of resources is required in the above code, namely Resources.Load("Perfabs/"+gameObjectName), when you need this game object, look for it from the Resource folder.
Then the question is, there are more than one kind of objects in our game that need to be created and destroyed frequently, so do I have to write one for each one? So, here comes the advanced buffer pool, which is similar to storageThe box, let's make it more detailed, one area for chopsticks, one area for dishes, and one area for forks... In this case, one box can solve all the problems, then the problem is, this big box shouldWhat data class to use? It must be able to save the collection and be easy to find.So, we naturally think of the dictionary.
Continue to show the code below:
(Roughly the same, just an extra layer of dictionary is added, so I won't comment in detail)
public class GameObjectPool : MonoBehaviour{public static GameObjectPool Instance;private void Awake(){Instance = this;}Dictionary> pool = new Dictionary>();public GameObject InstantiatePool(string gameObjectName){GameObject go = null;if (pool.ContainsKey(gameObjectName)){if (pool[gameObjectName].Count > 0){go = pool[gameObjectName][0];pool[gameObjectName].RemoveAt(0);}}if (go == null){go = GameObject.Instantiate(Resources.Load("Pool/" + gameObjectName) as GameObject);go.name = gameObjectName;}go.SetActive(true);go.transform.SetParent(null);return go;}public GameObject InstantiatePool(string gameObjectName, Vector3 position, Quaternion quaternion){GameObject go = InstantiatePool(gameObjectName);go.transform.position = position;go.transform.rotation = quaternion;return go;}public void DestoryPool(GameObject go){if (pool.ContainsKey(go.name)){pool[go.name].Add(go);}else{pool.Add(go.name, new List());pool[go.name].Add(go);}go.SetActive(false);go.transform.SetParent(transform);}} 边栏推荐
- Does flink cdc currently support Gauss database sources?
- 路由器和静态路由的配置
- ALC experiment
- Seven Ways to Center a Box Horizontally and Vertically
- flink cdc 目前支持Gauss数据库源吗
- Detailed explanation of ten solutions across domains (summary)
- Complete mysql offline installation in 5 minutes
- 邮件管理 过滤邮件
- vscode笔记
- Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
猜你喜欢

Nacos集群的搭建过程详解

Q 2020, the latest senior interview Laya soul, do you know?

从“双卡双待“到”双通“,vivo率先推动DSDA架构落地

selenium学习

One-arm routing experiment and three-layer switch experiment

Passing parameters in multiple threads

Collision, character controller, Cloth components (cloth), joints in the Unity physics engine

Alibaba Cloud Video on Demand

路由器和静态路由的配置

Complete mysql offline installation in 5 minutes
随机推荐
[问题已处理]-jenkins流水线checkout超时
程序员应该这样理解I/O
单臂路由实验和三层交换机实验
前置++和后置++的区别
One-arm routing experiment and three-layer switch experiment
network issue?Service packet loss?This is enough
Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station
H5开发调试-Fiddler手机抓包
Does flink cdc currently support Gauss database sources?
Passing parameters in multiple threads
网络层协议介绍
The future of cloud gaming
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
大小屏适配
摆脱极域软件的限制
Will intelligent operation and maintenance replace manual operation and maintenance?
获取预训练模型的网络输入尺寸
Seven Ways to Center a Box Horizontally and Vertically
通过反射获取Class对象的四种方式
数组&的运算