How exactly do I use EditorApplication.update? (Javascript)

I have a script inside my editor folder. The script looks (entirely) like this:

EditorApplication.update = MyUpdate;
function MyUpdate()
{
	print("Hello");
}

I assumed that my editor console would be printing the message 100 times per second but it does nothing. Clearly I have the wrong idea of how this works. Can anyone enlighten me? Cheers

Ok, usually you subscribe as listener to a delegate with delegate += myfunction and unsubscribe with delegate -= myfunction. However it seems that UnityScript (Javascript) doesn’t support this. It seems you can only assign one function at the same time (which is not very nice but i guess you can get around that except using C# ;)).

Your first problem is that you can’t assign the delegate in the class-body. You have to do it in a function.

When using ExecuteInEditMode, Start() is also called whenever you enter the editmode (after stopping the playmode). You can use EditorApplication.isPlaying to check whether you enter edit or play mode. I’ve done some tests and Start is only called at playmode changes. When Unity recompiles your scripts, Start() isn’t called again. An alternative would be OnEnable() which is called also after the recompilation is completed.

I can remember that you wanted to download “something” (assetbundles, whatever) in the editor. It could look like this:

// UnityScript
@script ExecuteInEditMode()

private var www : WWW;
var target : Renderer;

function OnEnable()
{
	LoadTexture("http://download.unity3d.com/webplayer/images/unity-icon-big.jpg");
}

function LoadTexture(url)
{
    www = new WWW(url);
	#if UNITY_EDITOR
    if (!EditorApplication.isPlaying)
        EditorApplication.update = MyUpdate;
	else
	    WaitForDownload();
	#else
	WaitForDownload();
    #endif
}

#if UNITY_EDITOR
function MyUpdate ()
{
    if (www.isDone)
	{
	    EditorApplication.update = null;
		LoadCompleted();
	}
}
#endif

function WaitForDownload()
{
    yield www;
	LoadCompleted();
}

function LoadCompleted()
{
    target.sharedMaterial.mainTexture = www.texture;
}

Note: this script is designed to be used at runtime es well as in the editor. When ever the script is enabled (entering of leaving plamode / recompile) it will download a texture. Depending on whether we are in edit or play mode it does two different things to wait for the download. In the editor it uses the update-delegate which is only available in the editor to continously check if the download is completed. In playmode or when you create a build a coroutine is called that will wait for the download.

Both checking-methods will call LoadCompleted() when the download is finished. The update-delegate removes itself so it’s no longer being called. LoadCompleted just assigns the texture to a referenced renderer. Keep in mind that this script will download the texture every time you start OR stop the game in the editor.

Just a sidenote: Don’t try to access the .material property of any renderer at edit mode. This will cause a memory leak. In edit mode you should only access the shared properties (sharedMaterial / sharedMaterials)

I’m still not sure how (or for what) you want to use the downloaded stuff, but usually you create editor scripts for such tasks. ExecuteInEditMode does NOT turn the script into an editor script. It’s still a runtime script. “True” Editorscripts aren’t included in the build and are ment to extend the Unity3D editor.

You can use OnWizardUpdate for extended ScriptableWizard. OR

You can use Update() for extended EditorWindow.

OKay this is really cool and has helped me a lot. I now have a the debug.console spitting out messages all the time during edit mode which was my main aim. Regarding the asset bundles I understand how this should work (using isDone instead of a yield), howver although the following code spits out the editor update running' message every frame, the download never triggers its 'isDone' function.

function EditorUpdate () {

    print("editor update running. download="+download);//this prints every frame yey!!

    if(assetRequested==false)
    {
        assetRequested=true;
        LoadAssetBundle("someBundle.unity3d");
    }   

    if (download.error != null) 
    { 
        Debug.LogError(download.error); 
        DebugConsole.Log(download.error); 
    }
    if (download.isDone)
    {
        //NEVER GETS HERE :(
        print("download done inside editor");
        assetRequested=false;
    }

}

Anyone any idea why the isDone function never gets triggered? I heard there maybe a bug in the isDone function. Is this correct, or is it something else?

P.S I would've posted this as a comment, but I cant seem to be able to paste code into comments.