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

Hello forum,

This post is loosely related to another one i did in the winter ( Integrating Unity inside of an existing UWP app. )

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

            var bridge = new ScriptingBridge();
            var appCallbacks = AppCallbacks.Instance ?? new AppCallbacks();
            appCallbacks.SetBridge(bridge);
            appCallbacks.SetSwapChainPanel(SwapChainHolder.SwapChain);
            appCallbacks.SetCoreWindowEvents(Window.Current.CoreWindow);
            appCallbacks.InitializeD3DXAML();
            appCallbacks.RenderingStarted += AppCallbacks_RenderingStarted;

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

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++?

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)

I’ve started exploring the API a bit

var mmap = MemoryMappedFile.CreateNew("hFileMap", 132_710_400, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.DelayAllocatePages, System.IO.HandleInheritability.Inheritable);
        var view = mmap.CreateViewAccessor();

        var hFileView = view.SafeMemoryMappedViewHandle.DangerousGetHandle();

        var hTex = Texture2D.CreateExternalTexture(3840, 2160, TextureFormat.ARGB32, false, false, hFileView);

        //Graphics.SetRenderTarget( <but somehow but this hTex in?> )

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.