- Home /
Update Texture type to GUI during runtime
I am creating a Texture during runtime that is created by using the Texture2D.SetPixels() method. I would like to change that texture type to GUI. I found out how to do this with a image that you are importing but this texture is being created at run time and does not exist on the file system. Any ideas how this can be done?
When you done a Texture2D by runtime you can set the size and the quality, so at the beginning you are generating a Texture2D you can use it into a GUI (GUITexture, GUIButton, GUILabel, etc.).
Answer by aldonaletto · Mar 19, 2012 at 11:27 PM
You should create the texture in a Texture2D variable, set the pixels with SetPixels (for block operations) or SetPixel(x,y) for individual pixels, and finally use Apply to apply the changes.
Once you have this job done, just assign the texture to guiTexture (case you're using GUITexture), or use it in a GUI function like DrawTexture.
A simple example derived from the SetPixel doc:
var texture: Texture2D;
function Start () { // Create a new texture: texture = new Texture2D(128, 128); // Fill the texture with Sierpinski's fractal pattern! for (var y : int = 0; y < texture.height; ++y) { for (var x : int = 0; x < texture.width; ++x) { var color = (x&y) ? Color.white : Color.gray; texture.SetPixel (x, y, color); } } // apply the modified pixels at once: texture.Apply();
// if the script is assigned to a GUITexture, just assign the texture to it:
guiTexture = texture;
}
// but if you're using the GUI system, use the texture in a GUI.DrawTexture, for instance:
function OnGUI(){ GUI.DrawTexture(Rect(10,10,60,60), texture); }