unity3d对象池的使用

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

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 }
原文地址:https://www.cnblogs.com/nsky/p/4625162.html