Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 3개 있습니다.)
개발 환경 구성: 707. 빌드한 Unity3D 프로그램을 C++ Windows Application에 통합하는 방법
; https://www.sysnet.pe.kr/2/0/13581

개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법
; https://www.sysnet.pe.kr/2/0/13584

닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신
; https://www.sysnet.pe.kr/2/0/13588




Unity3D - C# Windows Forms / WPF Application에 통합하는 방법

지난 글에서,

빌드한 Unity3D 프로그램을 C++ Windows Application에 통합하는 방법
; https://www.sysnet.pe.kr/2/0/13581

C++ Win32 응용 프로그램에서 어떻게 Unity3D 프로그램을 내장할 수 있는지 알아봤습니다. 위의 방법은, C# Windows Forms에서도 유사하게 써먹을 수 있습니다. 테스트 삼아 직접 해볼까요? ^^

이전 글에서, Unity3D 프로젝트를 Windows 대상으로 빌드하면 다음과 같은 내용을 가진 출력물이 나온다고 했는데요,

C:\temp\unity> tree /F
...[생략]...
│   My project.exe
│   UnityCrashHandler64.exe
│   UnityPlayer.dll
├───MonoBleedingEdge
│   ├───EmbedRuntime
│   └───etc
│       └───mono
│           ├───2.0
│           │   └───Browsers
│           ├───4.0
│           │   └───Browsers
│           ├───4.5
│           │   └───Browsers
│           └───mconfig
└───My project_Data
    ├───Managed
    └───Resources

닷넷에서도 역시 저 UnityPlayer.dll의 UnityMain 함수를 P/Invoke로 호출해 활용하면 그만입니다. 이를 위해 C# Windows Forms Application을 간단하게 하나 만들고,

using System.Runtime.InteropServices;

namespace WinFormsApp1;

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

    [DllImport("UnityPlayer.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "UnityMain")]
    public static extern int UnityMain(IntPtr hInstance, IntPtr hPrevInstance, string lpCmdLine, int nShowCmd);

    const int GWLP_HINSTANCE = -6;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IntPtr hInstFromHwnd = GetWindowLongPtr(this.Handle, GWLP_HINSTANCE); // https://www.sysnet.pe.kr/2/0/13307
        string cmd = $"-parentHWND '{this.Handle}'";
        UnityMain(hInstFromHwnd, IntPtr.Zero, cmd, 0); // hInstFromHwnd는 IntPtr.Zero로 줘도 무방
    }
}

보는 바와 같이 UnityMain을 DllImport로 연결한 다음 -parentHWND 문자열로 적절하게 Unity가 차지할 공간을 소유한 Window Handle로 채워 전달하면 됩니다.

소스코드는 저게 끝입니다. 남은 작업은, UnityPlayer.dll을 찾을 수 있도록 OutputPath, AppendTargetFrameworkToOutputPath를 조정해야 합니다. 예를 들어, Unity 결과물이 C:\temp\unity에 있다면 다음과 같이 csproj에 설정을 추가합니다.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
      
    <OutputPath>c:\temp\unity</OutputPath>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
  </PropertyGroup>

</Project>

그런데, 이렇게 해도 실행 시 이런 오류가 발생할 것입니다.

Data folder not found

Application folder:
c:/temp/unity
There should be 'WinFormsApp1_Data'
folder next to the executable

이름이 좀 특이하죠? 예제 윈폼 프로젝트의 이름이 "WinFormsApp1"인데 거기에 "_Data"가 붙어 있습니다. 그리고, Unity 예제 프로젝트의 이름이 "My Project.exe"인데, c:\temp\unity 하위에는 "My Project_Data"가 있습니다.

아하~~~ 그러니까, 우리가 만든 EXE 프로젝트가 아예 "My Project.exe"를 대체하는 것이군요. ^^ 따라서 출력 어셈블리 이름도 함께 바꿔야 합니다. 결국 다음과 같은 csproj 구성으로 빌드하면 됩니다.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!-- ...[생략]... --->
      
      <OutputPath>c:\temp\unity</OutputPath>
      <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
      <AssemblyName>My Project</AssemblyName>
  </PropertyGroup>

</Project>




WPF의 경우에도 Windows Forms와 방법은 같습니다. 단지 명시적인 HWND 윈도우 핸들을 주지는 않기 때문에 WindowInteropHelper를 이용해 알아낸 다음,

IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

UnityMain 호출을 거치면 됩니다.

using System;
using System.Runtime.InteropServices;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

        [DllImport("UnityPlayer.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "UnityMain")]
        public static extern int UnityMain(IntPtr hInstance, IntPtr hPrevInstance, [MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, int nShowCmd);

        const int GWLP_HINSTANCE = -6;
        const int SW_SHOWDEFAULT = 0x0a;

        public MainWindow()
        {
            InitializeComponent();
        }

        void EmbedUnity()
        {
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

            IntPtr hInstFromHwnd = GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
            string cmd = $"-parentHWND {hwnd}";

            UnityMain(hInstFromHwnd, IntPtr.Zero, cmd, SW_SHOWDEFAULT); // hInstFromHwnd는 IntPtr.Zero로 줘도 무방
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            EmbedUnity();
        }
    }
}

역시 csproj에서의 경로 설정도 잊지 마시고!

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>

      <OutputPath>c:\temp\unity</OutputPath>
      <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
      <AssemblyName>My Project</AssemblyName>
  </PropertyGroup>

</Project>

참고로, Windows Forms의 경우 모든 컨트롤이 Window Handle을 갖기 때문에 메인 윈도우의 일부에서 Unity를 포함시키는 것이 어렵지 않습니다. 반면, WPF는 Element들이 모두 비-윈도우 자원이기 때문에 Main Window 내에 포함하고 싶다면 WindowsFormsHost와 같은 방법을 경유해야 합니다.

Walkthrough: Hosting a Windows Forms Control in WPF
; https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-a-windows-forms-control-in-wpf

Walkthrough: Hosting a Windows Forms Control in WPF by Using XAML
; https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-a-windows-forms-control-in-wpf-by-using-xaml

따라서 대충 WindowsFormsHost 컨트롤을 하나 올려 두고,

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d" Loaded="Window_Loaded" 
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Button_Click"
                Grid.Column="0" />

        <WindowsFormsHost x:Name="host" Background="Yellow" Grid.Column="1">
            <wf:Panel x:Name="panel" />
        </WindowsFormsHost>
    </Grid>

</Window>

저 컨트롤의 Handle 값을 구해 UnityMain에 전달하면 됩니다.

IntPtr hwnd = host.Handle;

string cmd = $"-parentHWND {hwnd}";
UnityMain(IntPtr.Zero, IntPtr.Zero, cmd, SW_SHOWDEFAULT);

(첨부 파일은 이 글의 예제 코드를 포함>합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 3/26/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13545정성태1/30/20242147닷넷: 2212. ASP.NET Core - 우선순위에 따른 HTTP/HTTPS 호스트:포트 바인딩 방법
13544정성태1/30/20242177오류 유형: 894. Microsoft.Data.SqlClient - Could not load file or assembly 'System.Security.Permissions, ...'
13543정성태1/30/20242211Windows: 254. Windows - 기본 사용 중인 5357 포트 비활성화는 방법
13542정성태1/30/20242225오류 유형: 893. Visual Studio - Web Application을 실행하지 못하는 IISExpress - 두 번째 이야기
13541정성태1/29/20242329VS.NET IDE: 188. launchSettings.json의 useSSL 옵션
13540정성태1/29/20242474Linux: 69. 리눅스 - "Docker Desktop for Windows" Container 환경에서 IPv6 Loopback Address 바인딩 오류
13539정성태1/26/20242430개발 환경 구성: 703. Visual Studio - launchSettings.json을 이용한 HTTP/HTTPS 포트 바인딩
13538정성태1/25/20242543닷넷: 2211. C# - NonGC(FOH) 영역에 .NET 개체를 생성파일 다운로드1
13537정성태1/24/20242651닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성파일 다운로드1
13536정성태1/23/20242684닷넷: 2209. .NET 8 - NonGC Heap / FOH (Frozen Object Heap) [1]
13535정성태1/22/20242585닷넷: 2208. C# - GCHandle 구조체의 메모리 분석
13534정성태1/21/20242348닷넷: 2207. C# - SQL Server DB를 bacpac으로 Export/Import파일 다운로드1
13533정성태1/18/20242539닷넷: 2206. C# - TCP KeepAlive의 서버 측 구현파일 다운로드1
13532정성태1/17/20242447닷넷: 2205. C# - SuperSimpleTcp 사용 시 주의할 점파일 다운로드1
13531정성태1/16/20242427닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/20242278닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/20242186닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/20242335Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/20242246오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
13526정성태1/14/20242338닷넷: 2201. C# - Facebook 연동 / 사용자 탈퇴 처리 방법
13525정성태1/13/20242291오류 유형: 891. Visual Studio - Web Application을 실행하지 못하는 IISExpress
13524정성태1/12/20242359오류 유형: 890. 한국투자증권 KIS Developers OpenAPI - GW라우팅 중 오류가 발생했습니다.
13523정성태1/12/20242163오류 유형: 889. Visual Studio - error : A project with that name is already opened in the solution.
13522정성태1/11/20242302닷넷: 2200. C# - HttpClient.PostAsJsonAsync 호출 시 "Transfer-Encoding: chunked" 대신 "Content-Length" 헤더 처리
13521정성태1/11/20242379닷넷: 2199. C# - 한국투자증권 KIS Developers OpenAPI의 WebSocket Ping, Pong 처리
13520정성태1/10/20242160오류 유형: 888. C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....'
1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...