- Home /
LoadImageIntoTexture + GetPixels + SetPixels
Hi,
I am trying to load an image into a texture, split it up in 3x 1/3 parts and assign these 1/3 parts to three different walls. The problem is that it doesn't assign the downloaded texture to the walls at all and I don't know what I did wrong.
Here's the code:
var texture : Texture;
var sourceRect : Rect;
var url = "http://www10.pic-upload.de/28.05.13/d8f3w59xp643.jpg";
var wallL : GameObject;
var wallR : GameObject;
var wallT : GameObject;
var EnterName: String = "Enter A Name";
var showBox : boolean = true;
function Start () {
wallL = GameObject.Find("WallL");
wallR = GameObject.Find("WallR");
wallT = GameObject.Find("WallT");
}
function Update () {
}
function OnGUI () {
if (showBox == true) {
Submit = GUI.Button(Rect(Screen.width/2+5, Screen.height/2+25, 165, 25), "Finished");
EnterName = GUI.TextField(Rect(Screen.width/2, Screen.height/2, 175, 60), EnterName);
if (Submit){
url = EnterName;
textureLoad();
}
}
}
function textureLoad () {
texture = new Texture2D(1024, 512, TextureFormat.DXT1, false);
// 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(texture);
// The values in the Rect structure are floats, so round them
// down to the nearest integer.
var x = Mathf.FloorToInt(sourceRect.x);
var y = Mathf.FloorToInt(sourceRect.y);
var width = Mathf.FloorToInt(sourceRect.width);
var height = Mathf.FloorToInt(sourceRect.height);
// Get the pixel block and place it into a new texture.
var pix = texture.GetPixels(x, y, width, height);
var destTex = new Texture2D(width, height);
destTex.SetPixels(pix);
// Set the current object's texture to show the
// extracted rectangle.
renderer.material.mainTexture = destTex;
}
When I do it seperatly, like the LoadImage part only, then this part works fine. It's the same case with the GetPixels and SetPixels. When I use a texture that is already included in Unity, then the split part works fine as well. But when I combine the LoadImage and Get/SetPixels then it just doesn't apply the texture.
PS: I have only a basic setup with three walls and I'm trying to do it with this texture. The points 'x', 'y', 'width' and 'height' are predefined for every wall (e.g. 0, 0, 1024, 170 and 0, 170, 1024, 170 ...).
Thanks in advance for every answer, Opak
Your answer
Follow this Question
Related Questions
Reset a Texture After GetPixels/SetPixels 0 Answers
Setting pixels on a texture? 2 Answers
Draw one texture on to another? Get Set Pixels. 0 Answers