Search Unity

onGUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by Alex0621, Jul 13, 2016.

  1. Alex0621

    Alex0621

    Joined:
    Sep 2, 2013
    Posts:
    4
    Hi all, Why the block of my script not working in OnGUI method? :confused:

    1.png
     
  2. IzzySofty

    IzzySofty

    Joined:
    Jul 4, 2016
    Posts:
    19
    OnGUI() gets called more than once in a single frame.

    I always used a class variable to store the GUI.Button() result, and used that in my conditions.

    ex:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MyOnGUI : MonoBehaviour
    6. {
    7.     bool showDialog_AppQuit = false;
    8.     bool showDialog_AppQuit_Yes = false;
    9.     bool showDialog_AppQuit_No = false;
    10.    
    11.     void OnGUI( )
    12.     {
    13.         if( showDialog_AppQuit == false ) {
    14.             showDialog_AppQuit = GUI.Button( new Rect(10,10,120,20), "Quit Game!" );
    15.         } else {
    16.             GUI.Label( new Rect(10,10,180,20), "Do you really what to Quit?" );
    17.  
    18.             showDialog_AppQuit_Yes = GUI.Button( new Rect(35,30,45,20), "Yes" );
    19.             showDialog_AppQuit_No = GUI.Button( new Rect(85,30,45,20), "No" );
    20.  
    21.             if( showDialog_AppQuit_Yes ) {
    22.                 if( Application.isEditor )
    23.                     UnityEditor.EditorApplication.isPlaying = false;
    24.                 else
    25.                     Application.Quit();
    26.             }
    27.  
    28.             if( showDialog_AppQuit_No )
    29.                 showDialog_AppQuit = false;
    30.         }
    31.     }
    32. }
    33.  
     
    Alex0621 likes this.
  3. Alex0621

    Alex0621

    Joined:
    Sep 2, 2013
    Posts:
    4
    Thanks a lot, man :)
     
    IzzySoft likes this.