Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity Integration? #1514

Closed
David20321 opened this issue Dec 20, 2017 · 45 comments
Closed

Unity Integration? #1514

David20321 opened this issue Dec 20, 2017 · 45 comments
Labels

Comments

@David20321
Copy link

Has anyone tried integrating Dear ImGUI into Unity? It seems much better than any of the other UI options for Unity in my experience. If not, if I did that would it be ok to sell a wrapper for it on the asset store?

@volcoma
Copy link

volcoma commented Dec 20, 2017

As far as I remember unity has its own imgui implementation which is pretty similar to this one. So I don't see any point of it. Their whole editor is built with it.

@David20321
Copy link
Author

I believe their internal implementation that they use for the editor is not exposed for use in games -- they only expose this system which is much less powerful than Dear ImGUI.

@volcoma
Copy link

volcoma commented Dec 20, 2017

As far as I remember it was pretty similar but its your time man do whatever you like :)

@heroboy
Copy link
Contributor

heroboy commented Dec 21, 2017

You can use the Unity's IMGUI in game. https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html
And it's much powerful than Dear ImGUI. Because Dear ImGUI is one pass, but Unity's OnGUI is like WinProc. In OnGUI there is a Event.current to indicate what to should do, then the system can percisely know what you want to do.

@David20321
Copy link
Author

Is this the complete list of all imgui-style Unity controls? Labels, buttons, textfields, sliders, checkboxes and windows. https://docs.unity3d.com/Manual/gui-Controls.html

I would like to use nested menus, tooltips, trees, graphs, multiple controls per line, and so on.

@MrFrenik
Copy link

I would also like to see this attempted. Unity's ImGUI implementation leaves a lot to be desired, especially after having used Dear ImGUI.

@stevesan
Copy link

I'd be interested in this, mostly because Unity IMGUI API has garbage generation issues. Not sure if this was fixed recently though.

@codecat
Copy link
Contributor

codecat commented Sep 19, 2018

Has really nobody tried this yet? 😱

@DoctorGester
Copy link
Contributor

@David20321 ended up doing it himself it seems: https://twitter.com/Wolfire/status/1057060079483011072

Maybe he'll share the tech!

@ghost
Copy link

ghost commented Nov 12, 2018

I am currently attempting this myself and have already hooked into the rendering pipeline. Unfortunately though I am not experienced with DirectX11, hence I wasn't able yet to get ImGui to render properly. Will update you guys if I happen to be successful.

EDIT1 Quick update after an hour into experimenting with the integration.

You might see the very small window on the top left - for some very weird reasons it it only visible when I have the context menu of the Game view open; otherwise, not.

At least this is an indicator that I am doing at least something right. As mentioned before though, I have never used low level graphics APIs and will definitely need a lot of time since I am getting this done via trial-and-error.

EDIT2 It seems that Unity clearing the screen and thus not rendering the ImGui window unless I open the context menu which actually pauses execution of the game which makes the ImGui window visible all of a sudden.

EDIT3 Here is a topic I just created for the issue if someone of you wants to chime in to get this issue fixed.

EDIT4 I already managed to fix the issue which was indeed a wrong order of draw calls. Now it renders almost correctly:

However the window has a very weird size, doesn't resize to its contents and cannot be interacted with. Most likely this is an issue with the mouse handling as well as the inability to fetch the window handle of the game window. I will try my best in the next few days after seing that it is indeed possible :)

@ghost
Copy link

ghost commented Nov 12, 2018

Well call me stupid, I completely forgot to hook the Unity WndProc in order to call the ImGui Win32 WndProc that is being supplied in order to handle input: https://www.youtube.com/watch?v=EqqXp8S62Rw

Unfortunately I am still having two issues:

  1. When closing the game or stopping execution inside of Unity, I am getting a crash without any details. My hook regarding the WndProc seems to be not entirely clean yet.
  2. When running in the Unity Editor (instead of a standalone executable) all positions are computed relative to the top-left corner of the Unity Editor rather than the Game view since I am nnot able to fetch the handle to the Game view yet. I might have to figure that out, otherwise I will have to maximize the game view and just add some offset to all RECTs (in Editor mode) which would be horrible.

@codecat
Copy link
Contributor

codecat commented Nov 12, 2018

Very nice work so far! Will you put the sources for this on GitHub soon?

@ghost
Copy link

ghost commented Nov 12, 2018

Once I'm able to wrap my head around hooking into the Editor itself (mouse click events are still not processed in Play mode, only in the standalone variant) I can definitely write a summary on how to integrate imgui into Unity and share the necessary snippets.

The problem is mainly, that Unity is really weird with regards to native plugins and on top of that I am having my code in an entirely different library that my managed code dynamically loads using LoadLibrary instead of extern definitions using DllImport. What it really comes down to is that the actual implementation will differ for each use case since it's quite difficult to interop between C# and C++ unless you write everything (even the game code) in C++ as I do. I really think, unlike other wrappers, this won't be as easy as dropping a DLL and having access to it right away 😄

@codecat
Copy link
Contributor

codecat commented Nov 12, 2018

Ah, fair point. I think for it to work well in C# you'd have to write interop functions for cimgui, and then probably write the rendering manually in C# as well.

@ghost
Copy link

ghost commented Nov 12, 2018

I'd like to announce that I managed to fully inject imgui into Unity, both for standalone games and the Unity editor itself: https://youtu.be/LXd33Bs3JWY

In the end I was able to find the corresponding handles of the Game view window (thank you Winspector Spy, you never cease to amaze me) and with some tweaks I am able to differentiate between a standalone build and the in-editor one.

One of the caveats is that for some reason ImGui stops to render once the Game view is maximized using the context menu command - I can just assume for the time being that a different swap chain or something akin to this is created onto which I am no longer drawing at that point.

Furthermore another caveat is that imgui breaks the way I hot reload my native DLL into a running game in the editor. This is just an issue on my end due to the architecture and nature of the project, which I should be able to fix nonetheless.

I will be looking into explaning the steps I had to take in order to inject this properly (unfortunately I had to do some minor tweaks to the imgui sources themselves to handle the editor) in the upcoming days, once I'm in the mood for it 🤣

@ghost
Copy link

ghost commented Nov 12, 2018

In the hopes of getting some positive feedback I jumped in the shark tank over on Reddit: https://www.reddit.com/r/Unity3D/comments/9wevmn/wip_integrating_dear_imgui_into_unity_a_small/

@uglycoyote
Copy link

@ghost I checked out the video, that looks great that you have it running.

Reddit page looks dead now. Did you ever release this? I agree it would be great to get this working in Unity. I have used Dear ImGui a fair bit and just on a quick glance I can tell that Unity's built in IMGui stuff is going to be a bit of a pain to use, just with having to specify the dimensions of buttons and such, and doesn't look like it has windows in the way that Dear ImGui does.

In your use case are you able to write ImGui calls from C# game code, or did you avoid that because you have your game code in a native C++ DLL? For my purposes, I would want to access it from the C# side.

@ocornut
Copy link
Owner

ocornut commented Jun 17, 2019

@David20321 and his team have been working on a very complete port of Dear ImGui to the Unity ecosystem (including fully ported C# Demo and various adhoc tools). It's a little late as they had to shift resources to the current game they are making but hoping it get released shortly!

@BjarkeCK
Copy link

BjarkeCK commented Jul 9, 2019

@ocornut @David20321 I'm very much looking forward to this, is any of it on a public repo?

@bddvlpr
Copy link

bddvlpr commented Oct 28, 2019

Any updates on this?

@bddvlpr
Copy link

bddvlpr commented Nov 9, 2019

Decided to give it a shot myself and (almost) got it to work. Getting close. (Think my material is in fault here)
Image

@bddvlpr
Copy link

bddvlpr commented Nov 9, 2019

image
jup...

@bddvlpr
Copy link

bddvlpr commented Nov 9, 2019

I'd say i got about 70-80% of it working but i'll probably be halting development here. If anyone else wants to take it over just comment.
image

@bddvlpr
Copy link

bddvlpr commented Nov 13, 2019

Couldn't help myself from actually fixing it but got it now!
Image

@uglycoyote
Copy link

@stijnsimons that's great. Are you planning to open-source your effort? I'm sure all who are following this thread would be interested.

@bddvlpr
Copy link

bddvlpr commented Nov 13, 2019

@stijnsimons that's great. Are you planning to open-source your effort? I'm sure all who are following this thread would be interested.

Most of the rendering logic is taken from @David20321 so its up to him. If he agrees; i'll be making it public.

@peterdijkstra
Copy link

Great work @stijnsimons !

@nitrog0d
Copy link

anything yet? :o

@bddvlpr
Copy link

bddvlpr commented Dec 27, 2019

anything yet? :o

Got no reply from him on this thread so I don’t know.

@ocornut
Copy link
Owner

ocornut commented Dec 27, 2019 via email

@JayPeet
Copy link

JayPeet commented Jan 19, 2020

I was already working on this for a while, because I got annoyed at seeing people having it working in unity, then never actually open sourcing it themselves :)

https://github.com/JayPeet/UnityImGUI

I'm at the end of my abilities on writing the c++ rendering side of things, and my time is quite limited due to other projects, however it functions with DX11 and allows you to write the ImGui code from the c sharp side of things. However it is just a prototype and is overall quite rough.

Looking for help finishing the project.

@ocornut
Copy link
Owner

ocornut commented Feb 10, 2020

We resumed working on that on our side.
Here Dear ImGui running in Unity on the Switch over Edgar Bokbok in Boulzac:

IMG_20200207_175321

@JayPeet
Copy link

JayPeet commented Feb 10, 2020

Thats awesome news :)
Looking forward to checking it out in the future!

@pointcache
Copy link

Faster. FASTER. :D

@Mantisis
Copy link

Very interested by this !
Thanks for pushing the effort forward

@goncalo
Copy link

goncalo commented Mar 6, 2020

We have been using Dear ImGui in Unity for some of our projects and I finally had the time so separate it into a package.

UPM package
https://github.com/realgamessoftware/dear-imgui-unity

Repository for the development project
https://github.com/realgamessoftware/dear-imgui-unity-dev

Usage

  • Add package from git URL: https://github.com/realgamessoftware/dear-imgui-unity.git .
  • Add a DearImGui component to one of the objects in the scene.
  • 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.
  • Subscribe to the ImGuiUn.Layout event and use ImGui functions.
  • Example script:
    using UnityEngine;
    using ImGuiNET;
    
    public class DearImGuiDemo : MonoBehaviour
    {
        void OnEnable()
        {
            ImGuiUn.Layout += OnLayout;
        }
    
        void OnDisable()
        {
            ImGuiUn.Layout -= OnLayout;
        }
    
        void OnLayout()
        {
            ImGui.ShowDemoWindow();
        }
    }

The package uses C bindings by cimgui and the C# wrapper by ImGui.NET.

Some extra functions and overloads were added, mostly dealing with text or images (ImGuiUn.Image() for Texture and Sprite), as well as helpers to wrap callbacks with try-catch blocks. These can be found in the ImGuiNET/Wrapper.Extra folder.

This integration uses the Unity rendering API instead of a native plugin for rendering.
It works on both the Built-In and the Universal render pipelines and has two renderer types: one using CommandBuffer.DrawMesh and another based on CommandBuffer.DrawProceduralIndirect.

There are 'platform bindings' for both Unity's old Input Manager and the new Input System. Some configuration options such as style, fonts and cursor shapes have corresponding assets and can be edited from the inspector.
dear-imgui-inspec

And here it is in use by one of our projects:
rvp-imgui


Known issue: blurred on Universal RP with FXAA.
urp-fxaa-blurred

@JayPeet
Copy link

JayPeet commented Mar 6, 2020

Very cool @goncalo !

@peterdijkstra
Copy link

Awesome @goncalo ! I opened up some issues regarding macOS. Not sure if you would fix them as it seems that official Unity support is on the way, but I thought I'd open up the issues regardless 😄

@zakkar
Copy link

zakkar commented Jun 4, 2020

Any compatibility with 2018 version ? When i opened it with 2018 LTS version it says that the assets are serialized with a newer version

@trapazza
Copy link

Hi there.

Has anyone managed to load and use a font other than the default one? It's not working for me and have no idea what I'm doing wrong.

This is what I do:

var io = ImGui.GetIO();
io.Fonts.AddFontDefault();
var font = io.Fonts.AddFontFromFileTTF( font_path, font_pixels ); // font stored in a dictionary
io.Fonts.Build();>

I checked that the font is properly loaded (.IsLoaded()), but the result is that all fonts get corrupted (even the default one). In this case I'm not even using the PushFont/PopFont pair.

image

Thanks everyone in advance.

@ocornut
Copy link
Owner

ocornut commented Aug 15, 2020

@trapazza I believe you should ask on the https://github.com/realgamessoftware/dear-imgui-unity repository.

@ocornut
Copy link
Owner

ocornut commented Feb 22, 2021

Closing this.
For Unity-backend related support please refer to https://github.com/realgamessoftware/dear-imgui-unity
David's own Unity backend which at some point we considered making official was used on David's project but not released seeing realgamessoftware's solution seemed adequate.

@noncom
Copy link

noncom commented Jan 17, 2024

Hi, I'm sorry to necro this thread, but I would want to just ask: what's the status of current support?

I'm asking here because the https://github.com/realgamessoftware/dear-imgui-unity repository was last updated 4 years ago, and there's quite a bunch of unanswered issues, so I don't believe that asking there is going to yield any kind of answer.

Is this officially over and nobody is working on making available ImGUI in Unity?

@ocornut
Copy link
Owner

ocornut commented Jan 17, 2024 via email

@PathogenDavid
Copy link
Contributor

@ocornut Looks like your link got eaten.

To save others from tracking things down:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests