Created
July 18, 2012 21:05
-
-
Save tapuo/3138891 to your computer and use it in GitHub Desktop.
unity object cache
This file contains hidden or 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
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