Unity3D program is embedded into WPF program and realizes communication

Demo resources have been uploaded: Embedding the Unity3D program in the WPF program and communicating resources-CSDN Library

To embed a Unity3D program into a WPF program and implement communication, you need to follow the following steps:

1. First, you need to create a new project in Unity3D and export it as a Windows Standalone application. In Unity, select File > Build Settings, then select PC, Mac & Linux Standalone, click Switch Platform, and finally click Build.

2. Next, add a control named WindowsFormsHost to the WPF project. This will allow you to embed Windows Forms controls in WPF. In order to achieve this, you need to add references to WindowsFormsIntegration and System.Windows.Forms in your WPF project.

3. In the WPF project, create a UserControl named UnityContainer. In this UserControl, add a WindowsFormsHost control and set its name to _host.

<UserControl x:Class="WpfUnityIntegration.UnityContainer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <WindowsFormsHost x:Name="_host" />
    </Grid>
</UserControl>

4. In the UnityContainer's code file, add the following code to load the Unity application:

using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WpfUnityIntegration
{
    public partial class UnityContainer : UserControl
    {
        private UnityApp _unityApp;

        public UnityContainer()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            Unloaded += OnUnloaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            _unityApp = new UnityApp();
            _host.Child = _unityApp;
        }

        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            _unityApp.Dispose();
        }
    }
}

5. Create a new class named UnityApp, which inherits from System.Windows.Forms.Control. In this class, add the following code to start and shut down the Unity application:

using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WpfUnityIntegration
{
    public class UnityApp : Control
    {
        private Process _process;

        public UnityApp()
        {
            StartUnityApp();
        }

        private void StartUnityApp()
        {
            _process = new Process
            {
                StartInfo =
                {
                    FileName = "path/to/your/unity/app.exe",
                    Arguments = "-parentHWND " + Handle.ToInt32() + " " + Environment.CommandLine,
                    UseShellExecute = true,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden
                }
            };

            _process.Start();
        }

        protected override void Dispose(bool disposing)
        {
            if (_process != null)
            {
                _process.Kill();
                _process.Dispose();
                _process = null;
            }

            base.Dispose(disposing);
        }
    }
}

Make sure to replace path/to/your/unity/app.exe with the actual path to the Unity application you exported in step 1.

6. Now, you can add the UnityContainer control in the main window of the WPF application to display the Unity3D program in WPF.

As for implementing communication, you can use mechanisms such as TCP/IP, Named Pipes, or other IPC (Inter-Process Communication) to communicate between WPF and Unity applications. This will involve creating clients and servers in Unity and WPF applications so that they can send and receive messages 

Guess you like

Origin blog.csdn.net/u013543846/article/details/130850057