Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PickAxeTestManager : MonoBehaviour {
  5.  
  6. public GameObject PickAxeprefab;
  7.  
  8. void Start ()
  9. {
  10. PickAxePoolManager.instance.CreatePool (PickAxeprefab, 2); //CreatePool is a method in PickAxePoolManager
  11. }
  12.  
  13.  
  14. void Update ()
  15. {
  16.  
  17. if (Input.GetKeyDown (KeyCode.A))
  18. {
  19. PickAxePoolManager.instance.ReuseObject(PickAxeprefab, Vector3.zero, Quaternion.identity); //ReuseObject is also a method in PickAxePoolManager
  20. }
  21.  
  22. if (ResetByWall.instance.OnTriggerEnter()) //ERROR CS1501 is here. "No overload for method OnTriggerEnter takes 0 arguments".
  23. {
  24. PickAxePoolManager.instance.ReuseObject(PickAxeprefab, Vector3.zero, Quaternion.identity); // Same here...
  25. }
  26. }
  27. }
  28.  
  29. using UnityEngine;
  30. using System.Collections;
  31.  
  32. public class ResetByWall : MonoBehaviour {
  33.  
  34. public bool collided;
  35.  
  36. //NOTE:
  37. //Singleton Pattern from lines 12 to 25 //
  38. // ************************************
  39.  
  40. static ResetByWall _instance; // Reference to the Reset By Wall script
  41.  
  42. public static ResetByWall instance // This is the accessor
  43. {
  44. get
  45. {
  46. if(_instance == null) // Check to see if _instance is null
  47. {
  48. _instance = FindObjectOfType<ResetByWall>(); //Find the instance in the Reset By Wall script in the currently active scene
  49. }
  50.  
  51. return _instance;
  52. }
  53. }
  54.  
  55. //public GameObject prefab;
  56.  
  57. public void OnTriggerEnter(Collider other) {
  58.  
  59. collided = true;
  60.  
  61. if (other.gameObject.tag == "Pick Axe_PoolerTest") //I tagged the prefab as "Pick Axe_PoolerTest"
  62. {
  63. Debug.Log("Pick Axe entered the trigger!");
  64.  
  65. Destroy(other.gameObject);
  66.  
  67. }
  68.  
  69. }
  70. }
  71.  
  72. using UnityEngine;
  73. using System.Collections;
  74. using System.Collections.Generic;
  75.  
  76. public class PickAxePoolManager : MonoBehaviour {
  77.  
  78. Dictionary<int,Queue<GameObject>> poolDictionary = new Dictionary<int,Queue<GameObject>>();
  79.  
  80. //NOTE:
  81. //Singleton Pattern used from lines 12 to 25
  82.  
  83. static PickAxePoolManager _instance; // Reference to the Pool Manager script
  84.  
  85. public static PickAxePoolManager instance // This is the accessor
  86. {
  87. get
  88. {
  89. if(_instance == null) // Check to see if _instance is null
  90. {
  91. _instance = FindObjectOfType<PickAxePoolManager>(); //Find the instance in the Pool Manager script in the currently active scene
  92. }
  93.  
  94. return _instance;
  95. }
  96. }
  97.  
  98. /// <summary>
  99. /// Creates the pool.
  100. /// </summary>
  101. /// <param name="prefab">Prefab.</param>
  102. /// <param name="poolSize">Pool size.</param>
  103.  
  104. public void CreatePool(GameObject prefab, int poolSize)
  105. {
  106. int poolKey = prefab.GetInstanceID (); // Unique integer for every GameObject
  107.  
  108. if (!poolDictionary.ContainsKey (poolKey)) //Make sure poolKey is not already in the Dictionary,
  109. //if it's not then we can create the pool
  110. {
  111. poolDictionary.Add(poolKey, new Queue<GameObject>());
  112.  
  113. for (int i = 0; i < poolSize; i++) //Instantiate the prefabs as dictated by the "poolSize" integer
  114. {
  115. GameObject newObject = Instantiate (prefab) as GameObject; //Instantiate as a GameObject
  116. newObject.SetActive(false); // Don't want it to be visible in the scene yet
  117. poolDictionary [poolKey].Enqueue(newObject); // Add it to our Pool
  118. }
  119. }
  120. }
  121.  
  122. /// <summary>
  123. /// Reuses the object in our pool.
  124. /// </summary>
  125. /// <param name="prefab">Prefab.</param>
  126. /// <param name="position">Position.</param>
  127. /// <param name="rotation">Rotation.</param>
  128.  
  129. public void ReuseObject (GameObject prefab, Vector3 position, Quaternion rotation)
  130. {
  131. int poolKey = prefab.GetInstanceID (); // Get our pool key once again
  132.  
  133. if (poolDictionary.ContainsKey (poolKey)) // Quick check to make sure our pool dictionary contains the pool key
  134. {
  135. GameObject objectToReuse = poolDictionary[poolKey].Dequeue(); //Get the next object in the pool
  136. poolDictionary[poolKey].Enqueue(objectToReuse); // Add the object back onto the end of the queue so it can be reused later
  137.  
  138. objectToReuse.SetActive(true); //Make sure the object was not disabled
  139.  
  140. objectToReuse.transform.position = position; // set position to applied values
  141. objectToReuse.transform.rotation = rotation; // set rotation to applied values
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement