- Home /
GUI.BeginGroup = 1-2 fps
Hello, i have the following code. It works like this. When i move my mouse over a asteroid it opens a gui window at the mouse pos. In this window i will display the content of the Asteroid (the var´s at the bottom). It´s all working fine except the fact that when i insert GUI.EndGroup (); it runs with about 1-2 fps. And also it displays a gui window in the top left that shouldnd be there. If i leave the GUI.EndGroup away i get an error that says "Invalid GUIClip stack popping"
Any ideas why?
Thanks in advance, Alex
Here is the code: var initialColor : Color; var GUIopen : boolean = false; var boxPos: Vector3;
var H : float;
var O : float;
var C : float;
var Fe : float;
var Au : float;
function Start()
{
initialColor = renderer.material.color;
}
function OnMouseOver()
{
renderer.material.color = Color.green;
boxPos.x = Input.mousePosition.x;
boxPos.y = Screen.height - Input.mousePosition.y;
GUIopen = true;
}
function OnMouseExit()
{
renderer.material.color = initialColor;
GUIopen = false;
}
function OnGUI ()
{
if (GUIopen)
GUI.BeginGroup (Rect (boxPos.x, boxPos.y, 150, 200));
GUI.Box (Rect (0,0,150,200), "Content:" );
//GUI.EndGroup(); //if i put that there it runs very slow if not i get the error as mentioned above
}
function Update()
{
//H = GetComponent(AsteroidResourceGen).H ;
//O = GetComponent(AsteroidResourceGen).O ;
//C = GetComponent(AsteroidResourceGen).C ;
//Fe = GetComponent(AsteroidResourceGen).Fe ;
//Au = GetComponent(AsteroidResourceGen).Au ;
}
I suspect it's because you have a material change in On$$anonymous$$ouseOver (each frame the mouse is OVER your collider/GUIElement). On$$anonymous$$ouseEnter could, maybe, work better.
i had the script working without the gui stuff and it worked like this perfectly. So i dont think it is because of that. It has to do with the Gui function stuff. hmm any other ideas?
Answer by Hoeloe · Feb 09, 2014 at 02:04 PM
Your problem is syntactic.
Let me address these lines:
if (GUIopen)
GUI.BeginGroup (Rect (boxPos.x, boxPos.y, 150, 200));
GUI.Box (Rect (0,0,150,200), "Content:" );
GUI.EndGroup();
Your problem is not GUI.EndGroup(), but actually the conditional above. Let's address the syntax of conditional statements.
Conditionals have two basic forms, that I call "block conditionals" and "line conditionals". Most conditional statements will be block conditionals. These look like this:
if (condition)
{
//do a thing
}
If the condition is true here, everything inside the following block will be executed, otherwise it will be skipped.
Now, line conditionals are slightly different. They look like this:
if(condition)
//do this line
I call these "line" conditionals because they are designed for short statements. The semantics of conditional statements is that if you don't specify where the conditional block begins and ends, it is assumed to be one line long. So, for example, this code:
if(x < 5)
{
x ++;
y = 4;
}
Will NOT be the same as this code:
if(x < 5)
x ++;
y = 4;
If we run both of those with x = 6 and y = 0, the first will produce x = 6, y = 0, while the second will produce x = 6, y = 4, because the line conditional does not include the assignment in its scope.
So, with this in mind, let's take another look at your code:
if (GUIopen)
GUI.BeginGroup (Rect (boxPos.x, boxPos.y, 150, 200));
GUI.Box (Rect (0,0,150,200), "Content:" );
GUI.EndGroup();
Now, I don't see a block definition anywhere (that's the curly brackets), so this must be a line conditional.
Oops.
What this means is that your program would be better written like this:
if (GUIopen)
GUI.BeginGroup (Rect (boxPos.x, boxPos.y, 150, 200));
GUI.Box (Rect (0,0,150,200), "Content:" );
GUI.EndGroup();
Which is functionally identical to this:
if (GUIopen)
{
GUI.BeginGroup (Rect (boxPos.x, boxPos.y, 150, 200));
}
GUI.Box (Rect (0,0,150,200), "Content:" );
GUI.EndGroup();
Now you might start to see what is causing the problem. If GUIopen is true, then this will execute just fine. If GUIopen is false, however, then you skip the BeginGroup call. This doesn't cause a problem when calling GUI.Box, but you then call GUI.EndGroup. However, you haven't actually started a group under these circumstances, so you are trying to pop something off the stack that doesn't exist.
What was that error you said you got?
Invalid GUIClip stack popping
The frame rate drop is likely actually from the console printing out hundreds and hundreds of error messages.
Hopefully this has taught you a thing or two, and you can see how to fix it.
Ahh ok. I never knew that there was a diffrence between these if statments. Thanks for the great explanation.
Your answer
Follow this Question
Related Questions
The next problem with gui. 1 Answer
Showing text near object 1 Answer
Setting Scroll View Width GUILayout 1 Answer
How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers
Circle GUI Button, Javascript? 1 Answer