- Home /
Need help with a flickering menu problem.
Hello. I need help with a flickering menu. This code I purchased for a menu system. The problem with it is when the background rect expands after clicking on "RoomViews" button to expose 8 sub buttons, it doesn't collapse back to it's original size when I toggle "Room view" button back. I added code to make this happen, but I think I have it in the wrong place because that's when the flickering started. Can someone help me figure out where I need to put the code in order to stop the flickering? The code I added was this:
bool ToggleButton( bool toggle, string text )
{
if (toggle==false)
windowRect = new Rect (20, 110, 170, 60);
if(GUILayout.Button( text ))
return !toggle;
else
if (toggle==true)
windowRect = new Rect (20, 110, 170, 60);
return toggle;
}
An example of what happens if I don't put in the "windowRect = new Rect (20, 110, 170, 60);" in the ToggleButton section is attached. With the code, "Floorplan Navigation" rect along with the buttons inside it flicker everytime I use left or right mouse buttons. What it looks like is happening is the rect goes really small vertically the resets itself according to the new Rect code I put in. Any help would be appreciated. The whole code is below:
using UnityEngine;
using System.Collections;
public class ControlGUI : MonoBehaviour
{
private Rect windowRect = new Rect (20, 110, 170, 50);
public GameObject[] cameras;
private string[] camNames;
public string[] sceneNames;
public Texture[] textures;
public Waypoint[] waypoints;
private string[] wayNames;
private string[] texNames;
// Use this for initialization
void Start ()
{
camNames = new string[cameras.Length];
texNames = new string[textures.Length];
wayNames = new string[waypoints.Length];
for(int i = 0; i < cameras.Length; i++)
camNames[i] = cameras[i].name;
for(int i = 0; i < textures.Length; i++)
texNames[i] = textures[i].name;
for(int i = 0; i < waypoints.Length; i++)
wayNames[i] = waypoints[i].name;
}
// Update is called once per frame
//void Update ()
// {
// }
void OnGUI(){
windowRect = GUILayout.Window (0, windowRect, DoControls, "Quick Walk Through");
// The window can be dragged around by the users - make sure that it doesn't go offscreen.
//windowRect.x = Mathf.Clamp (windowRect.x, 0.0f, Screen.width - windowRect.width);
//windowRect.y = Mathf.Clamp (windowRect.y, 0.0f, Screen.height - windowRect.height);
}
bool switchScene;
bool switchCam;
bool switchWaypoint;
//bool toggleQuality;
void DoControls(int windowID)
{
//GUI.DragWindow (new Rect (0,0, float.MaxValue, 20));
if(cameras.Length > 0){
switchCam = ToggleButton(switchCam, "2D Floorplan Layout");
if(switchCam)
cameraSwitchControls();
}
if(waypoints.Length > 0)
{
switchWaypoint = ToggleButton(switchWaypoint, "Room Views");
if(switchWaypoint)
waypointSwitchControls();
}
if(sceneNames.Length > 0){
switchScene = ToggleButton(switchScene, "Other Floor Plans");
if(switchScene)
sceneSwitchControls();
}
//toggleQuality = ToggleButton(toggleQuality, "Toggle Quality: " + QualitySettings.currentLevel.ToString());
//if(toggleQuality)
//{
//i//f(GUILayout.Button("Increase Quality"))
//QualitySettings.IncreaseLevel();
//if(GUILayout.Button("Decrease Quality"))
//QualitySettings.DecreaseLevel();
}
//}
bool ToggleButton( bool toggle, string text )
{
if (toggle==false)
windowRect = new Rect (20, 110, 170, 60);
if(GUILayout.Button( text ))
return !toggle;
else
if (toggle==true)
windowRect = new Rect (20, 110, 170, 60);
return toggle;
}
int sceneSelect = -1;
public float sceneButtonHeight = 0;
public float sceneButtonWidth = 0;
void sceneSwitchControls ()
{
sceneSelect = GUILayout.SelectionGrid(sceneSelect, sceneNames, 1, GUILayout.Height(sceneButtonHeight), GUILayout.Width(sceneButtonWidth));
if(sceneSelect != -1){
Application.LoadLevel(sceneNames[sceneSelect]);
}
}
int camSelect = -1;
public float cameraButtonHeight = 0;
public float cameraButtonWidth = 0;
void cameraSwitchControls(){
camSelect = GUILayout.SelectionGrid(camSelect, camNames, 1, GUILayout.Height(cameraButtonHeight), GUILayout.Width(cameraButtonWidth));
if(camSelect != -1){
for(int i = 0; i < cameras.Length; i++)
{
if(i == camSelect){
cameras[camSelect].gameObject.active = true;
cameras[camSelect].tag = "MainCamera";
}
else{
cameras[i].gameObject.active = false;
cameras[i].tag = "Untagged";
}
}
camSelect = -1;
}
}
int waySelect = -1;
public float waypointButtonHeight = 0;
public float waypointButtonWidth = 0;
void waypointSwitchControls ()
{
waySelect = GUILayout.SelectionGrid(waySelect, wayNames, 1, GUILayout.Height(waypointButtonHeight), GUILayout.Width(waypointButtonWidth));
if(waySelect != -1)
GameObject.FindWithTag("Player").transform.position = waypoints[waySelect].Position;
waySelect = -1;
}
}
[1]: /storage/temp/6478-\menu.jpg
Answer by CodeMasterMike · Jan 08, 2013 at 10:00 AM
It's kind of a messy code, so its hard to see whats going on. But I think that the problem is that your toggleButton script switches between true/false every GUI update.
Try something like this instead:
bool ToggleButton( bool toggle, string text )
{
// If the button has been pressed
if(GUILayout.Button( text ) == true)
{
if(toggle == true)
{
toggle = false;
}
else
{
toggle = true;
}
}
if (toggle == true)
{
// expand
}
else
{
// Collapse
}
return toggle;
}
This example, it switches the toggle variable, if the button was clicked. If the button wasn't clicked, it returns the same boolean value as it had when it was coming in (meaning it hasn't changed).
Good luck!
Your answer
Follow this Question
Related Questions
Why is only my last for-loop-button triggering? 1 Answer
Do Something ONLY when all Toggles are On or Off 4 Answers
No Buttons Click when Play Exported WebGL version of the Game 0 Answers
Centering Radial Menu 0 Answers
If using a conditional-situational menu, such as a pop-up, must wait till next OnGUI? 2 Answers