How to detect if RectTransform size has changed without implementing Update method?

Is there a message or event that I can subscribe to, to be able to detect RectTransform size changes without implementing an Update method and check if it has changed in every single frame?

My script may be attached to too many game objects and I don’t want them all to get into the update queue of Unity just for this purpose.

1 Like

I found a perfect solution (with help of my best friend) and it’s a built-in feature by Unity.

UIBehaviour.OnRectTransformDimensionsChange:
https://docs.unity3d.com/ScriptReference/EventSystems.UIBehaviour.OnRectTransformDimensionsChange.html

Example code:

using UnityEngine;
using UnityEngine.EventSystems;

class MyScript : UIBehaviour
{
    RectTransform RectTransform;

    protected override void Awake() => RectTransform = (RectTransform)transform;

    protected override void OnRectTransformDimensionsChange() => print(1);

    void OnGUI()
    {
        if (GUILayout.Button("Set rect width to 100"))
        {
            print(0);
            RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
            print(2);
        }
    }
}

Observed console output when clicked the button:
0
1
2