Mostly to make this thread appear in google search results
Spend like 4 hours for this trying understand why OnRectTransformDimensionsChange is not being called.
So for all who try to do this and it does not work, you should add
[ExecuteInEditMode]
to make it work in editor, any tips i found in google does not say this and from my point of view this is something you need in editor and rarely during execution, so my thoughts it should have work in editor out of the box and adding “ExecuteInEditMode” is not obvious.