Created
June 22, 2015 03:39
-
-
Save Oxygamer/c93d958ae138aef607b2 to your computer and use it in GitHub Desktop.
Object Pool script for Unity3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
public class ObjectStorage : MonoBehaviour | |
{ | |
public List<GameObject> Storage; | |
public List<GameObject> WeaponStorage; | |
public List<GameObject> InterfaceStorage; | |
public List<GameObject> ShipStorage; | |
private Dictionary<int, string> ShipNames = new Dictionary<int, string>(); | |
public Transform Trash; | |
private Dictionary<string, GameObject> CachedList = new Dictionary<string, GameObject>(); | |
private List<GameObject> TrashList=new List<GameObject>(); | |
private Dictionary<string, GameObject> ShipFastList = new Dictionary<string, GameObject>(); | |
// Use this for initialization | |
public static ObjectStorage Instance; | |
//Load all object. Other scripts can use it | |
void Awake() | |
{ | |
Instance = this; | |
foreach (GameObject obj in Storage) | |
{ | |
CachedList.Add(obj.name, obj); | |
} | |
foreach (GameObject obj in WeaponStorage) | |
{ | |
CachedList.Add(obj.name, obj); | |
} | |
foreach (GameObject obj in InterfaceStorage) | |
{ | |
CachedList.Add(obj.name, obj); | |
} | |
foreach (GameObject obj in ShipStorage) | |
{ | |
ShipFastList.Add(obj.name, obj); | |
} | |
ShipNames.Add(1, "Firefly"); | |
ShipNames.Add(2, "FlappyFighter"); | |
ShipNames.Add(3, "Nighthawk"); | |
ShipNames.Add(4, "Ufo"); | |
ShipNames.Add(5, "Miner"); | |
} | |
public GameObject GetObject(string name) | |
{ | |
GameObject result=null; | |
CachedList.TryGetValue(name, out result); | |
return result; | |
} | |
public GameObject GetShip(int id) | |
{ | |
string value=""; | |
ShipNames.TryGetValue(id, out value); | |
if (value == null) | |
{ | |
value = "Firefly"; | |
} | |
return GetShip(value); | |
} | |
public GameObject GetShip(string name) | |
{ | |
GameObject result = null; | |
ShipFastList.TryGetValue(name, out result); | |
return result; | |
} | |
public void AddTrash(GameObject obj) | |
{ | |
TrashList.Add(obj); | |
obj.transform.parent = Trash; | |
} | |
public void CleanTrash() | |
{ | |
// Debug.Log("Clean"); | |
foreach (GameObject obj in TrashList) | |
{ | |
if(obj!=null) | |
Destroy(obj); | |
} | |
TrashList=new List<GameObject>(); | |
} | |
public void SelectiveClean() | |
{ | |
if (TrashList.Count > 200) | |
{ | |
CleanTrash(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment