Search Unity

Passive GUI Controls Idea

Discussion in 'Immediate Mode GUI (IMGUI)' started by Marc, Jul 14, 2008.

  1. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    I have this game menu which is made up of gui elements such as button, selection grid, toggles etc. This is all great.

    I have a global variable representing pause, lets call it gamePaused, which everything can check up against to see if the game is paused and decide what to do.

    The problem is that i would like to do a form of popup exit menu that become visible on ESC, pauses the game and only lets the user interact with components in the esc menu and not any other GUI components, but those should still be rendered.

    I initially thought i could just check on all action states such as buttons if the game was paused and not do anything.

    Code (csharp):
    1.  
    2. if( GUI.Button(..., "myStyle") )
    3. {
    4.    if(!gamePaused) OnClick();
    5. }
    6.  

    This work to make the gui passive in that it wont activate anything. BUT its not passive enough all the hover effects and such are still fully functional.

    I then though i could just render a passive control using Box and still have the menu look like it should.

    Code (csharp):
    1.  
    2. if(gamePaused)
    3. {
    4.   if( GUI.Button(..., "myStyle") )
    5.   {
    6.      OnClick();
    7.   }
    8. }
    9. else
    10. {
    11.   GUI.Box(..., "myStyle");
    12. }
    13.  
    This works for some components, but not for toggles. Here i will have to detect the state of the toggle and extract the correct style image to use.

    Code (csharp):
    1.  
    2. if(gamePaused)
    3. {
    4.    GUIStyle toggle = GUI.skin.GetStyle("toggle");
    5.    GUI.Box(new GUIContent(toggle.onNormal.background), ..., toggle);
    6. }
    7. else
    8. {
    9.      GUIStyle toggle = GUI.skin.GetStyle("toggle");
    10.    GUI.Box(new GUIContent(toggle.normal.background), ..., toggle);
    11. }
    12.  
    This can get even more complicated, but thats not the point the point is it clutters the code up with a lot of extracting the right data based on state.

    It would be really nice and simplify things to just be able to write something like

    Code (csharp):
    1.  
    2. GUI.PassiveToggle(...);
    3.  
    So basically to create the effect i want i could write:

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.  
    5.   GUI.skin = mySkin;
    6.  
    7.   ... some shared code for both paused an non paused
    8.  
    9.   if(!gamePaused)
    10.   {
    11.      value = GUILayout.Toggle(value, ...);
    12.   }
    13.   else
    14.   {
    15.      GUILayout.PassiveToggle(value, ...);
    16.   }
    17.  
    18. }
    19.  

    So a passive version of a gui component use the same information as the active ones, just that passive ones are only renders of the components state without the interactivity.

    Is there any way to do this simple or do i have to wrap all components? Is it even a good idea, maybe im doing things wrong?

    Comments are welcome.

    Regards,
    Marc
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Maybe you want to play a bit around with GUI.enabled? Try setting it to false, and see what happens :)

    (yes, you can do this for individual controls)

    Code (csharp):
    1.  
    2. GUI.enabled = false;
    3. GUILayout.Button ("You can't click me! HA!");
    4. GUI.enabled = true;
    5. GUILayout.Button ("But you can click me");
    6.  
     
  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Haha :oops: even better. Thanks!

    Somehow GUI.enabled create a conflict in my mind thinking it would disable the entire GUI and maybe create a blackhole in the process!

    Edit: LOL it even creates a greyish transparent effect. Less work for me to do! I love this GUI system.

    Also replace all the tangled code i derived my idea from by one line at the top of the code

    Code (csharp):
    1.  
    2. GUI.enabled = !gamePaused;
    3.  
    LOL :oops:
     
  4. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Lets say my pitch to boss that the transparency effect is cool fails. Is there a way to change that just as easy?