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 /
avatar image
1
Question by Suyuanhan · Jun 16, 2011 at 10:12 AM · disableongui

Is there a good method to stop the OnGUI function?

I want to stop OnGUI function when I don't want to use it, but it defined in void OnGUI function ,and it will caculate every frame by computer. And I want to use it when I want to display something ,and disable it when i don't want it to display.

Is some good method to use?

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

2 Replies

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

Answer by eheimburg · Jun 16, 2011 at 11:28 AM

The most CPU-efficient way to do this is to put the OnGUI on a separate script. That way you can set the script's "enabled" boolean to turn it on and off. (You will need to set the boolean this from a DIFFERENT script, because a disabled script can't re-enable itself!)

For example, your other script would look like:

 var guiScript : GameObject;
 
 function Start()
 {
     guiScript = GetComponent(WhateverICalledTheScript);
 }
 
 function TurnTheGUIOn()
 {
     guiScript.enabled = true;
 }
 
 function TurnTheGUIOff()
 {
     guiScript.enabled = false;
 }
Comment
Add comment · Show 2 · 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 save · Jun 16, 2011 at 11:34 AM 0
Share

Up for CPU-efficiency!

avatar image Eric5h5 · Jun 16, 2011 at 04:56 PM 4
Share

A disabled script can actually enable itself. The only things disabled in a disabled script are Update functions and OnGUI. So for example you could have

 function OnGUI () {
     // stuff
 }
 
 function On$$anonymous$$ouseDown () {
     enabled = !enabled;
 }

on an object, and then you could turn the GUI on/off by clicking the object.

avatar image
1

Answer by save · Jun 16, 2011 at 10:21 AM

OnGUI will continue throughout everything as long as it's called, and it's called as long as the object is still active/appearing in the scene. You could although boolean the content inside it:

 var useGui : boolean = true;
 OnGUI () {
     if(useGui) {
         //Draw all GUI objects
     }
 }

Then call the object from another script/function:

 var objectWithGui : GameObject;
 function disableGui () {
     objectWithGui.GetComponent(script).useGui = false;
 }
Comment
Add comment · Show 2 · 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 glenneroo · May 24, 2019 at 10:37 PM 0
Share

Considering that OnGui is called more frequently than e.g. Update, is this still efficient CPU-wise, particularly with multiple scripts?

avatar image Bunny83 glenneroo · May 25, 2019 at 08:40 AM 0
Share

This makes almost no difference. OnGUI is usually called exactly 2 times per frame (Repaint event and its preceeding Layout event). If other events occur (like $$anonymous$$eyDown, $$anonymous$$ouseDown, $$anonymous$$eyUp, ...) you get an extra call (+ the corresponding Layout event). If you disable GUILayout the Layout event will not be issued anymore so without an actual event OnGUI runs just as often as Update / LateUpdate.


What do you mean by "multiple"? Do you talk about 2-10 or about 500-5000? OnGUI isn't very efficient to begin with since each control will issue seperate drawcalls. So the performance gain of using a boolean to disable all the gui content or disabling the whole script is about the same. Disabling the script of course will be slightly better since the whole processing of the event(s) on the unity side will be gone. Though this is in the range of micro optimisations. If you worry, just use the profiler and do a deep profile on your project.

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

6 People are following this question.

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

Related Questions

How to : enable/disable OnGUI() script on C# 1 Answer

Disable function OnGUI after 4 seconds 1 Answer

keep collider disabled even when the player re-enter the scene 1 Answer

Disable collision before collision actually happens 1 Answer

Spawning different GameObjects once player reaches certain score 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