- Home /
Question by
DavidDebnar · Apr 15, 2012 at 07:38 PM ·
texture2dwwwwww.loadimageintotexture
www.LoadImageIntoTexture doesn't work?
Hey guys! I tried using www.LoadImageIntoTexture today, but all I got was a grey or really weird texture.
My code:
var url : String;
private var loaded : boolean = false;
var texture : Texture2D;
function OnGUI () {
if(!loaded) {
GUI.Label(Rect(20,20,300,20), "Enter the image url below and click load");
url = GUI.TextField(Rect(20,40,Screen.width-40,20), url);
if(GUI.Button(Rect(20,60,300,20), "Load")) {
loaded = false;
LoadImage();
}
}
GUI.DrawTexture(Rect(20,80,100,100), texture, ScaleMode.StretchToFill);
}
function LoadImage () {
texture = new Texture2D(16,16, TextureFormat.RGB24, false);
while(true) {
var www : WWW = WWW(url);
yield www;
}
www.LoadImageIntoTexture(texture);
}
Any ideas?
David
Comment
Best Answer
Answer by Lttldude · Apr 15, 2012 at 07:50 PM
This worked for me:
var url : String;
private var loaded : boolean = false;
var texture : Texture2D;
function OnGUI () {
if(!loaded) {
GUI.Label(Rect(20,20,300,20), "Enter the image url below and click load");
url = GUI.TextField(Rect(20,40,Screen.width-40,20), url);
if(GUI.Button(Rect(20,60,300,20), "Load")) {
loaded = false;
LoadImage();
}
}
if(loaded)
{
GUI.DrawTexture(Rect(20,80,100,100), texture, ScaleMode.StretchToFill);
}
}
function LoadImage () {
texture = new Texture2D(16,16, TextureFormat.RGB24, false);
var www = WWW(url);
yield www;
www.LoadImageIntoTexture(texture);
loaded = true;
}
I used this as a sample image: facebook_icon
Oh, right, the while(true) was destroying it. Thanks!
Just FYI TextureFormat.DXT1 is a good performance tip on some platforms
Is there a simple way to adapt this so that the image loaded is used for the texture of a 3D object ins$$anonymous$$d of a GUI overlay element?
And aditionally, could this be modified simply to use an OpenFile Dialog to load the texture rather than a URL?