Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

if (GuiLayout.Button()) repeatedly called from custom Editor

Discussion in 'Editor & General Support' started by thebarryman, Oct 20, 2021.

  1. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    122
    Hello,

    I'm experiencing a bug with the following pseudocode in a custom editor window

    void OnGUI()
    {
    if (GUILayout.Button("Do Something That Takes 10 minutes"))
    {
    DoSomethingThatTakes10Minutes();
    EditorUtility.DisplayDialog("Result", "result text", "OK");
    }
    }

    The issue is that as soon as "something that takes 10 minutes" is complete and I click OK from the resulting pop up box, the whole function will run all over again, as if I pressed the button again (even though I didnt click anywhere near it).

    Sometimes it only runs once, sometimes it runs multiple times. As you could imagine, I don't really want to run "something that takes 10 minutes" more than is absolutely necessary so any suggestions for how to fix this would be much appreciated!
     
  2. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    122
    Bump - anybody else have experience with this or have a fix for it? It's popping up again for a new feature and is very frustrating.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,843
    Are you doing this long operation synchronously? I would imagine holding up execution of the UI for a while might cause a few issues. It almost sounds like, if it's taking so long, it isn't clearing inputs for the following editor loop updates.

    I would look at running the process over a number of frames with perhaps editor coroutines.
     
  4. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    122
    I've seen this error with purely synchronous operations previously, but this latest issue was happening with a heavy parallelized load using Parallel.For. The function being invoked by the Editor is still executing synchronously overall, though. I didn't know about the Editor Coroutines package, I'll take a look - thanks for the tip!