Skip to content

Instantly share code, notes, and snippets.

@tapuo
Created July 18, 2012 21:05
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 tapuo/3138891 to your computer and use it in GitHub Desktop.
Save tapuo/3138891 to your computer and use it in GitHub Desktop.
unity object cache
import UnityEngine
static class ObjectCache(MonoBehaviour):
public pool as Hash = {}
public target as Hash = {}
#
def OCInstantiate(obj as GameObject, pos as Vector3, rot as Quaternion):
name as string = obj.name
if ObjectCache.pool[name] == null:
ObjectCache.target[name] = obj
ObjectCache.pool[name] = []
s as List = ObjectCache.pool[name] cast List
t as GameObject
if len(s) > 0:
t = s.Pop()
if t: # destroyed?
t.transform.position = pos
t.transform.rotation = rot
t.active = true
t.SendMessage("Start") #restart
return t
t = Object.Instantiate(ObjectCache.target[name] cast GameObject, pos, rot)
t.name = name
return t
def OCDestroy(obj as GameObject):
obj.active = false
(ObjectCache.pool[obj.name] cast List).Push(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment