RegisterDestructionCallback(Action<GameWindow>)
Registers a callback that is invoked when the window is destroyed.
Read time 1 minuteLast updated 10 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Windowing
- Assembly: UnityEngine.WindowingModule
public void RegisterDestructionCallback(Action<GameWindow> callback)
Parameters
The callback to invoke when the window is destroyed, with the GameWindow being destroyed as its parameter.
Remarks
The callback runs while the window is still valid, which makes it the right place to clear any reference you hold to this GameWindow. After the callback returns, the instance is no longer valid and you must not use it.
This fires only for secondary windows. The main window can't be destroyed, so a callback registered on it never runs. Closing the main window quits the application instead.
Examples
using UnityEngine;using UnityEngine.Windowing;public class WindowingTest : MonoBehaviour{ GameWindow secondaryWindow; private void Start() { var settings = new GameWindowCreationSettings( title: "Secondary Window", width: 800, height: 600, position: new Vector2Int(0, 0), cameraDisplayIndex: 1, displayInfo: GameWindow.Main.GetDisplayInfo() ); var op = GameWindowManager.Create(settings); op.completed += (AsyncOperation o) => { secondaryWindow = ((CreateWindowAsyncOperation)o).window; // Register the callback after the window exists secondaryWindow.RegisterDestructionCallback(WindowDestroyedCallback); }; } private void WindowDestroyedCallback(GameWindow window) { Debug.Log($"Window destroyed: {window.GetTitle()}"); secondaryWindow = null; }}