Search Unity

[UWP][IL2CPP] Finding a way to interop between existing uwp (C#) app and unity

Discussion in 'Windows' started by Ionut-Popovici, Apr 1, 2019.

  1. Ionut-Popovici

    Ionut-Popovici

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

    This post is loosely related to another one i did in the winter (https://forum.unity.com/threads/integrating-unity-inside-of-an-existing-uwp-app.606874/)

    We currently have a pipeline that allows unity to be placed inside an existing uwp app but it's quite convoluted and also depends on the .NET scripting backend.

    I wish to RnD my way out of the deprecation of .net scripting backend and somehow find a solution that will work with il2cpp.

    Current pipeline:

    1. From the unity sources, create a unity c# xaml build.
    2. Build and run from visual studio (the run step is to create the Appx folder inside the bin\x64 folder)
    3. Copy all relevant DLLs (a bunch of them) into a folder in the main app project repo.
    4. The actual UWP target app has references to all the unity engine dlls and AssemblyCSharp.dll (basically it's similar to the one that is created in a regular .sln build from unity)
    5. The Data folder is copied in the main app and is set to Content

    Notes: In the main UWP app, we activate unity with code that's similar to the one in the output c# project

    Code (CSharp):
    1.  
    2.             var bridge = new ScriptingBridge();
    3.             var appCallbacks = AppCallbacks.Instance ?? new AppCallbacks();
    4.             appCallbacks.SetBridge(bridge);
    5.             appCallbacks.SetSwapChainPanel(SwapChainHolder.SwapChain);
    6.             appCallbacks.SetCoreWindowEvents(Window.Current.CoreWindow);
    7.             appCallbacks.InitializeD3DXAML();
    8.             appCallbacks.RenderingStarted += AppCallbacks_RenderingStarted;
    9.  
    As I'm looking to optimizing and future proofing this, il2cpp seems the way to go.

    My main hurdle is interop. Our app needs to call stuff from unity (let's say to trigger an animation, but that's an oversimplification). I've yet to find a way to get this working without going into C++. Seems like that's not going to fly, as I've seen as a response to my initial post.

    Being that my project is not store-deployed, I am able to do some hacky out of the way stuff, so if there is anything you guys here know, let's say to instantiate unity in a process and get the framebuffer and render it in a swapchain within uwp, i'm open to anything.

    Thanks
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,443
    I don't think you'll be able to talk from C# to C# without _any_ C++, although you can minimize it to 10 lines of code. Is there a reason you have a requirement for no C++?
     
  3. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    So thinking more on this we want to try and test out-of-app rendering instead of directly integrating the unity build within our main app. So it would go as follows: we have 2 running apps, the UWP App and the Unity App (UWP/Win32, does it even matter?) and if possible i'd like to take the frame buffer and set it to a memory mapped file (our target is usually 4k or 6k) of an appropriate size. Then have a view in the UWP app and render that inside a WritableBitmap or a SwapChainPanel or anything that would support linking an existing mem address to that location.

    Is this something that can be done and have decent performance? (assume il2cpp build on unity side)
     
  4. Ionut-Popovici

    Ionut-Popovici

    Joined:
    Nov 22, 2016
    Posts:
    11
    I've started exploring the API a bit
    Code (CSharp):
    1.        
    2. var mmap = MemoryMappedFile.CreateNew("hFileMap", 132_710_400, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages, System.IO.HandleInheritability.Inheritable);
    3.         var view = mmap.CreateViewAccessor();
    4.  
    5.         var hFileView = view.SafeMemoryMappedViewHandle.DangerousGetHandle();
    6.  
    7.         var hTex = Texture2D.CreateExternalTexture(3840, 2160, TextureFormat.ARGB32, false, false, hFileView);
    8.  
    9.         //Graphics.SetRenderTarget( <but somehow but this hTex in?> )
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,443
    That will crash, because CreateExternalTexture() expects D3D11 texture, rather than a memory handle.

    If your main app is UWP, you'll have to embed Unity into your app as UWP doesn't suppose inter-app communication.

    Besides, even if that worked, the performance would be really bad as copying the data from the GPU to the CPU is a slow operation.