.NET framework and Mono interop

Hi,

I have the following architecture of my project

Winforms app (.NET framework 4.7.2) → P/Invoke unity to a handle via UnityMain function (2nd option from the following link Unity - Manual: Integrating Unity into Windows applications)

This works great, but now I want to access from my winforms the data stored in the dlls unity built from my scripts (Assembly-CSharp.dll) using mono backend.

I can reference my dll but all the data there is null (it seems like 2 dlls are loaded into the process memory, 1 by mono and 1 by .net framework)

I guess I can use IPC methods like shared memory or tcp sockets, but maybe there’s an easier way
Is it possible to have an easy way to interop between mono and .net framework in the same process ?

Thanks, Benny

I am also interested in this, I am able to launch Unity from a Winforms app, but Unity takes over at that point and no more code is executed in the WinForms app. I’m not sure the -parentHWND command is working correctly.

I am calling UnityMain like this:
unityMain(IntPtr.Zero, IntPtr.Zero, "-parentHWND " + this.Handle.ToInt32() + " " + Environment.CommandLine, 10);

The delegate is declared as such:
private delegate int UnityMainDelegate(IntPtr hInstance, IntPtr hPrevInstance, [MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, int nShowCmd);

After resolving this issue, I would also be interested in what the intended approach is for the app to communicate with Unity beyond the initial launching. Maybe through a native plugin? I watched the video on Youtube for Unity as a Library regarding Android and IOS, but haven’t found much information on working from a Win32 app.

Thanks!

You could do Marshal.GetDelegateForFunctionPointer() and Marshal.GetFunctionPointerForDelegate() so you could call methods from .NET to Mono and vice versa directly. However, you’d still need a native plugin to do the bootstrapping.

Yes, Unity will take over whichever thread you call UnityMain from. If you want to retain your thread, spawn a new thread and call UnityMain from there

Perfect, that is working.

I was also able to get the native plugin working as a way to send commands from the form to Unity, by passing delegates to methods from the Monobehaviour on the Unity side into the dll as UnmanagedFunctionPointers and then calling those from the form on the main thread to queue up commands. Those commands then get consumed on the Unity thread.

Thanks for the help!