- Home /
Drawing Multiple Textures to the GUI during runtime
I'm helping code something similar to a scrabble game and have been having trouble with the GUI. What I want to happen is when I collect a letter in the game it will show up on the GUI and if I collect subsequent letters they will also show up on the GUI in a specified place. I already know the place I want them to be but I'm wondering how I can add textures to the GUI during runtime. So far I have the basic GUI being drawn with box's.
void OnGUI(){
if(scrabbleTile != null)
GUI.DrawTexture(new Rect((Screen.width - 600) / 2, Screen.height - 100, 50, 50), scrabbleTile);
GUI.Box(new Rect((Screen.width - 600) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width - 500) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width - 400) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width - 300) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width - 200) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width - 100) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width - 0) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width + 100) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width + 200) / 2, Screen.height - 100, 50, 50), "");
GUI.Box(new Rect((Screen.width + 400) / 2, Screen.height - 100, 50, 50), "");
}
You can see that I'm using the GUI.DrawTexture function if the scrabbleTile(texture I want to be drawn) is not null. Well this doesn't work for me because OnGUI() redraws this texture every frame so what happens is when a second letter is collected the first letter is overwritten with the old one. I want it to be drawn next to the old one and keep the old one. For this too happen I would have to keep this drawtexture function and add another one for each letter that is collected with the right positioning, however I'm not sure how to do this since it's all happening during runtime. Here are some pictures for reference.
Answer by MikeNewall · Apr 16, 2014 at 07:21 PM
You need an array of textures the same length as the word that you're going to display so you'll you have a texture for each box. When you collect a tile you can assign the appropriate texture to its position in the array.
Create an array of textures matching the length of the word:
void Start(){
// Create an array the same length as the word
// All elements will be null
scrabbleTiles = new Texture[numCharacters];
}
When you select a letter you'd assign its texture to a position in the array like
scrabbleTextures[3] = a;
Then in OnGUI you can iterate through the array and draw the textures that aren't null
void OnGUI(){
for (int i = 0; i < scrabbleTiles.Length; i++) {
if(scrabbleTiles[i]) GUI.DrawTexture(new Rect(0, 0, 100, 100), scrabbleTiles[i]);
}
}
You'd need to add some code in there to deal with the position offset of each piece. Hope that makes sense. I need sleep :p
Is it bad to use a for loop in OnGUI() since it's updating every frame?
Yea there is a similar problem I was having earlier, you can't just have a for loop with a single GUI.DrawTexture call because everytime it's called it overwrites the last one so it only draws the last letter picked up. Here's what I mean.
Nvm I figured out what I was doing wrong, I wasn't incrementing correctly when drawing the textures thanks for the help!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
NullReferenceException in a GUI Dialog Method 1 Answer
GUILayout not working for me C# 1 Answer
Can I use GUI.DrawTexture/Graphics.DrawTexture with UGUI? 0 Answers