Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Shawnmac63 · Jan 08, 2013 at 01:51 AM · menubuttonstoggle

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

\menu.jpg (39.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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!

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

9 People are following this question.

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

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


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