- Home /
Loading textures runtime and assign to multiple button
hi,
i am writing simple script to loading textures and assign this textures to buttons but nothing happening.My debugging works perfectly but buttons not appears.Any help wourld be great. Thanks
here is my script
var Textures : Object[];
function LoadTextures()
{
Textures = Resources.LoadAll("Textures",Texture2D);
}
function OnGUI(){
if(GUI.Button(Rect(Screen.width-177,0,175,30),"Textures"))
{
LoadTextures();
for (var i in Textures)
{
GUI.Button(Rect(Screen.width/2,Screen.height/2,100,100),i as Texture2D);
Debug.Log(i.ToString());
}
Debug.Log(Textures.Length);
}
}
Comment
Best Answer
Answer by Bunny83 · Sep 18, 2012 at 01:52 AM
The body of your if statement ("if(GUI.Button(...))") will only be executed one time when you press the button. You can't draw your buttons inside the if-statement. Unity uses an immediate mode for the GUI elements. You have to execute your GUI elements every frame, so they have to be directly in OnGUI:
var Textures : Object[] = null;
function LoadTextures()
{
Textures = Resources.LoadAll("Textures",Texture2D);
}
funtion OnGUI()
{
if(GUI.Button(Rect(Screen.width-177,0,175,30),"Textures"))
{
LoadTextures();
}
if (Textures != null) // When the textures are loaded
{
for (var i in Textures)
{
GUI.Button(Rect(Screen.width/2,Screen.height/2,100,100),i as Texture2D);
}
}
}
Note: Your GUI.Buttons inside your for loop are all at the exact same position, so they all overlap. Keep in mind to give them unique positions. You can also use GUILayout