GetNativeHandle(GameWindow)
Obtains the native Win32 window handle of the specified window. This is an extension method for GameWindow.
Read time 2 minutesLast updated 20 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Windowing.Windows
- Assembly: UnityEngine.WindowsWindowingModule
public static nint GetNativeHandle(this GameWindow window)
Parameters
The GameWindow instance this extension method is implicitly called on.
Returns
Type | Description |
|---|---|
| IntPtr | The native |
Remarks
Windows identifies each top-level window with a window handle, or . Use this method to obtain the that corresponds to a GameWindow so that you can pass it to a native plugin, or to a Win32 function that operates on a window, such as .
HWNDHWNDSetWindowPosNotes:
- This method is synchronous and returns the handle immediately.
- The handle is valid only for as long as the window exists. Query the handle each time you need it rather than caching the returned value, because Unity invalidates the handle when it destroys the window.
- Modifying the window directly through the Win32 API can put the window out of sync with the state that Unity tracks. Where an equivalent GameWindow method exists, such as GameWindow.SetTitle or GameWindow.MoveTo(Vector2Int), use that method instead.
- In Development builds, this method throws an if the GameWindow is no longer valid. Use GameWindow.IsValid to check the window before you call this method.
InvalidOperationException - This method is available only on Windows.
Examples
using System;using System.Runtime.InteropServices;using UnityEngine;using UnityEngine.Windowing;using UnityEngine.Windowing.Windows;// Attach this script to a GameObject in the scene. It runs only in a Windows player.public class GetNativeHandleExample : MonoBehaviour{ [DllImport("user32.dll")] static extern bool FlashWindow(IntPtr hWnd, bool bInvert); void Start() { IntPtr hwnd = GameWindow.Main.GetNativeHandle(); if (hwnd == IntPtr.Zero) { Debug.LogWarning("The main window has no native handle."); return; } // Pass the handle to a Win32 function to flash the window in the taskbar FlashWindow(hwnd, true); }}