Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Problems running unity in a WPF project

Discussion in 'Windows' started by hyunjin_s_lim, Mar 13, 2023.

  1. hyunjin_s_lim

    hyunjin_s_lim

    Joined:
    Mar 9, 2023
    Posts:
    3
    hi. I'm a beginner just starting to learn coding.

    I'm working on adding a Unity 3D Earth to the screen in a WPF window right now.

    When you start a WPF project, like this image

    upload_2023-3-13_15-16-36.png

    window close button not showing

    upload_2023-3-13_15-17-39.png ← this

    and coudln't resize and move window.




    I want to display the earth screen on the 0th grid

    upload_2023-3-13_15-18-43.png



    My code is from the forum.


    Code (CSharp):
    1.     public partial class MainWindow : ThemedWindow
    2.     {
    3.  
    4.         [DllImport("User32.dll")]
    5.         static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
    6.  
    7.         internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
    8.         [DllImport("User32.dll")]
    9.         internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
    10.  
    11.         [DllImport("User32.dll")]
    12.         static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    13.  
    14.         private Process _process;
    15.         private IntPtr _unityHWND = IntPtr.Zero;
    16.  
    17.         private const int WM_ACTIVATE = 0x0006;
    18.         private readonly IntPtr WA_ACTIVE = new IntPtr(1);
    19.         private readonly IntPtr WA_INACTIVE = new IntPtr(0);
    20.  
    21.         //Frame p = MainWindow.Instance.floatingFrame;
    22.         Grid UnityContent;
    23.  
    24.         bool initialized = false;
    25.  
    26.         public MainWindow()
    27.         {
    28.             InitializeComponent();
    29.             // this.DataContext = new ViewModels.MainViewModel();
    30.             UnityContent = unityContent;
    31.             //MainWindow.Instance.MainWindowClosing += Application_Exit;
    32.  
    33.             System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    34.             dispatcherTimer.Tick += attemptInit;
    35.             dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    36.             dispatcherTimer.Start();
    37.         }
    38.         void attemptInit(object sender, EventArgs e)
    39.         {
    40.  
    41.             if (initialized)
    42.                 return;
    43.  
    44.             HwndSource source = (HwndSource)HwndSource.FromVisual(UnityContent);
    45.  
    46.             Debug.WriteLine("attempting to get handle...");
    47.  
    48.             if (source == null)
    49.             {
    50.                 Debug.WriteLine("Failed to get handle source");
    51.                 return;
    52.             }
    53.  
    54.             IntPtr hWnd = source.Handle;
    55.  
    56.             try
    57.             {
    58.                 _process = new Process();
    59.                 _process.StartInfo.FileName = "path";
    60.                 _process.StartInfo.Arguments = "-parentHWND " + hWnd.ToInt32() + " " + Environment.CommandLine;
    61.                 _process.StartInfo.UseShellExecute = true;
    62.                 _process.StartInfo.CreateNoWindow = true;
    63.  
    64.                 _process.Start();
    65.  
    66.                 _process.WaitForInputIdle();
    67.                 // Doesn't work for some reason ?!
    68.                 //hWnd = _process.MainWindowHandle;
    69.                 EnumChildWindows(hWnd, WindowEnum, IntPtr.Zero);
    70.  
    71.                 //unityHWNDLabel.Text = "Unity HWND: 0x" + unityHWND.ToString("X8");
    72.                 Debug.WriteLine("Unity HWND: 0x" + _unityHWND.ToString("X8"));
    73.  
    74.                 // TODO: rename. What are the Args?
    75.                 UnityContentResize(this, EventArgs.Empty);
    76.  
    77.                 initialized = true;
    78.             }
    79.             catch (Exception ex)
    80.             {
    81.                 MessageBox.Show(ex.Message + ".\nCheck if Container.exe is placed next to Earth and mooon.exe");
    82.             }
    83.         }
    84.  
    85.         private void ActivateUnityWindow()
    86.         {
    87.             SendMessage(_unityHWND, WM_ACTIVATE, WA_ACTIVE, IntPtr.Zero);
    88.         }
    89.  
    90.         private void DeactivateUnityWindow()
    91.         {
    92.             SendMessage(_unityHWND, WM_ACTIVATE, WA_INACTIVE, IntPtr.Zero);
    93.         }
    94.  
    95.         private int WindowEnum(IntPtr hwnd, IntPtr lparam)
    96.         {
    97.             _unityHWND = hwnd;
    98.             ActivateUnityWindow();
    99.             return 0;
    100.         }
    101.  
    102.         private void UnityContentResize(object sender, EventArgs e)
    103.         {
    104.             MoveWindow(_unityHWND, 0, 0, (int)UnityContent.Width, (int)UnityContent.Height, true);
    105.             Debug.WriteLine("RESIZED UNITY WINDOW TO: " + (int)UnityContent.Width + "x" + (int)UnityContent.Height);
    106.             ActivateUnityWindow();
    107.         }
    108.  
    109.         // Close Unity application
    110.         private void ApplicationExit(object sender, EventArgs e)
    111.         {
    112.             try
    113.             {
    114.                 _process.CloseMainWindow();
    115.  
    116.                 Thread.Sleep(1000);
    117.                 while (!_process.HasExited)
    118.                     _process.Kill();
    119.             }
    120.             catch (Exception)
    121.             {
    122.  
    123.             }
    124.         }
    125.  
    126.         private void UnityContentActivate(object sender, EventArgs e)
    127.         {
    128.             ActivateUnityWindow();
    129.         }
    130.  
    131.         private void UnityContentDeactivate(object sender, EventArgs e)
    132.         {
    133.             DeactivateUnityWindow();
    134.         }
    135.     }


    I've been stuck on this problem for two days.
    Is there anyone who can help me? upload_2023-3-13_15-16-36.png
     

    Attached Files:

  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,172
    Does it show up if you don't launch the Unity content in the app?
     
  3. hyunjin_s_lim

    hyunjin_s_lim

    Joined:
    Mar 9, 2023
    Posts:
    3
    First of all thanks for the reply.
    The above problem was solved by myself. I solved it with <WindowsFormsHost> tag and handler. thank you.

    ↓ here is run window
    upload_2023-4-5_15-9-37.png