Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hathakas · Sep 12, 2015 at 04:25 AM · lagprofileronguiswitch-case

OnGui() Causing lag, am I doing it wrong?

Hey guys,

When I added this line of code, my game gets super laggy.

         void OnGUI() 
     {
             
         if (nanoStage1States == NanoStage1States.Level1)
         {    
 
             pressed = GUILayout.Toggle(pressed, "Toggle me !", "Button");
             Debug.Log(pressed);
         }
         Debug.Log(nanoStage1States);
     }

When level1 becomes true, the fps drops from 70-80 to like 20-30. The profiler also pointed to it. I commented it out and there lag went away.

Is there a better way to do this?

Thanks a lot :)

Comment
Add comment · Show 7
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 Hathakas · Sep 12, 2015 at 04:35 AM 0
Share

bool tessst = false;

     void OnGUI() 
 {
         
     if (nanoStage1States == NanoStage1States.Level1 && tessst == false)
     {    

         pressed = GUILayout.Toggle(pressed, "Toggle me !", "Button");
         Debug.Log(pressed);
         Debug.LogError("IF TOGGLE IS NOT THERE, IT $$anonymous$$AY HAVE APPEARED FOR ONLY A SEC");
         tessst = true;
     }
     Debug.Log(nanoStage1States);
 }

Hmmm, this has less lag but the toggle button doesnt show, I did get the LogError only once, So I'm assu$$anonymous$$g the toggle button was there for one frame?

avatar image Eno-Khaon · Sep 12, 2015 at 05:06 AM 0
Share

Well, either way, the GUI is impressively inefficient in large quantities. If you have a simple text label and duplicate it a few dozen times, your framerate tanks.

That said, I can't say I know exactly why a single GUI object would hit quite so hard, but just as a thought, you may want to try comparing your current use of "GUILayout.Toggle()" with "GUI.Toggle()" to see whether one may be doing significantly worse than the other. If that also doesn't improve it, I would recommend testing a variety of GUI pieces to see whether it's only that one type impacting framerate so much, or if it is even something like having more copies of that GUI object in the same place than you might think.

Then, if all else fails, I believe the new GUI system (well, "new" now that it's been around for a few versions) may provide better performance.

avatar image Hathakas Eno-Khaon · Sep 12, 2015 at 05:20 AM 0
Share

Thank you for the response. I've barely played with UI so this is new for me lol. Is it actually supposed to call the OnGUI every frame like that? Can I not just create it once? I feel like my script is creating a toggle button everyframe?

avatar image Eno-Khaon Hathakas · Sep 12, 2015 at 06:43 AM 0
Share

Yep. In fact, OnGUI() is called twice per frame, in general.

In order to keep something displayed, you'll have to state that you want it displayed every frame (i.e. in OnGUI()), so that aspect of it's pretty much normal.

But, that's not really important or a big deal as far as that's concerned. That's simply the GUI behaving the way it's supposed to. $$anonymous$$ainly, just bear in $$anonymous$$d that for every GUI object you create, you're generally adding one more draw call to your scene. So, if you're looking to display a large amount of information on screen in the GUI, it might not hurt to try and condense it into as few separate objects as is reasonably possible.

Show more comments
avatar image Lo0NuhtiK · Sep 12, 2015 at 08:43 AM 0
Share

All of the Debugs are what is causing the lag.

avatar image Hathakas Lo0NuhtiK · Sep 12, 2015 at 03:21 PM 0
Share

I only added the debugs to find when the lag was happening, this was before I found out there was such a thing as profiler lol. The profiler pointed to OnGUI()

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Highwalker · Jan 24, 2016 at 05:15 AM

OnGui is notoriously slow!! I would just avoid using it.

Comment
Add comment · 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

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Massive (>1sec) lag spikes in game, within 10 seconds of starting 1 Answer

Game FPS suddenly slow on Android 1 Answer

How to check to see if Switch Case is true. 1 Answer

What is UnityChoreograp? 0 Answers

Garbage Collection Constant Lag Spikes? 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