Search Unity

Using Unity in WPF App

Discussion in 'Windows' started by sacunha, Sep 21, 2017.

  1. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Our company is currently searching for a way to have a 3D house visualizer in our (already developed) software.
    We already have a prototype where we use a windows form host to hold a Unity application and use it (the Unity app) as the 3D visualizer.
    However, this is not optimal as multiple windows form hosts with multiple unity instances get really heavy really fast.

    We tried a Unity UWP build, wich showed the best performance but failed to integrate it in our software.

    Can someone sugest an alternative, or point us in the right direction on how we can embed a Unity UWP in a WPF software?
     
  2. Deleted User

    Deleted User

    Guest

    I am sort of familiar with WPF... that's the newer .NET windows forms software. I have never heard of anybody trying to do something like this with Unity. I don't know how you would improve the efficiency of your prototype. Let me point you to the OGRE 3d engine written in C++, perhaps that would suit your needs better. http://www.ogre3d.org/

    [edit] apologies to the mods if this constitutes advertising, not my intention. Just trying to be helpful as I am not sure this user will be able to do what they wish with Unity.
     
    sacunha likes this.
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Regardless of the solution you go with, you'll have to use HwndHost WPF control. The easiest way I see is to use -parentHwnd argument to Windows Standalone player to make it parent itself to your HwndHost control. I don't know if it's possible to do it for a UWP application (you might have to mess with win32 specific calls).

    I find it interesting that you found UWP applications performing better in Unity than the Standalone player. The performance should be identical. How big of a difference are we talking about?
     
  4. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    We are already using HwndHost. We based ourselfs in this example provided by Unity: https://docs.unity3d.com/Manual/CommandLineArguments.html but here Unity player is hosted in a Windows Form application and in order for us to replicate the behaviour, we had to use a windows form host on our WPF application.

    Maybe I didnt explain myself correctly about the performance of the aplication.
    When we run a single instance of the Unity player (embeded in our WPF application, using a windows form host control) the performance is identical as if the player was not embeded at all. But if we run a second instance of the player (not the same one, a different Unity application with different code), the performance drops dramastically, causing the application to have a 2-3 second lag input (not just the unity application that lags, our applications that embed the players also lag)
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
  6. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    XAML componentes don't have a .handle Id, only the entire application window has a handle id. I cant fit the unity application inside a grid (that takes a 3rd of the screen).
    I haven't profiled yet. Its not at the top of the list to run multiple 3D applications yet. I've tried only windows task manager to see memory and CPU consumage and they were both average to low (1.5% CPU on both unity processes and 300mb RAM). But will take a look at that profiler link you provided as soon as the company allows
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
  8. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Previous search of mine led me to believe I could not get the controller Handle, but only the (main/parent) window. A quick new search has shed some light on some new results. I'm going to leave this thread on standby until I fully test my new found results. Thanks for the pointers :D

    https://docs.microsoft.com/en-us/do...ed/walkthrough-hosting-a-win32-control-in-wpf
     
  9. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Hey! I've finally managed to put unity applications on WPF! Hurray!
    Just 1 problem: If I run 2 or more "Unities", everything gets suuuper slow!
    Why?
    How can I solve this?

    (Here is a link with a simple project example. Just go to VS Project\teste 2.sln and open it)
    https://www.dropbox.com/s/pc8k1kixtbfe6p3/projects.rar?dl=0
     
    Last edited: Oct 3, 2017
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Did you try using a profiler?
     
  11. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Unity profiler?

    EDIT:

    I just tried Unity profiler. I had no ideia it could be used on a Build. I thought it could only be used in editor mode.
    Anyway, I profiled my applications and theyr performance did was the same when I ran a single application/unity build, when I embeded that same application/unity build and when I embeded 2 applications/unity builds. I did not see any changes in the profiler, so my problem is probably not with Unity, but with WPF :S
     
    Last edited: Oct 4, 2017
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    So Unity profiler says they're running smoothly, but you can see visual glitches?
     
  13. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    In Unity there are no glitches. It runs smoothly (either with 1 or 2 applications). My WPF application is what gets slowed. The framerate drops to 1 frame per 4-8 seconds. Yes, seconds. That slow. I have to mouse over a button, wait for 4 or 5 seconds for it to fire mouse over and only then I'm able to click it.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674