博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity3d对象池的使用
阅读量:4557 次
发布时间:2019-06-08

本文共 3495 字,大约阅读时间需要 11 分钟。

说对象池之前首先来看看单例类和单例脚本的区别。这里有介绍

 

使用对象池的好处是不用每次都创建对象。销毁在创建,比如子弹的发射,当创建好的子弹。可以在使用后保存到对象池里面。当用的时候。直接从对象池中取即可

这里随便举个列子

鼠标左键单击发射蓝色球。右键发射红色球

Main Camera挂载Fire.cs脚本

GameObject挂载单列脚本ObjectPools.cs

Fire.cs代码

1 using UnityEngine; 2 using System.Collections; 3  4 public class Fire : MonoBehaviour 5 { 6  7     public GameObject builPrefabRed; 8  9     public GameObject builPrefabGreen;10 11     GameObject o;12 13     // Use this for initialization14     void Start()15     {16 17     }18 19     // Update is called once per frame20     void Update()21     {22 23         if (Input.GetMouseButtonDown(1))24         {25 26             o = ObjectPools.Instance.MyInstantiate(builPrefabRed, transform.position, Quaternion.identity) as GameObject;27 28             //o = Instantiate(builPrefab, transform.position, Quaternion.identity) as GameObject;29 30             o.rigidbody.AddForce(Vector3.forward * 200);31 32             //Destroy(o, 2);33         }34         else if (Input.GetMouseButtonDown(0))35         {36             o = ObjectPools.Instance.MyInstantiate(builPrefabGreen, transform.position, Quaternion.identity) as GameObject;37 38             o.rigidbody.AddForce(Vector3.forward * 200);39         }40     }41 }

ObjectPools.cs类

1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4  5 ///  6 /// 对象池脚本 7 ///  8 /// 管理子弹。当子弹用完后。回收 9 /// 10 public class ObjectPools : MonoBehaviour11 {12 13     GameObject result;14 15     List
poolsred;16 17 18 Dictionary
> poolList;19 20 21 private static ObjectPools instance;22 23 public static ObjectPools Instance24 {25 get { return instance; }26 //set { ObjectPools.instance = value; }27 }28 29 void Awake()30 {31 poolsred = new List
(); //实例化对象池32 33 instance = this; //单列34 35 poolList = new Dictionary
>();36 }37 string tag;38 public GameObject MyInstantiate(GameObject prefab, Vector3 position, Quaternion rotation)39 {40 tag = prefab.tag;41 //如果有某个小集合的key,并且小集合内元素数量大于042 if (poolList.ContainsKey(tag) && poolList[tag].Count > 0) //弹夹里面有子弹43 {44 result = poolList[tag][0];//取弹夹里面的第一颗子弹45 poolList[tag].Remove(result); //从弹夹移除子弹46 47 result.transform.position = position;48 result.transform.rotation = rotation;49 }50 else51 {52 53 if (!poolList.ContainsKey(tag))54 {55 poolList.Add(tag, new List
());56 }57 result = Instantiate(prefab, position, rotation) as GameObject;58 }59 //开启协程方法60 StartCoroutine(ReturnToPools(result));61 result.SetActive(true);62 return result;63 }64 65 66 ///
67 /// 协程 同步方法 在同一个线程里面68 /// 69 ///
70 ///
71 public IEnumerator ReturnToPools(GameObject bullet)72 {73 yield return new WaitForSeconds(2f); //等待2秒74 75 poolList[bullet.tag].Add(bullet);76 77 //poolsred.Add(bullet);78 79 80 //取消力81 bullet.rigidbody.velocity = Vector3.zero;82 83 bullet.SetActive(false);84 }85 86 // Use this for initialization87 void Start()88 {89 90 }91 92 // Update is called once per frame93 void Update()94 {95 96 }97 }

 

转载于:https://www.cnblogs.com/nsky/p/4625162.html

你可能感兴趣的文章
归档及压缩命令
查看>>
Mybatis步骤
查看>>
WPF自定义控件之扩展原生控件
查看>>
《区块链100问》笔记整理——42~49问
查看>>
使用Jquery+EasyUI 进行框架项目开发案例讲解之二---用户管理源码分享
查看>>
深入理解计算机系统(1.4)---并发与并行、浅谈抽象
查看>>
函数依赖的公理化系统
查看>>
rabbitmq学习(四):利用rabbitmq实现远程rpc调用
查看>>
侯捷C++学习(二)
查看>>
EasyPlayer RTSP Android安卓播放器修复播放画面卡在第一帧bug
查看>>
web项目中全局常量的添加
查看>>
搬运工程 启动!
查看>>
局部加权回归(LWR) Matlab模板
查看>>
Connect to the DSP on C6A8168/DM8168/DM8148 using CCS
查看>>
hibernate在使用getCurrentSession时提示no session found for current thread
查看>>
【Luogu1471】方差(线段树)
查看>>
【agc028E】High Elements(动态规划,线段树,贪心)
查看>>
DEV中svg图标的使用
查看>>
Codefroces Gym101572 I.Import Spaghetti-有向图跑最小环输出路径(Floyd)
查看>>
有关位运算的操作+二进制状态压缩
查看>>