Search Unity

OnGUI buttons not updating in Edit Mode

Discussion in 'Immediate Mode GUI (IMGUI)' started by RyFridge, Aug 27, 2016.

  1. RyFridge

    RyFridge

    Joined:
    Apr 7, 2014
    Posts:
    52
    Hello folks.

    I'm seeing a very wierd behaviour on the GUI.buttons in Unity (tested in 5.0, 5.1, 5.4).
    Whenever I have a script that has an OnGUI function creating GUI.buttons, they are visible like they should be, in the Game View. The problem is that they only "work" (something happens if I click them) if I am in Play Mode (or in some cases if I have a GameObject selected that has the script attached, so it is visible in the Inspector).

    Is this by design? If so, why?

    Can you reproduce the bug with this simple script?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class ButtonTest : MonoBehaviour {
    6.     void OnGUI(){
    7.         if (GUI.Button (new Rect (0f, 0f, 50f, 50f), "button")) {
    8.             Debug.Log ("I am doing something!");
    9.         }
    10.     }
    11. }
    12.  
    Thanks!
     

    Attached Files:

    Last edited: Aug 28, 2016
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Hey, this is expected behavior (though understandably not obvious, nor documented). When in edit mode (or paused play mode) the GameView will intercept mouse events and consume them for game view manipulation. The intent for ExecuteInEditMode is more for previewing things live, not for creating interactivity in the GameView.

    I've noted that this isn't obvious behevior. I'll see about updating the documentation.
     
  3. RyFridge

    RyFridge

    Joined:
    Apr 7, 2014
    Posts:
    52
    Hi shawn,
    thanks a lot for clearing this up!

    One more question, if you don't mind:
    The click events seem to be stored even if they are intercepted, because when I click on the buttons (nothing happens like you stated) and then select a GameObject in the Hierarchy that has a Component of the same type that shows the buttons (not necessarily the same instance), it will then fire all the button clicks from before in an instant, as if the Game View would be updated just then. (Also Debug.Log of the OnGUI events will be called several times then.)

    I wonder if there could be a global OnGUI fire event in Edit Mode for updating this kind of stuff?

    Thanks in any way! I disabled this feature in my editor extension for now.
     
  4. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    Hmm... that sounds like a bug. Could you submit a bug report describing what you're seeing?
     
  5. RyFridge

    RyFridge

    Joined:
    Apr 7, 2014
    Posts:
    52
    Hi shawn,
    I can not reproduce the bug with this simple script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class ButtonNotWorking : MonoBehaviour {
    6.  
    7.     void OnGUI(){
    8.         Debug.Log (Event.current.ToString ());
    9.         if (GUI.Button (new Rect (0f, 0f, 50f, 50f), "Button")) {
    10.             Debug.Log ("Button pressed");
    11.         }
    12.     }
    13. }
    It is just like you stated: In Edit Mode, the button is shown but cannot be pressed. In Play Mode, everything works. Anyhow, is there a reason why the button presses are not fired in Edit Mode? I see the "button pressed animation" when I click it.
     
  6. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    you wanna be just like kissUI? ;)

    ex:


    just a hint, not sharing more:
    Code (csharp):
    1.  
    2.     static EditorWindow gameView = null;
    3.  
    4.     public static void GameView_Repaint()
    5.     {
    6.         if( gameView == null )
    7.         {
    8.             System.Reflection.Assembly assembly = typeof( UnityEditor.EditorWindow ).Assembly;
    9.             System.Type type = assembly.GetType( "UnityEditor.GameView" );
    10.             gameView = EditorWindow.GetWindow( type );
    11.         }
    12.  
    13.         if( gameView != null )
    14.             gameView.Repaint();
    15.     }
    16.  
     
  7. RyFridge

    RyFridge

    Joined:
    Apr 7, 2014
    Posts:
    52
    Hey Izzysoft,
    very impressive idea, I can see what it does, but it still won't work for me. The Repaint is fired (Debug in console), but the OnGUI is still not working. Is there a way to fire OnGUI manually if any button is pressed, or just periodically?

    In Edit Mode from ExecuteInEditMode script:
    The OnGUI buttons save the incoming button presses and fire them all at once when I hit Play.

    The documentation states:
    - OnGUI is called when the Game View recieves an Event.

    So that means OnGUI is not receiving a Repaint event even though I pressed a button in a script (in Edit Mode from ExecuteInEditMode script).
     
    Last edited: Sep 1, 2016
    tylerinthezoo likes this.