How to integrate Unity3D into C# WPF application?

Hello,

I am very new to Unity3D.

I need to :

  1. Display Unity3D content inside a C# WPF application
  2. Add content to the Unity3D space programmatically from the C# application
  3. Feed information about which objects in the Unity3D model were clicked by the user, back to the C# application (eg. show a message on the WPF form saying “User clicked on cell #xxx

How can this be done?

Thanks,
-Brendan

P.S. As an aside, I tried posting this in the “Answers” website but continually got “INVALID AUTHORIZATION REQUEST - an error occurred trying to sign in: bad_request”. This happened via two different accounts in both IE, Chrome and Chrome incognito mode. Hence posting it here.

You can use -parentHWND command line option to create an embedded unity window:

You’d use HwndHost control on WPF side.

And then you could use an IPC mechanism of your choice to communicate between unity and your application.

4 Likes

There will be a substantial amount of communication - updates to objects color & location etc and the performance of serialising & deserialising this continuously over an IPC in real time I think would be poor.

Is there any way to make their integation closer eg. expose the Unity3d stuff to the C# application via a DLL?

I doubt it, one of the main reasons, is because your WPF application is running on Microsoft .NET, while Unity child application is running on Mono. So it will be hard to get “close” integration.

I highly doubt the performance of IPC will be your bottleneck.

As for embedding it within a single process: it’s possible, I guess, but I don’t think it will make things easier at all (harder even, if I had to guess). You’d have to P/Invoke to a native DLL from your script, which would in turn load your application in the same process. You’d still have to come up with communication scheme, as you would not be able to directly call WPF land C# from script C# and vice versa.

As far communication is concerned i’m sure we can communicate between 2 applications. I’m also trying to do something similar, but i don’t want to a WPS application to interact with a unity application or .exe. Instead i want to display my game view inside a WPS application then interact with unity.

Is it even possible or do i have to opt-out a different approach?

You basically just have to start the player with “-parentHwnd ” argument and Unity will create the window under your parent window.

1 Like

I

this will start process thirgh exe file
but i want to load the ouput of unity in wpf
mean i want to play the animation in a grid in wpf ?

You can call into UnityPlayer.dll with that command line argument. UnityMain is the entry point function. It takes the same arguments as wWinMain but has cdecl calling convention:

extern "C" __declspec(dllimport) int UnityMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd);