Search Unity

How to integrate Unity3D into C# WPF application?

Discussion in 'Windows' started by brendan.hill, Jun 10, 2015.

  1. brendan.hill

    brendan.hill

    Joined:
    Jun 10, 2015
    Posts:
    2
    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.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You can use -parentHWND command line option to create an embedded unity window:

    http://docs.unity3d.com/Manual/CommandLineArguments.html

    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.
     
  3. brendan.hill

    brendan.hill

    Joined:
    Jun 10, 2015
    Posts:
    2
    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?
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,918
    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.
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    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.
     
  6. jasimejaz123

    jasimejaz123

    Joined:
    May 23, 2018
    Posts:
    1
    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?
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    You basically just have to start the player with "-parentHwnd <parentWindowHandle>" argument and Unity will create the window under your parent window.
     
    nirvanajie likes this.
  8. itshadimughal

    itshadimughal

    Joined:
    Feb 6, 2024
    Posts:
    1
    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 ?
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    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:

    Code (csharp):
    1. extern "C" __declspec(dllimport) int UnityMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd);