- Home /
How to assign an image to the Texture class in script
I have a variable of class Texture and I want to assign an image to it without using the Inspector. I want to assign the image to the class Texture in the actual script. I couldnt find the answer in the documentation. Anyone know how to do this?
Not sure of your question You can assign one texture to another:
Texture tex1;
Texture tex2;
tex1 = tex2;
But in order for this to be meaningful, you have to get the texture from somewhere:
Assign one of them in the inspector.
Load the texture from Resources()
Create the texture from scratch
$$anonymous$$odify an existing texture
...
What i want to do is assign an actual image file(image.png .jpg)to the Texture variable inside the script without using the inspector.
Answer by robertbu · Jun 18, 2013 at 01:24 AM
You have two choices. If the image file comes form Assets/Resources, you can use Resources.Load().
var tex : Texture = Resources.Load("ImageName") as Texture;
If the file comes from a directory on the machine, or if it comes from the net you can use the WWW class:
var url = "file://C:/someplace/ImageName.png";
var www : WWW = new WWW (url);
yield www;
var tex = www.texture;
Thanks! that's what I was looking for! works like a charm!