How to get Window/Screen resize event notification in runtime UITK

After googling, it seems right now Unity doesn’t provide a general Window/Screen resize event for user to register callback, and I have used following successively in UGUI to response to resizing event.

public class UICanvas : UIBehaviour
{
    protected override void OnRectTransformDimensionsChange()
    {
        base.OnRectTransformDimensionsChange();
        // do our resizing logic
    }
}

I am upgrading old UGUI to runtime UITK, and wandering if similar functionality exist in UITK to get Window/Screen resize event.

PS, I know the solution of polling Screen status and check for changes in Update which i don’t like, and want to know alternative more elegant solutions.

Thanks.

You can use this callback: Unity - Scripting API: GeometryChangedEvent

Have you try it, can you provide some code examples?

It works the same as any other event in UI Toolkit. Register it to the visual element you want to be informed of being resized, go from there.

It works just as i wanted, Thanks.

root = uidoc.rootVisualElement;
root.RegisterCallback<GeometryChangedEvent>(e => OnScreenSizeChanged());
1 Like