当前位置:网站首页>Unity object pool implementation
Unity object pool implementation
2022-08-10 06:14:00 【Nobel Prize Winner】
Unity object pool implementation
using System.Collections;using System.Collections.Generic;using UnityEngine;/*** Object pool*/public class ObjectPool{//Singleton modeprivate static ObjectPool instance = null;private ObjectPool() { }public static ObjectPool Instance{get{if (instance == null){instance = new ObjectPool();}return instance;}}//data partDictionary> pool = new Dictionary>();//API part/// /// Put a game object into the object pool/// /// The object to be placed in the object poolpublic void Put(GameObject obj){// not the first caseif (pool.ContainsKey(obj.name)){pool[obj.name].Add(obj);}else{// The first time to store this type of game object//declare a new list containing this class of game objectsList list = new List();/ / Create the corresponding relationship between the key name and the key valuepool.Add(obj.name, list);// put the game object in the listlist.Add(obj);}//Hide the object put into the object poolobj.SetActive(false);}/// /// Get a game object with a name from the object pool/// /// Game object name to get/// public GameObject Get(string key){//Check if there is an object with that nameif (pool.ContainsKey(key)){// Are there any remaining elements in the listif (pool[key].Count <= 0){return null;}else {//Get the specific game object and returnGameObject obj = pool[key][0];// remove the game object from the object poolpool[key].Remove(obj);obj.SetActive(true);return obj;}}else{return null;}}/// /// Clear the object pool, called when jumping the scene/// public void Clear(){//The statically modified object will not be released when jumping the scene in Unitypool.Clear();}}
边栏推荐
猜你喜欢
随机推荐
Unity对象池实现
【fiddler4】使用fiddler设置简单并发
lua的模块与类
酸回收工艺讲解
工业废酸回收工艺
优化Mysql运行OrderBy性能
剑指 Offer(第 2 版)7/5 5-8
C#热更新比Lua好在哪里?
手机端应用类型
常用模块封装-pymysql、pymongo(可优化)
Unity中采用二进制存档与读档
mysql使用常见问题和解决
hanLP探索-语义距离计算的实现
STM32单片机OLED经典2048游戏单片机小游戏
Easy to master Unity of eight prior to rendering
21天学习挑战赛--字符串切割
初学者也能看懂的Ray March体积云
剑指 Offer(第 2 版)7/6 9-13
Gradle学习 (一) 入门
51单片机BH1750智能补光灯台灯光强光照恒流源LED控制系统