ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
61.41k stars 10.34k forks source link

Unity Integration? #1514

Closed David20321 closed 3 years ago

David20321 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

heroboy commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

codecat commented 6 years ago

Has really nobody tried this yet? 😱

DoctorGester commented 6 years ago

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

Maybe he'll share the tech!

ghost commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

ghost commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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

bddvlpr commented 5 years ago

Any updates on this?

bddvlpr commented 5 years ago

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

bddvlpr commented 5 years ago

image jup...

bddvlpr commented 5 years ago

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 commented 5 years ago

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

uglycoyote commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

Great work @StijnSimons !

nitrog0d commented 4 years ago

anything yet? :o

bddvlpr commented 4 years ago

anything yet? :o

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

ocornut commented 4 years ago

I think I found a developer to take on this project seriously but they’ll only be able to start working in February.

JayPeet commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

pointcache commented 4 years ago

Faster. FASTER. :D

Mantisis commented 4 years ago

Very interested by this ! Thanks for pushing the effort forward

goncalo commented 4 years ago

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

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 commented 4 years ago

Very cool @goncalo !

peterdijkstra commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

ocornut commented 3 years ago

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 commented 10 months ago

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 commented 10 months ago

There’s another backend listed in Wiki which apparently has been updated 3 months ago.Bindingsgithub.com

PathogenDavid commented 10 months ago

@ocornut Looks like your link got eaten.

To save others from tracking things down: