代码改变世界

unity3d对象池的使用

  糯米粥  阅读(897)  评论(0编辑  收藏  举报

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

http://blog.csdn.net/lzhq1982/article/details/12649281

 

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

这里随便举个列子

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

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 initialization
14     void Start()
15     {
16 
17     }
18 
19     // Update is called once per frame
20     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 /// <summary>
 6 /// 对象池脚本
 7 /// 
 8 /// 管理子弹。当子弹用完后。回收
 9 /// </summary>
10 public class ObjectPools : MonoBehaviour
11 {
12 
13     GameObject result;
14 
15     List<GameObject> poolsred;
16 
17 
18     Dictionary<string, List<GameObject>> poolList;
19 
20 
21     private static ObjectPools instance;
22 
23     public static ObjectPools Instance
24     {
25         get { return instance; }
26         //set { ObjectPools.instance = value; }
27     }
28 
29     void Awake()
30     {
31         poolsred = new List<GameObject>(); //实例化对象池
32 
33         instance = this; //单列
34 
35         poolList = new Dictionary<string, List<GameObject>>();
36     }
37     string tag;
38     public GameObject MyInstantiate(GameObject prefab, Vector3 position, Quaternion rotation)
39     {
40         tag = prefab.tag;
41         //如果有某个小集合的key,并且小集合内元素数量大于0
42         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         else
51         {
52 
53             if (!poolList.ContainsKey(tag))
54             {
55                 poolList.Add(tag, new List<GameObject>());
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     /// <summary>
67     /// 协程 同步方法 在同一个线程里面
68     /// </summary>
69     /// <param name="bullet"></param>
70     /// <returns></returns>
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 initialization
87     void Start()
88     {
89 
90     }
91 
92     // Update is called once per frame
93     void Update()
94     {
95 
96     }
97 }
复制代码

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
'
点击右上角即可分享
微信分享提示