Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fearthefrozt · Apr 16, 2014 at 06:54 PM · c#guionguidrawtexture

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.

alt text

alt text

gui1.png (394.8 kB)
gui2.png (190.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fearthefrozt · Apr 16, 2014 at 08:11 PM 0
Share

Is it bad to use a for loop in OnGUI() since it's updating every frame?

avatar image Fearthefrozt · Apr 16, 2014 at 08:24 PM 0
Share

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.

alt text

alt text

guigui2.png (164.6 kB)
guigui.png (192.7 kB)
avatar image Fearthefrozt · Apr 17, 2014 at 04:50 PM 0
Share

Nvm I figured out what I was doing wrong, I wasn't incrementing correctly when drawing the textures thanks for the help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

GUI DrawTexture is sliding across the screen? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges