Search Unity

Editor GUI - Force window to refresh the displayed value?

Discussion in 'Immediate Mode GUI (IMGUI)' started by KyleStaves, May 16, 2011.

  1. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    So I'm having an odd issue while writing an editor script. I have a lot of EditorGUILayout.FloatField/StringField/IntField... being displayed. The values in these fields are cleared by another portion of the script in certain situations.

    Code (csharp):
    1.  
    2. conFloat.Value = EditorGUILayout.FloatField(conFloat.Value);
    3.  
    Lets say this hypothetically (where conFloat.Value is a float). If I edit conFloat.Value from another source it properly edits the value, but the GUI does not update. Even if I close/re-open the window the value is not updated.

    Is there any way to force these fields to update/clear?

    Example scenario:
    I have a string set up just like that float which is used to create new objects. You type the name in and click the button next to it and it does what it needs to do. Clicking the button clears the string automatically (which properly clears the variable), but the GUI does not clear with it.

    Any thoughts on how to achieve this?
     
  2. JAHansens

    JAHansens

    Joined:
    Aug 21, 2010
    Posts:
    7
    If this is anything like what i experienced recently the values wouldn't update until I clicked back in the window.
    Try Repaint() when you change the values.
     
    AbgaryanFX likes this.
  3. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    Thanks! For the most part it's working great.
     
  4. MaliceA4Thought

    MaliceA4Thought

    Joined:
    Feb 25, 2011
    Posts:
    406
    Kyle, greetings.. did you ever get a full solution to this?

    I am hitiing the same problem and its always (in my case) the last text, int or whatever field on editor GUI that's holding it's value until the form is clicked again even if other types of GUI field follow it that do update correctly..

    What's really frustrating is that debug.log entries show the field as empty, but the onGUI routines are not properly populating the field.

    I have tried pretty much everything, and also posted it in scripting section, but no-ones come back from there with an answer. I also tried playing with the focus to try and force a refresh and also with "repaint" but to no effect. Always the last one doesn't update even though the field is not saying what the variable associated with it has as a value..

    I am thinking currently, about putting a dummy hidden text field at the end of the form, as it always moves to the last one not being updated but haven't tried this yet so not sure if a hidden field would be treated as the last one on a form.

    Regards

    Graham
     
  5. CSEliot

    CSEliot

    Joined:
    May 9, 2014
    Posts:
    33
    For Those of you struggling with this, use inheritance and call your Runtime-Space Script from your Editor-Space script like this:
    Code (CSharp):
    1. public class MyGameObject : MonoBehaviour {
    2.  
    3.   public static EditorWindow MyEditorWindow;
    4.   private GameObject _previouslySelectedGameObject;
    5.  
    6.   protected void Update () {
    7.     if( selection.activeGameObject !=_previouslySelectedGameObject) {
    8.       _previouslySelectedGameObject = Selection.activeGameObject;
    9.       MyEditorWindow.Repaint();
    10.       return;
    11.     }
    12. }
    13.  
    14. public class MyEditorWindow : EditorWindow
    15. {
    16.   public static void ShowWindow()
    17.   {
    18.       MyEditorWindow window = GetWindow<MyEditorWindow>("My Window");
    19.     MyGameObject.MyEditorWindow = window;
    20.     window.minSize = new Vector2(200, 300);
    21.     window.Focus();
    22.   }
    23. }
    My specific use case included the fact that I have MonoBehaviour code running at editor-time with the [ExecuteAlways] tag.
     
    kyle_lam likes this.