Object Pooling

来源:互联网 发布:三维动画制作软件 编辑:程序博客网 时间:2024/06/04 18:43

这样做对,这样做不对!

原文里的static加上就成singleton了感觉意义不大,除非只有子弹用这个pool。

 using UnityEngine; using System.Collections; using System.Collections.Generic; public class ObjectPoolScript : MonoBehaviour {     public GameObject pooledObject;     public int pooledAmount = 20;     public bool willGrow = true;     public List<GameObject> pooledObjects;     void Start ()     {         pooledObjects = new List<GameObject>();         for(int i = 0; i < pooledAmount; i++)         {             GameObject obj = (GameObject)Instantiate(pooledObject);             obj.SetActive(false);             pooledObjects.Add(obj);         }     }     public GameObject GetPooledObject()     {         for(int i = 0; i< pooledObjects.Count; i++)         {             if(pooledObjects[i] == null)             {                 GameObject obj = (GameObject)Instantiate(pooledObject);                 obj.SetActive(false);                 pooledObjects[i] = obj;                 return pooledObjects[i];             }             if(!pooledObjects[i].activeInHierarchy)             {                 return pooledObjects[i];             }             }         if (willGrow)         {             GameObject obj = (GameObject)Instantiate(pooledObject);             pooledObjects.Add(obj);             return obj;         }         return null;     } }
0 0