Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\
- | |
- | Made by Jason Helman © Jason Tech And Games |
- | I do not condone the use of my work without my permission |
- | |
- \*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class ObjectPool : MonoBehaviour
- {
- public GameObject obj;
- public int maxAmount;
- private List<GameObject> objs;
- // private List<GameObject> objsOverflow;
- void Start ()
- {
- for(int i = 0; i < maxAmount; i++)
- {
- GameObject objRef = Instantiate(obj, Vector3.zero, Quaternion.identity, transform);
- objRef.SetActive(false);
- objs.Add(objRef);
- }
- }
- public GameObject Instantiate(Vector3 pos)
- {
- GameObject go;
- int val = -1;
- for(int i = 0; i < objs.Count; i++)
- {
- if(objs[i].activeSelf == false)
- {
- val = i;
- break;
- }
- }
- if (val == -1)
- {
- go = Instantiate(obj, pos, Quaternion.identity, transform);
- // objsOverflow.Add(go);
- }
- else
- {
- objs[val].transform.position = pos;
- go = objs[val];
- }
- return go;
- }
- public void Destroy(/*ref*/ GameObject obj)
- {
- // objs.Find(obj);
- int val = -1;
- for(int i = 0; i < objs.Count; i++)
- {
- if(objs[i].Equals(obj))
- {
- val = i;
- break;
- }
- }
- if(val == -1)
- {
- Destroy(obj);
- }
- else
- {
- objs[val].SetActive(false);
- }
- }
- public IEnumerator Destroy( /*Ref<GameObject>*/ GameObject obj, float delay)
- {
- // Invoke("Destroy(obj)", delay);
- // Wait(obj, delay);
- yield return new WaitForSeconds(delay);
- Destroy(obj/*.Value*/);
- }
- /*private IEnumerator Wait(GameObject obj, float delay)
- {
- yield return new WaitForSeconds(delay);
- Destroy(obj);
- }*/
- }
- /*
- public class Ref<T>
- {
- private T backing;
- public T Value { get { return backing; } }
- public Ref(T reference)
- {
- backing = reference;
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement