- Home /
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?
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;
}
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.
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;
}
Considering that OnGui is called more frequently than e.g. Update, is this still efficient CPU-wise, particularly with multiple scripts?
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
Follow this Question
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