- Home /
How to assign multiple EditorApplication.update delegates in JS?
I have an object which needs to tie into the editor's update loop to keep itself up to date. I thus added this code to it's primary JS script:
#if UNITY_EDITOR
function OnEnable() {
EditorApplication.update = Update;
}
#endif
I also need to have multiple object's of it's type in a scene, so I modified the code to this:
#if UNITY_EDITOR
function OnEnable() {
EditorApplication.update += Update;
}
function OnDisable() {
EditorApplication.update -= Update;
}
#endif
Unfortunately, Unity's compiler balks at the syntax:
BCE0051: Operator '+' cannot be used with a left hand side of type 'function(): void' and a right hand side of type 'function(): void'.
Isn't this a basic feature of delegates? Am I doing something wrong, or is Unity's js implementation totally screwed up? (I know we couldn't create multidimensional arrays for quite a while, is this a similar type of issue?) If so, what's the best way to work around it?
Here's some similar topics:
http://answers.unity3d.com/questions/15889/editorapplication-update-resetting
http://forum.unity3d.com/threads/9679
http://msdn.microsoft.com/en-us/library/ms173175(v=vs.80).aspx
Here's an idea - would I be best off creating my own delegate manager, such as this: http://answers.unity3d.com/questions/48542/how-can-i-declare-a-static-public-delegate-in-js ? I still can't imagine how this solution could work nicely with other objects that bypassed the manager...
Answer by Mike 3 · Mar 29, 2011 at 06:39 PM
Unityscript doesn't support it
I would make a simple c# script which exposes static Add and Remove functions, which do += and -= for you
Works good, thanks. I sure look forward to the day when each language is fully supported though...
Answer by Andrew 10 · Oct 25, 2011 at 10:26 AM
Try (instead of += which does not work in JS):
var cb:EditorApplication.CallbackFunction = Update;//function Update
EditorApplication.update = System.Delegate.Combine(cb, EditorApplication.update);