Advertisement
JJCUBER

Untitled

Feb 14th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
  2. |                                                               |
  3. |          Made by Jason Helman © Jason Tech And Games          |
  4. |   I do not condone the use of my work without my permission   |
  5. |                                                               |
  6. \*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  7.  
  8. using UnityEngine;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11.  
  12. public class ObjectPool : MonoBehaviour
  13. {
  14.     public GameObject obj;
  15.     public int maxAmount;
  16.  
  17.     private List<GameObject> objs;
  18.     // private List<GameObject> objsOverflow;
  19.  
  20.     void Start ()
  21.     {
  22.         for(int i = 0; i < maxAmount; i++)
  23.         {
  24.             GameObject objRef = Instantiate(obj, Vector3.zero, Quaternion.identity, transform);
  25.             objRef.SetActive(false);
  26.             objs.Add(objRef);
  27.         }
  28.     }
  29.    
  30.     public GameObject Instantiate(Vector3 pos)
  31.     {
  32.         GameObject go;
  33.         int val = -1;
  34.         for(int i = 0; i < objs.Count; i++)
  35.         {
  36.             if(objs[i].activeSelf == false)
  37.             {
  38.                 val = i;
  39.                 break;
  40.             }
  41.         }
  42.         if (val == -1)
  43.         {
  44.             go = Instantiate(obj, pos, Quaternion.identity, transform);
  45.             // objsOverflow.Add(go);
  46.         }
  47.         else
  48.         {
  49.             objs[val].transform.position = pos;
  50.             go = objs[val];
  51.         }
  52.  
  53.         return go;
  54.     }
  55.  
  56.     public void Destroy(/*ref*/ GameObject obj)
  57.     {
  58.         // objs.Find(obj);
  59.         int val = -1;
  60.         for(int i = 0; i < objs.Count; i++)
  61.         {
  62.             if(objs[i].Equals(obj))
  63.             {
  64.                 val = i;
  65.                 break;
  66.             }
  67.         }
  68.         if(val == -1)
  69.         {
  70.             Destroy(obj);
  71.         }
  72.         else
  73.         {
  74.             objs[val].SetActive(false);
  75.         }
  76.     }
  77.  
  78.     public IEnumerator Destroy( /*Ref<GameObject>*/ GameObject obj, float delay)
  79.     {
  80.         // Invoke("Destroy(obj)", delay);
  81.         // Wait(obj, delay);
  82.  
  83.         yield return new WaitForSeconds(delay);
  84.         Destroy(obj/*.Value*/);
  85.     }
  86.  
  87.     /*private IEnumerator Wait(GameObject obj, float delay)
  88.     {
  89.         yield return new WaitForSeconds(delay);
  90.         Destroy(obj);
  91.     }*/
  92. }
  93. /*
  94. public class Ref<T>
  95. {
  96.     private T backing;
  97.     public T Value { get { return backing; } }
  98.     public Ref(T reference)
  99.     {
  100.         backing = reference;
  101.     }
  102. }
  103. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement