Advertisement
Guest User

AsherVo object pool

a guest
Oct 18th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4.  
  5. public class PrefabPool : MonoBehaviour
  6. {
  7.     public MonoBehaviour prefab;
  8.     public int objectCount = 64;
  9.     public bool preallocate = true;
  10.     public bool sceneObject = false;
  11.  
  12.     Stack< GameObject > inactiveObjects = new Stack< GameObject >();
  13.     List< GameObject > activeObjects = new List< GameObject >();
  14.     bool allocated = false;
  15.  
  16.     void Awake ()
  17.     {
  18.         if ( !allocated )
  19.             Allocate();
  20.     }
  21.  
  22.     public void Allocate ()
  23.     {
  24.         if ( sceneObject )
  25.         {
  26.             prefab.gameObject.SetActive( false );
  27.             prefab.transform.SetParent( transform );
  28.         }
  29.  
  30.         if ( !preallocate )
  31.         {
  32.             allocated = true;
  33.             return;
  34.         }
  35.  
  36.         for ( int i = 0; i <= objectCount; i++ )
  37.         {
  38.             if ( i == 0 && sceneObject )
  39.                 continue;
  40.  
  41.             GameObject obj = Instantiate( prefab.gameObject ) as GameObject;
  42.             obj.SetActive( false );
  43.             obj.transform.SetParent( transform );
  44.             inactiveObjects.Push( obj );
  45.         }
  46.  
  47.         allocated = true;
  48.     }
  49.  
  50.     public T Spawn< T > () where T : MonoBehaviour
  51.     {
  52.         if ( !preallocate )
  53.         {
  54.             GameObject obj;
  55.  
  56.             if ( inactiveObjects.Count > 0 )
  57.             {
  58.                 obj = inactiveObjects.Pop();
  59.                 obj.SetActive( true );
  60.                 activeObjects.Add( obj );
  61.                 return obj.GetComponent< T >() as T;
  62.             }
  63.  
  64.             obj = Instantiate( prefab.gameObject, transform ) as GameObject;
  65.             obj.SetActive( true );
  66.             activeObjects.Add( obj );
  67.             T result = obj.GetComponent< T >() as T;
  68.  
  69.             Assert.IsTrue( result != null, "Did not find " + typeof( T ) + " component on spawned object!" );
  70.  
  71.             return result;
  72.         }
  73.  
  74.  
  75.         if ( !allocated )
  76.             Allocate();
  77.  
  78.         try
  79.         {
  80.             GameObject obj = inactiveObjects.Pop();
  81.             obj.SetActive( true );
  82.             activeObjects.Add( obj );
  83.             return obj.GetComponent< T >() as T;
  84.         }
  85.         catch
  86.         {
  87.             Debug.LogError( name + ": RAN OUT OF OBJECTS", this );
  88.             return null;
  89.         }
  90.     }
  91.  
  92.     public T Despawn< T > ( T obj ) where T : MonoBehaviour
  93.     {
  94.         Despawn( obj.gameObject );
  95.         return obj;
  96.     }
  97.  
  98.     public GameObject Despawn ( GameObject obj )
  99.     {
  100.         obj.SetActive( false );
  101.         obj.transform.SetParent( transform );
  102.         inactiveObjects.Push( obj );
  103.         activeObjects.Remove( obj );
  104.         return obj;
  105.     }
  106.  
  107.     public void DespawnAll ()
  108.     {
  109.         int numActiveObjects = activeObjects.Count - 1;
  110.         for ( int i = numActiveObjects; i >= 0; i-- )
  111.         {
  112.             activeObjects[ i ].SetActive( false );
  113.             activeObjects[ i ].transform.SetParent( transform );
  114.             inactiveObjects.Push( activeObjects[ i ] );
  115.         }
  116.         activeObjects.Clear();
  117.     }
  118.  
  119.     public List< T > GetAll< T > () where T : MonoBehaviour
  120.     {
  121.         List< T > result = new List< T >();
  122.         foreach ( GameObject obj in activeObjects )
  123.             result.Add( obj.GetComponent< T >() );
  124.  
  125.         return result;
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement