• 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 GlitchM · Aug 27, 2012 at 11:57 PM · guitexturelag

How do I prevent the GUI from creating Lag?

I am trying to simulate a keyboard on screen using pngs as GUI textures that form the background of the OnGUI() button control. However when I run the scene there is a lot of lag. I'm sure there is a better way of doing this so please let me know how i could make my code better.

the pngs I'm using are 81 x 82 pixels with a gap of 5px between buttons.

I have used the following code to do this

 public Texture[] alphaButtons;

 void OnGUI () 
     {    
         
         GUI.BeginGroup (new Rect (Screen.width / 2 - 428, Screen.height / 2 + 128, 855, 256));
         // All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
                 
         // Alphabet buttons
         
         /****************************First Row****************************/
         if (GUI.Button(new Rect(0, 0, 81, 82), alphaButtons[0], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(86, 0, 81, 82), alphaButtons[1], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(172, 0, 81, 82), alphaButtons[2], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(258, 0, 81, 82), alphaButtons[3], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(344, 0, 81, 82), alphaButtons[4], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(430, 0, 81, 82), alphaButtons[5], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(516, 0, 81, 82), alphaButtons[6], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(601, 0, 81, 82), alphaButtons[7], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(688, 0, 81, 82), alphaButtons[8], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(774, 0, 81, 82), alphaButtons[9], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         /****************************Second Row*****************************/
         
         if (GUI.Button(new Rect(0, 87, 81, 82), alphaButtons[10], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(86, 87, 81, 82), alphaButtons[11], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(172, 87, 81, 82), alphaButtons[12], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(258, 87, 81, 82), alphaButtons[13], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(344, 87, 81, 82), alphaButtons[14], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(430, 87, 81, 82), alphaButtons[15], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(516, 87, 81, 82), alphaButtons[16], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(601, 87, 81, 82), alphaButtons[17], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(688, 87, 81, 82), alphaButtons[18], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         
         /****************************Third Row****************************/
         
         if (GUI.Button(new Rect(0, 174, 81, 82), alphaButtons[19], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(86, 174, 81, 82), alphaButtons[20], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(172, 174, 81, 82), alphaButtons[21], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(258, 174, 81, 82), alphaButtons[22], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(344, 174, 81, 82), alphaButtons[23], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(430, 174, 81, 82), alphaButtons[24], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         if (GUI.Button(new Rect(516, 174, 81, 82), alphaButtons[25], "default1"))
         {
             GameObject.Find("Soldier").SendMessage("mistakeMade");
         }
         
         // End the group we started above. This is very important to remember!
         GUI.EndGroup ();
     }
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
2
Best Answer

Answer by TheDarkVoid · Aug 28, 2012 at 12:02 AM

just add this to the start of your OnGUI:

 useGUILayout = false;

This means you wont be able to use GUILayout or GUI.window. but ti will reduce lag. Btw you can do all that in a lot less lines of code: See Here

Comment
Add comment · Show 1 · 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 GlitchM · Aug 28, 2012 at 07:26 AM 0
Share

Thanks a lot! That script worked perfectly. Just had to tweak slightly to personalise for my scene but absolutely no lag :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

GUI.DrawTexture not working? (nothing shows up) 1 Answer

Inter Scene Loading Screen Animation 3 Answers

GUI texture size doesn't match control set with the same size 1 Answer

Touchscreen for unity 2 Answers

water mark texture 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