Loading images from web to editor window

Hi! I’m trying to download a image from a web, and use it. I’ve looked around and I can’t find something. Can this even be done?

class EditorGUITextures extends EditorWindow {

    var texture : Texture2D;
	var url : String = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
	var done : boolean = false;
    var aString : String = "nothing";
    
    @MenuItem("Examples/Texture Previewer")
    static function Init() {
        var window = GetWindow(EditorGUITextures);
        window.position = Rect(100,100,400, 200);
        window.Show();
    }
    
    function OnGUI() {
       texture = EditorGUI.ObjectField(Rect(3,3,200,20),"Add a Texture:",texture,Texture);
	   GUI.Label(Rect(100,200,100,50),aString);
       if(texture){
       		EditorGUI.PrefixLabel(Rect(25,45,100,15),0,GUIContent("Preview:"));
            EditorGUI.DrawPreviewTexture(Rect(25,60,100,100),texture);
       }
       if(GUI.Button(Rect(0,190,100,30),"Download")){
       		GetIcon();
       }
    }
    function GetIcon(){
    	var www : WWW = new WWW (url);
    	yield www;
    	aString = "Done!";
    	print("done");
    	texture = www.texture;
    	done = true;
    }
}

Yes after some more research I figured it out!! :smiley:

// UnityScript
@script ExecuteInEditMode()




class MyWindow extends EditorWindow {
	
	private var www : WWW;
	var target : Renderer;
    var texture : Texture2D;

    @MenuItem ("Window/My Window")
    static function ShowWindow () {
        EditorWindow.GetWindow (MyWindow);
    }

    function OnGUI () {
        if(GUILayout.Button("Download")){  LoadTexture("http://download.unity3d.com/webplayer/images/unity-icon-big.jpg"); }
        texture = EditorGUI.ObjectField(Rect(3,60,200,20),"Add a Texture:",texture,Texture);
        if(texture){
        	EditorGUI.DrawPreviewTexture(Rect(25,150,100,100),texture);
        }
    }
	/*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;
	    texture = www.texture;
	}
}

Here is my method, works without depending on update:

        public static void DownloadImage(Material m, string url)
        {
            using (WebClient client = new WebClient())
            {
                byte[] data = client.DownloadData(url);
                Texture2D tex = new Texture2D(2, 2);
                tex.LoadImage(data);

                m.mainTexture = tex;
            }
        }

I never did get any answers on this one. Not very useful ones anyway.

Short answer, yes it is possible.
But :
The editor goes to sleep and never properly finishes processing the WWW actions, unless you keep poking it to wake it up.

You can see a similiar question I asked some time back on my account page.