- Home /
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;
}
}
Answer by skoandi · Dec 26, 2012 at 12:15 PM
Yes after some more research I figured it out!! :D
// 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;
}
}
Answer by smilefr · May 07, 2019 at 03:12 PM
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;
}
}
Answer by MarkFinn · Dec 25, 2012 at 07:24 PM
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.
Your answer
Follow this Question
Related Questions
Downloading image from the server crash the game in Android Device ? 1 Answer
Downloading files in the Editor 2 Answers
Localhost does not work after launching build from hosting 1 Answer
few questions about using www for getting bunch of images... 0 Answers
Where is downloaded things from editor script being saved? 0 Answers