Advertisement
Guest User

Untitled

a guest
May 25th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class Pool : MonoBehaviour
  5. {
  6. public GameObject[] objects;
  7. public int[] number;
  8.  
  9. public List<GameObject>[] pool;
  10.  
  11. void Start ()
  12. {
  13. instantiate();
  14. }
  15.  
  16. // instantiate the pooling objects
  17. void instantiate ()
  18. {
  19. GameObject temp;
  20. pool = new List <GameObject>[objects.Length];
  21.  
  22. for (int count = 0; count < objects.Length; count++)
  23. {
  24. pool[count] = new List<GameObject>();
  25. for (int num = 0; num < number[count]; num ++)
  26. {
  27. temp = (GameObject)Instantiate(objects[count]);
  28. temp.transform.parent = this.transform;
  29. pool[count].Add(temp);
  30. }
  31. }
  32. }
  33.  
  34. public GameObject activate(int id)
  35. {
  36. for (int count = 0; count < pool[id].Count; count++)
  37. {
  38. if(!pool[id][count].activeSelf)
  39. {
  40. pool[id][count].SetActive(true);
  41. return pool[id][count];
  42. }
  43. }
  44. pool[id].Add((GameObject)Instantiate(objects[id]));
  45. pool[id][pool[id].Count - 1].transform.parent = this.transform;
  46. return pool[id][pool[id].Count - 1];
  47. }
  48.  
  49. public GameObject activate(int id,Vector3 position, Quaternion rotation) // deactive at a specific location
  50. {
  51. for (int count = 0; count < pool[id].Count; count++)
  52. {
  53. if(!pool[id][count].activeSelf)
  54. {
  55. pool[id][count].SetActive(true);
  56. pool[id][count].transform.position = position;
  57. pool[id][count].transform.rotation = rotation;
  58. return pool[id][count];
  59. }
  60. }
  61. // Error Handling
  62. pool[id].Add((GameObject)Instantiate(objects[id]));
  63. pool[id][pool[id].Count - 1].transform.position = position;
  64. pool[id][pool[id].Count - 1].transform.rotation = rotation;
  65. pool[id][pool[id].Count - 1].transform.parent = this.transform;
  66. return pool[id][pool[id].Count - 1];
  67. }
  68.  
  69. public void deActivate(GameObject deActivateObject)
  70. {
  71. deActivateObject.SetActive(false);
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement