Created
February 15, 2016 17:51
-
-
Save aberloni/70d256cb0427d5f8dec6 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
public class CallbackManager { | |
public class CallbackCategory { | |
public string name = ""; | |
public List<CallbackInfo> infos; | |
public CallbackCategory(string catName) { | |
name = catName; | |
infos = new List<CallbackInfo>(); | |
} | |
public CallbackInfo getInfoByGameObject(GameObject obj) { | |
for (int i = 0; i < infos.Count; i++) | |
{ | |
if (infos[i].obj == obj) return infos[i]; | |
} | |
return null; | |
} | |
public bool hasObject(GameObject obj) { | |
for (int i = 0; i < infos.Count; i++) | |
{ | |
if (infos[i].obj == obj) return true; | |
} | |
return false; | |
} | |
public bool hasInfo(CallbackInfo info) { return infos.Contains(info); } | |
public CallbackInfo getInfo(GameObject obj) { | |
for (int i = 0; i < infos.Count; i++) | |
{ | |
if (infos[i].obj == obj) return infos[i]; | |
} | |
return null; | |
} | |
public void throwCallbacks() { | |
for (int i = 0; i < infos.Count; i++) | |
{ | |
//Debug.Log(infos[i].obj); | |
if(infos[i].obj != null) infos[i].callback(); | |
} | |
} | |
} | |
public class CallbackInfo { | |
public GameObject obj; | |
public Action callback; | |
public CallbackInfo(GameObject obj, Action cb) | |
{ | |
this.obj = obj; | |
this.callback = cb; | |
} | |
} | |
static public List<CallbackCategory> data; | |
static public CallbackCategory getCategory(string name) { | |
if(data == null) data = new List<CallbackCategory>(); | |
for (int i = 0; i < data.Count; i++) | |
{ | |
if (data[i].name == name) return data[i]; | |
} | |
data.Add(new CallbackCategory(name)); | |
return getCategory(name); | |
} | |
static public void subscribe(string key, CallbackInfo info) { | |
CallbackCategory cat = getCategory(key); | |
if (!cat.infos.Contains(info)) cat.infos.Add(info); | |
//clear empty references | |
List<CallbackInfo> tmp = new List<CallbackInfo>(); | |
for (int i = 0; i < cat.infos.Count; i++) | |
{ | |
if (cat.infos[i].obj != null) tmp.Add(cat.infos[i]); | |
} | |
cat.infos = tmp; | |
} | |
static public void unsubscribe(string key, GameObject obj) { | |
CallbackCategory cat = getCategory(key); | |
CallbackInfo info = cat.getInfoByGameObject(obj); | |
if (info != null) unsubscribe(key, info); | |
} | |
static public void unsubscribe(string key, CallbackInfo info) { | |
CallbackCategory cat = getCategory(key); | |
if (cat.infos.Contains(info)) cat.infos.Remove(info); | |
} | |
static public void throwCallbacks(string key) { | |
CallbackCategory cat = getCategory(key); | |
cat.throwCallbacks(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment