Guest User

Untitled

a guest
Aug 17th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class ObjectPool
  6. {
  7.     #region Fields
  8.  
  9.     private GameObject _parentObject;
  10.  
  11.     private Dictionary<int, GameObject> _sourceByObjectId =
  12.        new Dictionary<int, GameObject>();
  13.  
  14.     private Dictionary<GameObject, Stack<GameObject>> _storage =
  15.         new Dictionary<GameObject, Stack<GameObject>>();
  16.  
  17.     #endregion
  18.  
  19.  
  20.     #region Methods
  21.  
  22.     public void SetParentObject(GameObject parentObject)
  23.     {
  24.         _parentObject = parentObject;
  25.     }
  26.  
  27.     public void Create(GameObject source, int count)
  28.     {
  29.         Stack<GameObject> objectsStack;
  30.         bool hasStack = _storage.TryGetValue(source, out objectsStack);
  31.  
  32.         if (!hasStack)
  33.         {
  34.             objectsStack = new Stack<GameObject>();
  35.             _storage[source] = objectsStack;
  36.         }
  37.  
  38.         for (int i = 0; i < count; i++)
  39.         {
  40.             var pooledObject = Object.Instantiate(source) as GameObject;
  41.  
  42.             pooledObject.SetActive(false);
  43.             pooledObject.name = source.name;
  44.  
  45.             if (_parentObject != null)
  46.             {
  47.                 pooledObject.transform.parent = _parentObject.transform;
  48.             }
  49.  
  50.             objectsStack.Push(pooledObject);
  51.  
  52.             int objectId = pooledObject.GetInstanceID();
  53.             _sourceByObjectId[objectId] = source;
  54.         }
  55.     }
  56.  
  57.     public GameObject Get(GameObject source)
  58.     {
  59.         return Get(source, Vector3.zero, Quaternion.identity);
  60.     }
  61.  
  62.     public GameObject Get(GameObject source, Vector3 initialPosition, Quaternion initialRotation)
  63.     {
  64.         Stack<GameObject> objectsStack;
  65.         bool hasStack = _storage.TryGetValue(source, out objectsStack);
  66.  
  67.         if (!hasStack)
  68.         {
  69.             objectsStack = new Stack<GameObject>();
  70.             _storage[source] = objectsStack;
  71.         }
  72.  
  73.         GameObject pooledObject = null;
  74.  
  75.         if (objectsStack.Count == 0)
  76.         {
  77.             pooledObject = Object.Instantiate(source) as GameObject;
  78.  
  79.             pooledObject.name = source.name;
  80.  
  81.             int objectId = pooledObject.GetInstanceID();
  82.             _sourceByObjectId[objectId] = source;
  83.         }
  84.         else
  85.         {
  86.             pooledObject = objectsStack.Pop();
  87.             pooledObject.SetActive(true);
  88.         }
  89.  
  90.         pooledObject.transform.parent = null;
  91.         pooledObject.transform.position = initialPosition;
  92.         pooledObject.transform.rotation = initialRotation;
  93.  
  94.         return pooledObject;
  95.     }
  96.  
  97.     public void Release(GameObject releasedObject)
  98.     {
  99.         int objectId = releasedObject.GetInstanceID();
  100.  
  101.         GameObject source;
  102.         bool hasSource = _sourceByObjectId.TryGetValue(objectId, out source);
  103.  
  104.         if (!hasSource)
  105.         {
  106.             throw new UnityException("Not found a source of pooled object.");
  107.         }
  108.  
  109.         releasedObject.SetActive(false);
  110.  
  111.         if (_parentObject != null)
  112.         {
  113.             releasedObject.transform.parent = _parentObject.transform;
  114.         }
  115.  
  116.         Stack<GameObject> objectsStack = _storage[source];
  117.         objectsStack.Push(releasedObject);
  118.     }
  119.  
  120.     #endregion
  121. }
Advertisement
Add Comment
Please, Sign In to add comment