psydack/uimgui

[BUG] ImGui window shows up even when _isOpen is false

Opened this issue · 1 comments

Describe the bug
Hello,

I’m experiencing an issue with the integration of ImGui in my Unity project. I have a script using UImGui, and even when I set the p_open variable to false, the window still appears. And when i click the top right cross for closing the windows, the windows stay open (the windows blink ).

To Reproduce
Steps to reproduce the behavior:

  1. Create a new scene
  2. Add SampleDemoWindow
  3. Attach linked script to SampleDemoWindow
  4. Setup UImGui script (Render feature, camera )
  5. Play
  6. The window should be close but it appears

Version/Branch of UImGui:
Version: 5.0.0

Unity Version
2022.3.19f1

Render pipeline (HDRP / URP / Built-in)
URP

Expected behavior
The window should not be displayed when p_open is set to false.

Standalone, minimal, complete and verifiable example:

using UnityEngine;
using ImGuiNET;
public class TestUImGUI : MonoBehaviour
{
	[SerializeField]
	private UImGui.UImGui _uimGuiInstance;
    private bool _isOpen = false;

	private void Awake()
	{
		if (_uimGuiInstance == null)
		{
			Debug.LogError("Must assign a UImGuiInstance or use UImGuiUtility with Do Global Events on UImGui component.");
		}

		_uimGuiInstance.Layout += OnLayout;
	}

    private void OnLayout(UImGui.UImGui obj)
    {

        _isOpen = false;
        if (!ImGui.Begin("Test Window", ref _isOpen))
        {
            ImGui.End();
            return;
        }

        ImGui.Text("Hello, World!");
        ImGui.End();
    }

	private void OnDisable()
	{
		_uimGuiInstance.Layout -= OnLayout;
	}
}

I believe this may just be an incorrect example in the repo. Looking at the official example from the ImGui.NET library used by uimgui, ImGui.Begin will always return true if the command executed successfully, and then assign closed status to the _isOpen bool which needs to be checked separately.

https://github.com/ImGuiNET/ImGui.NET/blob/master/src/ImGui.NET.SampleProgram/Program.cs#L116-L124

            if (_showAnotherWindow)
            {
                ImGui.Begin("Another Window", ref _showAnotherWindow);
                ImGui.Text("Hello from another window!");
                if (ImGui.Button("Close Me"))
                    _showAnotherWindow = false;
                ImGui.End();
            }