Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Host a Unity app inside a control of a UWP app

Discussion in 'Windows' started by stensquared, Aug 16, 2017.

  1. stensquared

    stensquared

    Joined:
    Aug 16, 2017
    Posts:
    4
    Hello Uniteers,

    Im researching something quite unusual here, and I hope someone can help me solving some issues I am encountering.
    I am trying to host a Unity app inside a control of a UWP app.

    This is the approach I took:

    - build the unity app as UWP xaml format

    - from the Visual Studio project of the Unity app, retrieve

    o the MainPage with its xaml

    o the dlls that Unity needs to run the app

    o the code of the App script to insert into the App script of the UWP exe.

    - Manually copying the code and dlls found into the UWP project, using a unique App.cs script to initialise, but loading the Unity app as a separate page.

    The control where the code from the unity MainPage is now inside a dll separate from the UWP exe. The code for the Unity app is therefore split across two assemblies.

    The execution fails when the splash screen is about to be shown, apparently being unable to load some dlls (no build errors are displayed when referencing the unity dlls).

    ttv3crash.PNG

    I am hitting a series of issues from within the unity dlls which are not exposed. I was wondering if anyone has ever tried anything similar and can correct my trajectory, perhaps suggesting a better approach.


    Thank you all and happy coding!
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    This error indicates that our AssemblyConverter.exe tool has not run on managed assemblies. It is run by a pre-build event in the generated .csproj file. Did you by any chance nuke those calls, or rather, did not copy them?
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Is there a reason to hack around this much?
    Unity exports a VS solution and lets you modify it the way you want. You are free to remove Unity player from MainPage and put it to some other page etc. Some people have done that successfully. The advantage is that you can continue to change project in Unity and build from there on top of your customized solution, all your modifications should be preserved.
     
  4. stensquared

    stensquared

    Joined:
    Aug 16, 2017
    Posts:
    4
    Thank you for your reply. Since the prior work looked indeed quite hacky, I am now trying a different approach. By using one unity app to host multiple swap-chains. This is the result:
    upload_2017-8-18_9-29-47.png
    upload_2017-8-18_9-30-22.png

    Hitting a brickwall at this point. The second swapchain doesnt seem to be willing to host a second project. I tried by making a second instance of the MainPage by making a second AppCallBack instance which failed because only one seems to be allowed.
    The question is: is it possible to host more than one unity player in the same UWP app? the other unity players being separate projects
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    It is not possible to host multiple simultaneous Unity players. The engine (UnityPlayer.dll) stores a lot of things in static and global variables (which is not great, but that's the situation today), and trying to start up more than one instance will fail miserably.

    Why do you want to host two instances of Unity? It's possible for a single instance of Unity to render different things to multiple swapchains (as long as you're ok with writing a graphics plugin).
     
  6. stensquared

    stensquared

    Joined:
    Aug 16, 2017
    Posts:
    4
    Thank you very much for clarifying. My intention was to build a Unity App to use as platform to host other Unity Apps while they running. I will have to change my plans accordingly, truly appreciate the quick reply.
     
  7. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    We're trying to do a similar thing, where we embed a Unity View into an existing UWP, so I'm trying to merge the exported UWP into existing.

    Can you explain where the UnityEngine.dll is stored in the exported UWP project? I don't understand how the reference is made, or where the file is. The only ones I can find are in bin/.

    CLRHost and UnityEngineDelegates are also not present in the exported folder.

    Obviously I'm missing something about how this reference is made... I can see the UnityPlayer reference in VS, but no info on the path and I can't determine how or where the reference is made.

    Any clarification on this would be greatly appreciated.
     
    Last edited: Dec 20, 2017
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    Are you using the .NET or IL2CPP scripting backend?

    You could make a XAML type build, which is essentially a mini-example of how you could integrate your UWP app with Unity.
     
  9. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    I was using XAML, but couldn't determine the path of any of the references. I upgraded Unity from 2017.1.1 to 2017.1.2, and rebuilt the XAML, and now when I select the reference, I can see the path in the Properties windows, as expected. So, not sure if it was an issue with the export, or visual studio, but I'm ok now.

    For the record, the missing DLL's were in the PlaybackEngines folder in the Unity install location.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,644
    Those DLLs get copied to the project folder if you check "Copy References" option in the build window in Unity editor. We don't do it by default and just reference them from Unity installation directory for performance reasons (not copying something is faster than copying it).
     
    shawnblais likes this.
  11. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    Perfect, thanks for the info, that helps.