Skip to content

Instantly share code, notes, and snippets.

@Oxygamer
Created June 22, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oxygamer/c93d958ae138aef607b2 to your computer and use it in GitHub Desktop.
Save Oxygamer/c93d958ae138aef607b2 to your computer and use it in GitHub Desktop.
Object Pool script for Unity3d
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