- Home /
LoadImageIntoTexture Runtime Change
Hey,
I'd like to create a script that assigns a texture loaded from the web to three different walls and the url should be changeable within a textfield, so that it changes the texture as well. What I have so far is that it assigns the texture of the predefined url but when I change the url in the textfield, somehow it only changes the texture of one wall. What should I do that it assigns the texture of the new link to every wall? Where's the problem and is there maybe a better solution for what I want to do?
PS: I have just a basic scene with three walls called WallR, WallT and WallL. The "user" should be able to put any link there with an image (ofc with the right format) so it's not possible to preload every texture.
Thank you in advance, Opak
var url = "http://www10.pic-upload.de/28.05.13/2mztreb6f16.jpg";
var wallL : GameObject;
var wallR : GameObject;
var wallT : GameObject;
function Start () {
wallL = GameObject.Find("WallL");
wallR = GameObject.Find("WallR");
wallT = GameObject.Find("WallT");
// Create a texture in DXT1 format
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
while(true) {
// Start a download of the given URL
var www = new WWW(url);
// wait until the download is done
yield www;
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture(renderer.material.mainTexture);
wallL.renderer.material.mainTextureScale = Vector2 (1 , 0.33);
wallT.renderer.material.mainTextureScale = Vector2 (1 , 0.33);
wallT.renderer.material.mainTextureOffset = Vector2 (1 , 0.335);
wallR.renderer.material.mainTextureScale = Vector2 (1 , 0.33);
wallR.renderer.material.mainTextureOffset = Vector2 (1 , 0.67);
}
}
function Update () {
}
function OnGUI () {
url = GUI.TextField (Rect (10, 10, 500, 20), url, 100);
}
Answer by Ashkan_gc · Jun 04, 2013 at 08:55 AM
You are assigning the texture directly to the material of the object which has the script attached. It's beter to put a reference of the texture that you are creating in a variable and set the renderer.material.mainTexture of the 3 wall objects to that texture.