├── DearImGuiDemoWindow.cs ├── OLD_README.md ├── README.md └── static └── img └── Banner.png /DearImGuiDemoWindow.cs: -------------------------------------------------------------------------------- 1 | using ImGuiNET; 2 | using UnityEngine; 3 | using UnityEngine.Events; 4 | 5 | public class DearImGuiDemoWindow : MonoBehaviour 6 | { 7 | private static UnityAction OnUpdateLighting; 8 | private static UnityAction OnLoadGame; 9 | private static UnityAction OnSaveGame; 10 | 11 | private void OnEnable() => ImGuiUn.Layout += OnLayout; 12 | 13 | private void OnDisable() => ImGuiUn.Layout -= OnLayout; 14 | 15 | private static readonly Vector4 TEXT_COLOR = new(1f, 0.8f, 0f, 1.0f); 16 | 17 | private float m_Timeline = 0.5f; 18 | private System.TimeSpan m_Time = System.TimeSpan.FromHours(12.0f); 19 | private string m_Username = string.Empty; 20 | private string m_Password = string.Empty; 21 | private Vector3 m_SunColor = new(1.0f, 0.9f, 0f); 22 | private bool m_WindowEnabled = false; 23 | private bool m_EnableDayAndNightCycle = true; 24 | private int m_DragInt = 0; 25 | private int m_AccountCount = 0; 26 | private bool m_ShowImGuiDemoWindow; 27 | private static uint s_tab_bar_flags = (uint)ImGuiTabBarFlags.Reorderable; 28 | static bool[] s_opened = { true, true, true, true }; // Persistent user state 29 | 30 | /** 31 | * DearImGui 32 | * Manual - https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html 33 | */ 34 | 35 | 36 | private void OnLayout() 37 | { 38 | // Begins ImGui window 39 | if (!ImGui.Begin("Game Manager", 40 | ref m_WindowEnabled, 41 | ImGuiWindowFlags.MenuBar)) 42 | return; 43 | 44 | ImGui.Checkbox("Enable Day And Night Cycle", ref m_EnableDayAndNightCycle); 45 | 46 | // Make a float slider (label, value, min, max) 47 | if (ImGui.SliderFloat("Time [%]", ref m_Timeline, 0f, 1f) && m_EnableDayAndNightCycle) 48 | { 49 | m_Time = System.TimeSpan.FromSeconds(m_Timeline * 86400f); 50 | OnUpdateLighting?.Invoke(); 51 | } 52 | 53 | // Display text of current in-game time 54 | ImGui.TextColored(TEXT_COLOR, $"In-game Time: {m_Time:hh\\:mm}"); 55 | 56 | // Create color editor for Vector3 57 | ImGui.ColorEdit3("Sun Color", ref m_SunColor); 58 | 59 | // Creates a menu bar 60 | if (ImGui.BeginMenuBar()) 61 | { 62 | if (ImGui.BeginMenu("File")) 63 | { 64 | if (ImGui.MenuItem("Open", shortcut: "Ctrl+O")) 65 | Debug.Log("Opening a file..."); 66 | 67 | if (ImGui.MenuItem("Save", shortcut: "Ctrl+S")) 68 | Debug.Log("Saving a file..."); 69 | 70 | if (ImGui.MenuItem("Close", shortcut: "Ctrl+W")) 71 | { 72 | m_WindowEnabled = false; 73 | Debug.Log("Closing this window..."); 74 | } 75 | 76 | ImGui.EndMenu(); 77 | } 78 | 79 | ImGui.EndMenuBar(); 80 | } 81 | 82 | if (ImGui.Button("Load Game")) 83 | { 84 | OnLoadGame?.Invoke(); 85 | Debug.Log("Loading the game..."); 86 | } 87 | 88 | if (ImGui.Button("Save Game")) 89 | { 90 | OnSaveGame?.Invoke(); 91 | Debug.Log("Saving the game..."); 92 | } 93 | 94 | if (ImGui.Button("Create Account")) 95 | { 96 | Debug.Log("Creating account..."); 97 | m_AccountCount++; 98 | } 99 | 100 | ImGui.SameLine(0, -1); 101 | ImGui.Text($"Account Count = {m_AccountCount}"); 102 | 103 | // Create input field (label, value, maxLength [uint]) 104 | ImGui.InputText("Username", ref m_Username, maxLength: 12u); 105 | ImGui.InputText("Password", ref m_Password, maxLength: 16u); 106 | 107 | ImGui.Text($"Mouse position: {ImGui.GetMousePos()}"); 108 | 109 | // Display contents in a scrolling region 110 | ImGui.TextColored(TEXT_COLOR, "Important Stuff"); 111 | ImGui.BeginChild("Scrolling"); 112 | 113 | for (int n = 0; n < 50; n++) 114 | ImGui.Text($"{n:0000}: Some text"); 115 | 116 | ImGui.EndChild(); 117 | 118 | // Generate samples and plot them 119 | float[] samples = new float[100]; 120 | 121 | for (int n = 0; n < 100; n++) 122 | samples[n] = Mathf.Sin((float)(n * 0.2f + ImGui.GetTime() * 1.5f)); 123 | 124 | ImGui.PlotLines("Samples", ref samples[0], 100); 125 | 126 | ImGui.DragInt("Draggable Int", ref m_DragInt); 127 | 128 | float framerate = ImGui.GetIO().Framerate; 129 | ImGui.Text($"Application average {1000.0f / framerate:0.##} ms/frame ({framerate:0.#} FPS)"); 130 | 131 | if (m_ShowImGuiDemoWindow) 132 | { 133 | // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. 134 | // Here we just want to make the demo initial state a bit more friendly! 135 | ImGui.SetNextWindowPos(new Vector2(650, 20), ImGuiCond.FirstUseEver); 136 | ImGui.ShowDemoWindow(ref m_ShowImGuiDemoWindow); 137 | } 138 | 139 | if (ImGui.TreeNode("Tabs")) 140 | { 141 | if (ImGui.TreeNode("Basic")) 142 | { 143 | ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags.None; 144 | 145 | if (ImGui.BeginTabBar("MyTabBar", tab_bar_flags)) 146 | { 147 | if (ImGui.BeginTabItem("Avocado")) 148 | { 149 | ImGui.Text("This is the Avocado tab!\nblah blah blah blah blah"); 150 | ImGui.EndTabItem(); 151 | } 152 | 153 | if (ImGui.BeginTabItem("Broccoli")) 154 | { 155 | ImGui.Text("This is the Broccoli tab!\nblah blah blah blah blah"); 156 | ImGui.EndTabItem(); 157 | } 158 | 159 | if (ImGui.BeginTabItem("Cucumber")) 160 | { 161 | ImGui.Text("This is the Cucumber tab!\nblah blah blah blah blah"); 162 | ImGui.EndTabItem(); 163 | } 164 | 165 | ImGui.EndTabBar(); 166 | } 167 | 168 | ImGui.Separator(); 169 | ImGui.TreePop(); 170 | } 171 | 172 | if (ImGui.TreeNode("Advanced & Close Button")) 173 | { 174 | // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). 175 | ImGui.CheckboxFlags("ImGuiTabBarFlags_Reorderable", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.Reorderable); 176 | ImGui.CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.AutoSelectNewTabs); 177 | ImGui.CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.NoCloseWithMiddleMouseButton); 178 | if ((s_tab_bar_flags & (uint)ImGuiTabBarFlags.FittingPolicyMask) == 0) 179 | s_tab_bar_flags |= (uint)ImGuiTabBarFlags.FittingPolicyDefault; 180 | if (ImGui.CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.FittingPolicyResizeDown)) 181 | s_tab_bar_flags &= ~((uint)ImGuiTabBarFlags.FittingPolicyMask ^ (uint)ImGuiTabBarFlags.FittingPolicyResizeDown); 182 | if (ImGui.CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.FittingPolicyScroll)) 183 | s_tab_bar_flags &= ~((uint)ImGuiTabBarFlags.FittingPolicyMask ^ (uint)ImGuiTabBarFlags.FittingPolicyScroll); 184 | 185 | // Tab Bar 186 | string[] names = { "Artichoke", "Beetroot", "Celery", "Daikon" }; 187 | 188 | for (int n = 0; n < s_opened.Length; n++) 189 | { 190 | if (n > 0) { ImGui.SameLine(); } 191 | ImGui.Checkbox(names[n], ref s_opened[n]); 192 | } 193 | 194 | // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): the underlying bool will be set to false when the tab is closed. 195 | if (ImGui.BeginTabBar("MyTabBar", (ImGuiTabBarFlags)s_tab_bar_flags)) 196 | { 197 | for (int n = 0; n < s_opened.Length; n++) 198 | { 199 | if (s_opened[n] && ImGui.BeginTabItem(names[n], ref s_opened[n])) 200 | { 201 | ImGui.Text($"This is the {names[n]} tab!"); 202 | if ((n & 1) != 0) 203 | ImGui.Text("I am an odd tab."); 204 | ImGui.EndTabItem(); 205 | } 206 | } 207 | 208 | ImGui.EndTabBar(); 209 | } 210 | 211 | ImGui.Separator(); 212 | ImGui.TreePop(); 213 | } 214 | 215 | ImGui.TreePop(); 216 | } 217 | 218 | // Ends ImGui window 219 | ImGui.End(); 220 | } 221 | } -------------------------------------------------------------------------------- /OLD_README.md: -------------------------------------------------------------------------------- 1 | # Guide-UnityDearImGui 2 | 3 | Code for using Dear ImGui solution with Unity 4 | 5 | # Repos 6 | 7 | [realgamessoftware - Original Repo](https://github.com/realgamessoftware/dear-imgui-unity)
8 | [mattmanj17 - Forked Repo](https://github.com/mattmanj17/dear-imgui-unity#matthewd) 9 | 10 | Forked repo fixes an issue from Unity version 2020.1.0b4: https://github.com/realgamessoftware/dear-imgui-unity/issues/28 11 | 12 | # How To Install 13 | 14 | * [Add package](https://docs.unity3d.com/Manual/upm-ui-giturl.html) from this git URL: https://github.com/mattmanj17/dear-imgui-unity.git#matthewd 15 | * Add a DearImGui component to one of the objects in the scene. 16 | * Create 'Ini Settings Asset' and 'Font Atlas Config Asset' in the project folder. 17 | * Add custom fonts to the '[StreamingAssets](https://docs.unity3d.com/Manual/StreamingAssets.html)' folder and link it up to the 'Font Atlas Config Asset'. 18 | * Change the 'Font pixel' in the 'Font Atlas Config Asset', to scale up the font size. 19 | * When using the Universal Render Pipeline, add a Render Im Gui Feature render feature to the renderer asset. Assign it to the render feature field of the DearImGui component. 20 | * Subscribe to the ImGuiUn.Layout event and use ImGui functions. 21 | * Example script: 22 | 23 | ```.c# 24 | using UnityEngine; 25 | using ImGuiNET; 26 | 27 | public class DearImGuiDemo : MonoBehaviour 28 | { 29 | void OnEnable() 30 | { 31 | ImGuiUn.Layout += OnLayout; 32 | } 33 | 34 | void OnDisable() 35 | { 36 | ImGuiUn.Layout -= OnLayout; 37 | } 38 | 39 | void OnLayout() 40 | { 41 | ImGui.ShowDemoWindow(); 42 | } 43 | } 44 | ``` 45 | 46 | # Advanced Script 47 | 48 | ```.c# 49 | using ImGuiNET; 50 | using UnityEngine; 51 | using UnityEngine.Events; 52 | 53 | public class DearImGuiDemoWindow : MonoBehaviour 54 | { 55 | private static UnityAction OnUpdateLighting; 56 | private static UnityAction OnLoadGame; 57 | private static UnityAction OnSaveGame; 58 | 59 | private void OnEnable() => ImGuiUn.Layout += OnLayout; 60 | 61 | private void OnDisable() => ImGuiUn.Layout -= OnLayout; 62 | 63 | private static readonly Vector4 TEXT_COLOR = new(1f, 0.8f, 0f, 1.0f); 64 | 65 | private float m_Timeline = 0.5f; 66 | private System.TimeSpan m_Time = System.TimeSpan.FromHours(12.0f); 67 | private string m_Username = string.Empty; 68 | private string m_Password = string.Empty; 69 | private Vector3 m_SunColor = new(1.0f, 0.9f, 0f); 70 | private bool m_WindowEnabled = false; 71 | private bool m_EnableDayAndNightCycle = true; 72 | private int m_DragInt = 0; 73 | private int m_AccountCount = 0; 74 | private bool m_ShowImGuiDemoWindow; 75 | private static uint s_tab_bar_flags = (uint)ImGuiTabBarFlags.Reorderable; 76 | static bool[] s_opened = { true, true, true, true }; // Persistent user state 77 | 78 | /** 79 | * DearImGui 80 | * Manual - https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html 81 | */ 82 | 83 | 84 | private void OnLayout() 85 | { 86 | // Begins ImGui window 87 | if (!ImGui.Begin("Game Manager", 88 | ref m_WindowEnabled, 89 | ImGuiWindowFlags.MenuBar)) 90 | return; 91 | 92 | ImGui.Checkbox("Enable Day And Night Cycle", ref m_EnableDayAndNightCycle); 93 | 94 | // Make a float slider (label, value, min, max) 95 | if (ImGui.SliderFloat("Time [%]", ref m_Timeline, 0f, 1f) && m_EnableDayAndNightCycle) 96 | { 97 | m_Time = System.TimeSpan.FromSeconds(m_Timeline * 86400f); 98 | OnUpdateLighting?.Invoke(); 99 | } 100 | 101 | // Display text of current in-game time 102 | ImGui.TextColored(TEXT_COLOR, $"In-game Time: {m_Time:hh\\:mm}"); 103 | 104 | // Create color editor for Vector3 105 | ImGui.ColorEdit3("Sun Color", ref m_SunColor); 106 | 107 | // Creates a menu bar 108 | if (ImGui.BeginMenuBar()) 109 | { 110 | if (ImGui.BeginMenu("File")) 111 | { 112 | if (ImGui.MenuItem("Open", shortcut: "Ctrl+O")) 113 | Debug.Log("Opening a file..."); 114 | 115 | if (ImGui.MenuItem("Save", shortcut: "Ctrl+S")) 116 | Debug.Log("Saving a file..."); 117 | 118 | if (ImGui.MenuItem("Close", shortcut: "Ctrl+W")) 119 | { 120 | m_WindowEnabled = false; 121 | Debug.Log("Closing this window..."); 122 | } 123 | 124 | ImGui.EndMenu(); 125 | } 126 | 127 | ImGui.EndMenuBar(); 128 | } 129 | 130 | if (ImGui.Button("Load Game")) 131 | { 132 | OnLoadGame?.Invoke(); 133 | Debug.Log("Loading the game..."); 134 | } 135 | 136 | if (ImGui.Button("Save Game")) 137 | { 138 | OnSaveGame?.Invoke(); 139 | Debug.Log("Saving the game..."); 140 | } 141 | 142 | if (ImGui.Button("Create Account")) 143 | { 144 | Debug.Log("Creating account..."); 145 | m_AccountCount++; 146 | } 147 | 148 | ImGui.SameLine(0, -1); 149 | ImGui.Text($"Account Count = {m_AccountCount}"); 150 | 151 | // Create input field (label, value, maxLength [uint]) 152 | ImGui.InputText("Username", ref m_Username, maxLength: 12u); 153 | ImGui.InputText("Password", ref m_Password, maxLength: 16u); 154 | 155 | ImGui.Text($"Mouse position: {ImGui.GetMousePos()}"); 156 | 157 | // Display contents in a scrolling region 158 | ImGui.TextColored(TEXT_COLOR, "Important Stuff"); 159 | ImGui.BeginChild("Scrolling"); 160 | 161 | for (int n = 0; n < 50; n++) 162 | ImGui.Text($"{n:0000}: Some text"); 163 | 164 | ImGui.EndChild(); 165 | 166 | // Generate samples and plot them 167 | float[] samples = new float[100]; 168 | 169 | for (int n = 0; n < 100; n++) 170 | samples[n] = Mathf.Sin((float)(n * 0.2f + ImGui.GetTime() * 1.5f)); 171 | 172 | ImGui.PlotLines("Samples", ref samples[0], 100); 173 | 174 | ImGui.DragInt("Draggable Int", ref m_DragInt); 175 | 176 | float framerate = ImGui.GetIO().Framerate; 177 | ImGui.Text($"Application average {1000.0f / framerate:0.##} ms/frame ({framerate:0.#} FPS)"); 178 | 179 | if (m_ShowImGuiDemoWindow) 180 | { 181 | // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. 182 | // Here we just want to make the demo initial state a bit more friendly! 183 | ImGui.SetNextWindowPos(new Vector2(650, 20), ImGuiCond.FirstUseEver); 184 | ImGui.ShowDemoWindow(ref m_ShowImGuiDemoWindow); 185 | } 186 | 187 | if (ImGui.TreeNode("Tabs")) 188 | { 189 | if (ImGui.TreeNode("Basic")) 190 | { 191 | ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags.None; 192 | 193 | if (ImGui.BeginTabBar("MyTabBar", tab_bar_flags)) 194 | { 195 | if (ImGui.BeginTabItem("Avocado")) 196 | { 197 | ImGui.Text("This is the Avocado tab!\nblah blah blah blah blah"); 198 | ImGui.EndTabItem(); 199 | } 200 | 201 | if (ImGui.BeginTabItem("Broccoli")) 202 | { 203 | ImGui.Text("This is the Broccoli tab!\nblah blah blah blah blah"); 204 | ImGui.EndTabItem(); 205 | } 206 | 207 | if (ImGui.BeginTabItem("Cucumber")) 208 | { 209 | ImGui.Text("This is the Cucumber tab!\nblah blah blah blah blah"); 210 | ImGui.EndTabItem(); 211 | } 212 | 213 | ImGui.EndTabBar(); 214 | } 215 | 216 | ImGui.Separator(); 217 | ImGui.TreePop(); 218 | } 219 | 220 | if (ImGui.TreeNode("Advanced & Close Button")) 221 | { 222 | // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). 223 | ImGui.CheckboxFlags("ImGuiTabBarFlags_Reorderable", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.Reorderable); 224 | ImGui.CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.AutoSelectNewTabs); 225 | ImGui.CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.NoCloseWithMiddleMouseButton); 226 | if ((s_tab_bar_flags & (uint)ImGuiTabBarFlags.FittingPolicyMask) == 0) 227 | s_tab_bar_flags |= (uint)ImGuiTabBarFlags.FittingPolicyDefault; 228 | if (ImGui.CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.FittingPolicyResizeDown)) 229 | s_tab_bar_flags &= ~((uint)ImGuiTabBarFlags.FittingPolicyMask ^ (uint)ImGuiTabBarFlags.FittingPolicyResizeDown); 230 | if (ImGui.CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.FittingPolicyScroll)) 231 | s_tab_bar_flags &= ~((uint)ImGuiTabBarFlags.FittingPolicyMask ^ (uint)ImGuiTabBarFlags.FittingPolicyScroll); 232 | 233 | // Tab Bar 234 | string[] names = { "Artichoke", "Beetroot", "Celery", "Daikon" }; 235 | 236 | for (int n = 0; n < s_opened.Length; n++) 237 | { 238 | if (n > 0) { ImGui.SameLine(); } 239 | ImGui.Checkbox(names[n], ref s_opened[n]); 240 | } 241 | 242 | // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): the underlying bool will be set to false when the tab is closed. 243 | if (ImGui.BeginTabBar("MyTabBar", (ImGuiTabBarFlags)s_tab_bar_flags)) 244 | { 245 | for (int n = 0; n < s_opened.Length; n++) 246 | { 247 | if (s_opened[n] && ImGui.BeginTabItem(names[n], ref s_opened[n])) 248 | { 249 | ImGui.Text($"This is the {names[n]} tab!"); 250 | if ((n & 1) != 0) 251 | ImGui.Text("I am an odd tab."); 252 | ImGui.EndTabItem(); 253 | } 254 | } 255 | 256 | ImGui.EndTabBar(); 257 | } 258 | 259 | ImGui.Separator(); 260 | ImGui.TreePop(); 261 | } 262 | 263 | ImGui.TreePop(); 264 | } 265 | 266 | // Ends ImGui window 267 | ImGui.End(); 268 | } 269 | } 270 | ``` 271 | 272 | # Helpful links 273 | 274 | [Dear ImGui Manual In C++](https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html)
275 | [realgamessoftware - Original Repo](https://github.com/realgamessoftware/dear-imgui-unity/issues)
276 | [mattmanj17 - Forked Repo](https://github.com/mattmanj17/dear-imgui-unity#matthewd)
277 | [ImGui.NET - Repo](https://github.com/mellinoe/ImGui.NET)
278 | [cimgui - Repo](https://github.com/cimgui/cimgui)
279 | [imgui - 'Creator' Repo](https://github.com/ocornut/imgui)
280 | 281 | # See Also 282 | This package uses Dear ImGui C bindings by [cimgui](https://github.com/cimgui/cimgui) and the C# wrapper by [ImGui.NET](https://github.com/mellinoe/ImGui.NET). 283 | 284 | The development project for the package can be found at https://github.com/realgamessoftware/dear-imgui-unity-dev . 285 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Banner](static/img/Banner.png) 2 | 3 |
4 | 5 | [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/mrrobinofficial/guide-unitysteamnetcodegameobjects/blob/HEAD/LICENSE.txt) 6 | ![guide-status](https://img.shields.io/badge/guide_status-completed-green) 7 | 8 |
9 | 10 | # 11 | 12 |
13 |

14 | 15 | This repo is a comprehensive repository designed to help users quickly and effectively get started with integrating Dear ImGui, a bloat-free graphical user interface library, into their Unity projects. Dear ImGui provides a lightweight, customizable, and efficient GUI solution that allows you to create user interfaces for your applications and games with ease. 16 | 17 |

18 |
19 | 20 | ## ⚙️ Before you begin 21 | 22 | realgamessoftware - [Original Repo](https://github.com/realgamessoftware/dear-imgui-unity) 23 | 24 | mattmanj17 - [Forked Repo](https://github.com/mattmanj17/dear-imgui-unity#matthewd) 25 | 26 | Forked repo fixes an issue from Unity version 2020.1.0b4: https://github.com/realgamessoftware/dear-imgui-unity/issues/28 27 | 28 | ## ⚒️ How to Install 29 | 30 | * Install via Package Manager by clicking on [add package](https://docs.unity3d.com/Manual/upm-ui-giturl.html), then enter this git URL: 31 | 32 | ```console 33 | https://github.com/mattmanj17/dear-imgui-unity.git#matthewd 34 | ``` 35 | 36 | ## 📝 How To Use It 37 | 38 | * Add a DearImGui component to one of the objects in the scene. 39 | * Create 'Ini Settings Asset' and 'Font Atlas Config Asset' in the project folder. 40 | * Add custom fonts to the '[StreamingAssets](https://docs.unity3d.com/Manual/StreamingAssets.html)' folder and link it up to the 'Font Atlas Config Asset'. 41 | * Change the 'Font pixel' in the 'Font Atlas Config Asset', to scale up the font size. 42 | * When using the Universal Render Pipeline, add a Render Im Gui Feature render feature to the renderer asset. Assign it to the render feature field of the DearImGui component. 43 | * Subscribe to the ImGuiUn.Layout event and use ImGui functions. 44 | 45 | ## 🧠 Scripts 46 | 47 | Simple Script 48 | 49 | ```csharp 50 | using UnityEngine; 51 | using ImGuiNET; 52 | 53 | public class DearImGuiDemo : MonoBehaviour 54 | { 55 | void OnEnable() 56 | { 57 | ImGuiUn.Layout += OnLayout; 58 | } 59 | 60 | void OnDisable() 61 | { 62 | ImGuiUn.Layout -= OnLayout; 63 | } 64 | 65 | void OnLayout() 66 | { 67 | ImGui.ShowDemoWindow(); 68 | } 69 | } 70 | ``` 71 | 72 | Advanced Script 73 | 74 | ```csharp 75 | using ImGuiNET; 76 | using UnityEngine; 77 | using UnityEngine.Events; 78 | 79 | public class DearImGuiDemoWindow : MonoBehaviour 80 | { 81 | private static UnityAction OnUpdateLighting; 82 | private static UnityAction OnLoadGame; 83 | private static UnityAction OnSaveGame; 84 | 85 | private void OnEnable() => ImGuiUn.Layout += OnLayout; 86 | 87 | private void OnDisable() => ImGuiUn.Layout -= OnLayout; 88 | 89 | private static readonly Vector4 TEXT_COLOR = new(1f, 0.8f, 0f, 1.0f); 90 | 91 | private float m_Timeline = 0.5f; 92 | private System.TimeSpan m_Time = System.TimeSpan.FromHours(12.0f); 93 | private string m_Username = string.Empty; 94 | private string m_Password = string.Empty; 95 | private Vector3 m_SunColor = new(1.0f, 0.9f, 0f); 96 | private bool m_WindowEnabled = false; 97 | private bool m_EnableDayAndNightCycle = true; 98 | private int m_DragInt = 0; 99 | private int m_AccountCount = 0; 100 | private bool m_ShowImGuiDemoWindow; 101 | private static uint s_tab_bar_flags = (uint)ImGuiTabBarFlags.Reorderable; 102 | static bool[] s_opened = { true, true, true, true }; // Persistent user state 103 | 104 | /** 105 | * DearImGui 106 | * Manual - https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html 107 | */ 108 | 109 | private void OnLayout() 110 | { 111 | // Begins ImGui window 112 | if (!ImGui.Begin("Game Manager", 113 | ref m_WindowEnabled, 114 | ImGuiWindowFlags.MenuBar)) 115 | return; 116 | 117 | ImGui.Checkbox("Enable Day And Night Cycle", ref m_EnableDayAndNightCycle); 118 | 119 | // Make a float slider (label, value, min, max) 120 | if (ImGui.SliderFloat("Time [%]", ref m_Timeline, 0f, 1f) && m_EnableDayAndNightCycle) 121 | { 122 | m_Time = System.TimeSpan.FromSeconds(m_Timeline * 86400f); 123 | OnUpdateLighting?.Invoke(); 124 | } 125 | 126 | // Display text of current in-game time 127 | ImGui.TextColored(TEXT_COLOR, $"In-game Time: {m_Time:hh\\:mm}"); 128 | 129 | // Create color editor for Vector3 130 | ImGui.ColorEdit3("Sun Color", ref m_SunColor); 131 | 132 | // Creates a menu bar 133 | if (ImGui.BeginMenuBar()) 134 | { 135 | if (ImGui.BeginMenu("File")) 136 | { 137 | if (ImGui.MenuItem("Open", shortcut: "Ctrl+O")) 138 | Debug.Log("Opening a file..."); 139 | 140 | if (ImGui.MenuItem("Save", shortcut: "Ctrl+S")) 141 | Debug.Log("Saving a file..."); 142 | 143 | if (ImGui.MenuItem("Close", shortcut: "Ctrl+W")) 144 | { 145 | m_WindowEnabled = false; 146 | Debug.Log("Closing this window..."); 147 | } 148 | 149 | ImGui.EndMenu(); 150 | } 151 | 152 | ImGui.EndMenuBar(); 153 | } 154 | 155 | if (ImGui.Button("Load Game")) 156 | { 157 | OnLoadGame?.Invoke(); 158 | Debug.Log("Loading the game..."); 159 | } 160 | 161 | if (ImGui.Button("Save Game")) 162 | { 163 | OnSaveGame?.Invoke(); 164 | Debug.Log("Saving the game..."); 165 | } 166 | 167 | if (ImGui.Button("Create Account")) 168 | { 169 | Debug.Log("Creating account..."); 170 | m_AccountCount++; 171 | } 172 | 173 | ImGui.SameLine(0, -1); 174 | ImGui.Text($"Account Count = {m_AccountCount}"); 175 | 176 | // Create input field (label, value, maxLength [uint]) 177 | ImGui.InputText("Username", ref m_Username, maxLength: 12u); 178 | ImGui.InputText("Password", ref m_Password, maxLength: 16u); 179 | 180 | ImGui.Text($"Mouse position: {ImGui.GetMousePos()}"); 181 | 182 | // Display contents in a scrolling region 183 | ImGui.TextColored(TEXT_COLOR, "Important Stuff"); 184 | ImGui.BeginChild("Scrolling"); 185 | 186 | for (int n = 0; n < 50; n++) 187 | ImGui.Text($"{n:0000}: Some text"); 188 | 189 | ImGui.EndChild(); 190 | 191 | // Generate samples and plot them 192 | float[] samples = new float[100]; 193 | 194 | for (int n = 0; n < 100; n++) 195 | samples[n] = Mathf.Sin((float)(n * 0.2f + ImGui.GetTime() * 1.5f)); 196 | 197 | ImGui.PlotLines("Samples", ref samples[0], 100); 198 | 199 | ImGui.DragInt("Draggable Int", ref m_DragInt); 200 | 201 | float framerate = ImGui.GetIO().Framerate; 202 | ImGui.Text($"Application average {1000.0f / framerate:0.##} ms/frame ({framerate:0.#} FPS)"); 203 | 204 | if (m_ShowImGuiDemoWindow) 205 | { 206 | // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. 207 | // Here we just want to make the demo initial state a bit more friendly! 208 | ImGui.SetNextWindowPos(new Vector2(650, 20), ImGuiCond.FirstUseEver); 209 | ImGui.ShowDemoWindow(ref m_ShowImGuiDemoWindow); 210 | } 211 | 212 | if (ImGui.TreeNode("Tabs")) 213 | { 214 | if (ImGui.TreeNode("Basic")) 215 | { 216 | ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags.None; 217 | 218 | if (ImGui.BeginTabBar("MyTabBar", tab_bar_flags)) 219 | { 220 | if (ImGui.BeginTabItem("Avocado")) 221 | { 222 | ImGui.Text("This is the Avocado tab!\nblah blah blah blah blah"); 223 | ImGui.EndTabItem(); 224 | } 225 | 226 | if (ImGui.BeginTabItem("Broccoli")) 227 | { 228 | ImGui.Text("This is the Broccoli tab!\nblah blah blah blah blah"); 229 | ImGui.EndTabItem(); 230 | } 231 | 232 | if (ImGui.BeginTabItem("Cucumber")) 233 | { 234 | ImGui.Text("This is the Cucumber tab!\nblah blah blah blah blah"); 235 | ImGui.EndTabItem(); 236 | } 237 | 238 | ImGui.EndTabBar(); 239 | } 240 | 241 | ImGui.Separator(); 242 | ImGui.TreePop(); 243 | } 244 | 245 | if (ImGui.TreeNode("Advanced & Close Button")) 246 | { 247 | // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). 248 | ImGui.CheckboxFlags("ImGuiTabBarFlags_Reorderable", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.Reorderable); 249 | ImGui.CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.AutoSelectNewTabs); 250 | ImGui.CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.NoCloseWithMiddleMouseButton); 251 | if ((s_tab_bar_flags & (uint)ImGuiTabBarFlags.FittingPolicyMask) == 0) 252 | s_tab_bar_flags |= (uint)ImGuiTabBarFlags.FittingPolicyDefault; 253 | if (ImGui.CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.FittingPolicyResizeDown)) 254 | s_tab_bar_flags &= ~((uint)ImGuiTabBarFlags.FittingPolicyMask ^ (uint)ImGuiTabBarFlags.FittingPolicyResizeDown); 255 | if (ImGui.CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", ref s_tab_bar_flags, (uint)ImGuiTabBarFlags.FittingPolicyScroll)) 256 | s_tab_bar_flags &= ~((uint)ImGuiTabBarFlags.FittingPolicyMask ^ (uint)ImGuiTabBarFlags.FittingPolicyScroll); 257 | 258 | // Tab Bar 259 | string[] names = { "Artichoke", "Beetroot", "Celery", "Daikon" }; 260 | 261 | for (int n = 0; n < s_opened.Length; n++) 262 | { 263 | if (n > 0) { ImGui.SameLine(); } 264 | ImGui.Checkbox(names[n], ref s_opened[n]); 265 | } 266 | 267 | // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): the underlying bool will be set to false when the tab is closed. 268 | if (ImGui.BeginTabBar("MyTabBar", (ImGuiTabBarFlags)s_tab_bar_flags)) 269 | { 270 | for (int n = 0; n < s_opened.Length; n++) 271 | { 272 | if (s_opened[n] && ImGui.BeginTabItem(names[n], ref s_opened[n])) 273 | { 274 | ImGui.Text($"This is the {names[n]} tab!"); 275 | if ((n & 1) != 0) 276 | ImGui.Text("I am an odd tab."); 277 | ImGui.EndTabItem(); 278 | } 279 | } 280 | 281 | ImGui.EndTabBar(); 282 | } 283 | 284 | ImGui.Separator(); 285 | ImGui.TreePop(); 286 | } 287 | 288 | ImGui.TreePop(); 289 | } 290 | 291 | // Ends ImGui window 292 | ImGui.End(); 293 | } 294 | } 295 | ``` 296 | 297 | ## 🔗 Helpful links 298 | 299 | * [Dear ImGui Manual In C++](https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html) 300 | * [realgamessoftware - Original Repo](https://github.com/realgamessoftware/dear-imgui-unity/issues) 301 | * [mattmanj17 - Forked Repo](https://github.com/mattmanj17/dear-imgui-unity#matthewd) 302 | * [ImGui.NET - Repo](https://github.com/mellinoe/ImGui.NET) 303 | * [cimgui - Repo](https://github.com/cimgui/cimgui) 304 | * [imgui - 'Creator' Repo](https://github.com/ocornut/imgui) 305 | 306 | ## 📌 See Also 307 | This package uses Dear ImGui C bindings by [cimgui](https://github.com/cimgui/cimgui) and the C# wrapper by [ImGui.NET](https://github.com/mellinoe/ImGui.NET). 308 | 309 | The development project for the package can be found at https://github.com/realgamessoftware/dear-imgui-unity-dev . 310 | 311 | ## 🆘 Support 312 | If you have any questions or issue, just write either to my [YouTube channel](https://www.youtube.com/@mrrobinofficial), [Email](mailto:mrrobin123mail@gmail.com) or [Twitter DM](https://twitter.com/MrRobinOfficial). 313 | -------------------------------------------------------------------------------- /static/img/Banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MrRobinOfficial/Guide-UnityDearImGui/2a3b94085327c4a56c8ad1444e971a2265261cad/static/img/Banner.png --------------------------------------------------------------------------------