lime雑記

ゲーム開発、その他雑記。

【Unity】Dear ImGuiのウィンドウ実装方法

この記事でのバージョン

Unity 2021.3.8f1
UImGui 4.1.1

概要

Dear ImGuiにおける、ウィンドウの実装方法を解説。
Dear ImGui自体の紹介や導入方法は以下の記事を参照。
limegame.hatenablog.com

ウィンドウとは


様々なパーツを載せる基本となるもの。

実装方法

コード
public class ImGuiManager : MonoBehaviour {
  private void OnEnable() {
    UImGuiUtility.Layout += OnLayout;
  }

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

  private void OnLayout(UImGui.UImGui uImGui) {
    UpdateWindow();
  }

  private bool _isOpen;

  private void UpdateWindow() {
    ImGuiNET.ImGui.Begin("Window", ref _isOpen);
    // ここにパーツを載せていく
    ImGuiNET.ImGui.End();
  }
}

Begin("Window", ref _isOpen)でウィンドウの表示開始。
End()で表示終了。
毎フレームBeginとEndを呼ぶことでウィンドウが表示され、その間に書いたパーツがウィンドウ内に表示される。

表示例
private bool _isOpen;
private int _int;

private void UpdateWindow() {
  ImGuiNET.ImGui.Begin("Window", ref _isOpen);

  ImGuiNET.ImGui.InputInt("Int", ref _int);
  if (ImGuiNET.ImGui.Button("Button")) {
    Debug.Log(_int);
  }

  ImGuiNET.ImGui.End();
}