I have only come up with a workaround. I am still curious to see if there is any easier way of going about.
JSDelegate.js
class JSDelegate
{
// Can't use List.<T> - Compile error:
// BCE0015: Node 'callable()' has not been correctly processed.
// private var callbacks : List.<function()> = new List.<function()>();
// So we have to use an ArrayList instead.
private var callbacks : ArrayList = new ArrayList();
function Add(callback : function())
{
callbacks.Add(callback);
}
function Remove(callback : function())
{
callbacks.Remove(callback);
}
function Invoke()
{
for (var callback : function() in callbacks)
callback();
}
}
Example1.js
static var Handler : JSDelegate = new JSDelegate();
function OnEnable()
{
Handler.Add(Bar);
}
function OnDisable()
{
Handler.Remove(Bar);
}
function Bar()
{
Debug.Log("Bar called!");
}
Example2.js
function Update()
{
if (Input.GetKeyDown(KeyCode.E))
Example1.Handler.Invoke();
}
answered
Mar 27 '11 at 12:01 PM
Statement ♦♦
20.2k
●
35
●
71
●
176