GameObjectPool

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System;  
  3. using System.Collections;  
  4.   
  5. /** 
  6.  * A general pool object for reusable game objects. 
  7.  *  
  8.  * It supports spawning and unspawning game objects that are 
  9.  * instantiated from a common prefab. Can be used preallocate 
  10.  * objects to avoid calls to Instantiate during gameplay. Can 
  11.  * also create objects on demand (which it does if no objects 
  12.  * are available in the pool). 
  13.  *  
  14.  * Converted JScript version to C#, original here: 
  15.  * http://vonlehecreative.com/video-games/unity-resource-gameobjectpool/ 
  16.  *  
  17.  * C# version by Bart Wttewaall, www.Mediamonkey.nl 
  18.  */  
  19.   
  20. public class GameObjectPool  
  21. {  
  22.   
  23.     private GameObject _prefab;  
  24.     private Stack available;  
  25.     private ArrayList all;  
  26.   
  27.     private Action<GameObject> initAction;  
  28.     private bool setActiveRecursively;  
  29.   
  30.     // ---- getters & setters ----  
  31.     #region getters & setters  
  32.   
  33.     // returns the prefab being used by the pool.  
  34.     public GameObject prefab  
  35.     {  
  36.         get { return _prefab; }  
  37.     }  
  38.   
  39.     // returns the number of active objects.  
  40.     public int numActive  
  41.     {  
  42.         get { return all.Count - available.Count; }  
  43.     }  
  44.   
  45.     // returns the number of available objects.  
  46.     public int numAvailable  
  47.     {  
  48.         get { return available.Count; }  
  49.     }  
  50.  
  51.     #endregion  
  52.     // ---- constructor ----  
  53.     #region constructor  
  54.   
  55.     public GameObjectPool(GameObject prefab, uint initialCapacity, Action<GameObject> initAction, bool setActiveRecursively)  
  56.     {  
  57.         this._prefab = prefab;  
  58.         this.initAction = initAction;  
  59.         this.setActiveRecursively = setActiveRecursively;  
  60.   
  61.         available = (initialCapacity > 0) ? new Stack((int)initialCapacity) : new Stack();  
  62.         all = (initialCapacity > 0) ? new ArrayList((int)initialCapacity) : new ArrayList();  
  63.     }  
  64.  
  65.     #endregion  
  66.     // ---- public methods ----  
  67.     #region public methods  
  68.   
  69.     public GameObject Spawn(Vector3 position, Quaternion rotation)  
  70.     {  
  71.         GameObject result;  
  72.   
  73.         if (available.Count == 0)  
  74.         {  
  75.   
  76.             // create an object and initialize it.  
  77.             result = GameObject.Instantiate(prefab, position, rotation) as GameObject;  
  78.   
  79.             // run optional initialization method on the object  
  80.             if (initAction != null) initAction(result);  
  81.   
  82.             all.Add(result);  
  83.   
  84.         }  
  85.         else  
  86.         {  
  87.             result = available.Pop() as GameObject;  
  88.   
  89.             // get the result's transform and reuse for efficiency.  
  90.             // calling gameObject.transform is expensive.  
  91.             Transform resultTrans = result.transform;  
  92.             resultTrans.position = position;  
  93.             resultTrans.rotation = rotation;  
  94.   
  95.             result.SetActive(true);  
  96.         }  
  97.   
  98.         return result;  
  99.     }  
  100.   
  101.     public bool Destroy(GameObject target)  
  102.     {  
  103.         if (!available.Contains(target))  
  104.         {  
  105.             available.Push(target);  
  106.   
  107.             SetActive(target, false);  
  108.             return true;  
  109.         }  
  110.   
  111.         return false;  
  112.     }  
  113.   
  114.     // Unspawns all the game objects created by the pool.  
  115.     public void DestroyAll()  
  116.     {  
  117.         for (int i = 0; i < all.Count; i++)  
  118.         {  
  119.             GameObject target = all[i] as GameObject;  
  120.   
  121.             if (target.activeSelf) Destroy(target);  
  122.         }  
  123.     }  
  124.   
  125.     // Unspawns all the game objects and clears the pool.  
  126.     public void Clear()  
  127.     {  
  128.         DestroyAll();  
  129.         available.Clear();  
  130.         all.Clear();  
  131.     }  
  132.   
  133.     // Applies the provided function to some or all of the pool's game objects.  
  134.     public void ForEach(Action<GameObject> action, bool activeOnly)  
  135.     {  
  136.         for (int i = 0; i < all.Count; i++)  
  137.         {  
  138.             GameObject target = all[i] as GameObject;  
  139.   
  140.             if (!activeOnly || target.activeSelf) action(target);  
  141.         }  
  142.     }  
  143.  
  144.     #endregion  
  145.     // ---- protected methods ----  
  146.     #region protected methods  
  147.   
  148.     // Activates or deactivates the provided game object using the method  
  149.     // specified by the setActiveRecursively flag.  
  150.     protected void SetActive(GameObject target, bool value)  
  151.     {  
  152.         if (setActiveRecursively)   
  153.         {  
  154.             target.SetActive(value);   
  155.         }  
  156.         else  
  157.         {  
  158.             target.SetActive(value);  
  159.         }  
  160.     }  
  161.  
  162.     #endregion  
  163. }  
原地址点击这里

猜你喜欢

转载自hypercube.iteye.com/blog/2319146