Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Integrating Unity inside of an existing UWP app.

Discussion in 'Windows' started by Ionut-Popovici, Jan 3, 2019.

  1. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    Hello forum!

    My end goal for the application I'm developing is to have a control that contains a Unity app (Arch-Vis).
    Things I have tried (and failed):

    1. Building the Unity project for UWP with .NET scripting backend and XAML (since we already have C# libs with XAML) and trying to integrate our existing framework.

    Issues encountered:

    The AssemblyConverter.exe command in the csproj fails. In an attempt to understand what's happening I've decompiled the exe and tried to debug what's happening, and I found out that more than 2 versions of System.Collections or something like that come into that exe and it's trying to add a key to the a dictionary with the same name (obv. doesn't work) so it crashes.

    In thinking maybe it's because our framework is split between multiple csproj (Universal Windows and NetStandard2.0) I created a single *.All library with all the code in it (the type of that lib being Universal Windows, target 1803 with C# 7.3 lang spec)

    Still the AssemblyConverter command failed. To note is that I also have additional nuget packages for the project:
    - Microsoft.Toolkit.Uwp.Ui.Animations
    - Microsoft.Toolkit.Uwp.Ui.Controls
    - Newtonsoft.Json


    2. Tried cannibalizing the unity project build and adding it to the exiting UWP app.

    I first created a XAML build with .NET scripting backend (tried both debug and master versions).
    I analized the project structure and tried to move the engine DLL's around to my projects.
    After I discovered (I think) what AssemblyConverter does, instead of moving the "unprocessed" DLL's I went into the bin/Debug/x64/Appx folder and tried to get the DLL's from there, and the .winmd files directly from the instalation folder. To note that all builds that I've talked about had "Copy References" active.

    All well and good, even managed to add the references and write in the initialization code.

    This is the version that I've got "the most functioning" (I say in quotes because it still crashes): When I start the debugger I can step over most of the initialization and even call InitializeD3DXAML but after exiting the ctor (where the init happens) it crashes with:

    Exception thrown at 0x00007FFB004245F7 (UnityPlayer.dll) in <theMainExecutable>: 0xC0000005: Access violation reading location 0x000000000000026B.

    I've also been stepping through dissasembly and have the following:



    (it crashes at the cursor)

    RCX is 0 at that time, and we can obv see where the 0x000000000000026B. comes from.

    Ideally what I would like is to have a separate DLL/winmd file that contains the unity app as a usable page/control.
    Is it possible to convert the unity build output into something that i can "add a reference to"?

    Under the current architecture I have a client controls library that needs the unity stuff, and if I could add this magic library to that proj, and then that client lib is referenced in the main app, then all can go well?

    Any help would be most appreciated.
    To note, all of the above have been worked on with 2018.3
     
  2. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    After I wrote this post I tried literally chaining the output type in the csproj to library and I don't know how but it seems it's working (but it's still crashing, but now i get unity actually outputing some logs, where previously unity loading would crash)

    Here is an excerpt from the log:


    Initialize engine version: 2018.3.0f2 (6e9a27477296)
    [AudioManager] InitNormal(tryDeviceDefaults = false, preferredOutputType = FMOD_OUTPUTTYPE_AUTODETECT) attempt with hardAudioDisable: false
    [AudioManager] Setting output to FMOD_OUTPUTTYPE_AUTODETECT
    [AudioManager] InitNormal succeeded with output "FMOD_OUTPUTTYPE_WASAPI". Driver name is "Speakers (USB Audio CODEC )". Speaker mode is "FMOD_SPEAKERMODE_STEREO"
    Failed to load assemblies:
    System.ArgumentNullException: Value cannot be null.
    Parameter name: type
    at System.Reflection.IntrospectionExtensions.GetTypeInfo(Type type)
    at WinRTBridge.AssemblyProvider.LoadUnityMetadata()
    at WinRTBridge.Utils.LoadAssemblies(String assemblies)
    (Filename: C:\buildslave\unity\build\Runtime/Scripting/DotNet/MonoManager_WinRT.cpp Line: 63)
    The program '[19400] UWPApp.exe' has exited with code -1 (0xffffffff).
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,163
    The above will happen if the assemblies copied to your build output didn't get processed by Assembly Converter.

    Generally, AssemblyConverter needs to run on all DLLs that are in your Unity project every time before you build Visual Studio project. The way Unity sets it up is that it makes the DLLs to be copied to the root of the project from Unprocessed folder, and then run AssemblyConverter on that.

    Note, AssemblyConverter doesn't really support processing DLLs targeting .NET Standard 2.0. It assumes all the DLLs it touches target .NET for Universal Windows Platform v5.0.

    Does the project work on its own if you don't embed it into your UWP app?

    Lastly, .NET scripting backend is deprecated at this point and doesn't really have a future. Have you considered using IL2CPP scripting backend?
     
  4. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    I've just got back in the office and I have successfully launch the app. What I finally ended up doing is, after taking the FINAL output of the build (after all the processing has been done) literally just copy all the unity specific dll's including Assembly-CSharp and the Data folder (in the future these need to be updated as we continue to develop the app).

    Project looks like this:


    I added the only 3 references I need to actually initialize Unity: UnityPlayer, BridgeInterface, WinRTBridge. I'll probably need Assembly-CSharp as well because we have introp between the app and the unity part.

    Currently it's crashing with
    System.TypeLoadException: 'Requested Windows Runtime type 'UnityPlayer.AppCallbacks' is not registered.'

    But that's more of my logistics problem, moving these dlls where arround, because in the screenshot above it actually loads (that being a test project)
     
  5. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    Note: all my projects are on the latest version of UWP so 6.1.9 not 5.0.0
     
  6. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    After testing with il2cpp, unfortunately it doesn't support (as far as I can see) my use case so I need the .net scripting backend
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,163
    Keep in mind .NET will be removed in 2019.1 so if you plan to upgrade your Unity version past 2018.3, you will need to migrate to IL2CPP.

    What use case does it not support that you require?
     
  8. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    From the main app I need to send and receive some data in order for the required animations to trigger. The way I do it now is reference the Assembly-CSharp.dll and call from there a static manager that integrates with the rest of the unity module.

    The main app is a C# UWP project with multiple other csproj attached to it, both universal, .net standard.

    Can i somehow reference the il2cpp output? maybe create a .winmd for GameAssembly.dll output?
     
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,163
  10. zen_luis_unity

    zen_luis_unity

    Joined:
    Mar 17, 2023
    Posts:
    1
    Hi all and @Tautvydas-Zilys ,

    I am able to run my Unity application within the UWP project. But I could not communicate between the two applications. I looked at previous examples.
    https://github.com/Unity-Technologies/DesktopSamples/tree/master/UniversalWindowsPlatformSamples/XAMLUnityConnection

    I think this project is incomplete.
    I tried to add Csharp-Assembly.dll to my own project as a reference (I get an error). For this reason, I cannot use the classes in UnityScript.cs. Apart from this, I tried options such as adding a c++ runtime component project, but I could not succeed. I couldn't find an up-to-date code example or article about it. Waiting for your help.
     
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,163