Search Unity

OnRectTransformDimensionsChange not called if Canvas NOT PixelPerfect?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by TimHeijden2, Aug 19, 2016.

  1. TimHeijden2


    Aug 11, 2016
    I was working on a UI element that can be dragged around by other devs while not in play mode. Whenever the element is moved, some code needs to be called.

    For this, I wanted to use the OnRectTransformDimensionsChange() method. As long as the canvas is pixelperfect this works fine! However, when the canvas isn't pixel perfect the function isn't called anymore!

    I was looking around in the UI source, but the only reference I found where pixelperfect is used is in the graphic's PixelAdjustPoint function.

    Is there a logical reason for OnRectTransformDimensionsChange() not being called? Am I missing something?

    Note: For now I just fixed it by checking the rectTransform values in the Update() but that doesn't feel very clean :)
  2. eses


    Feb 26, 2013

    Hi there,

    not sure what is going on, never used that command, but;

    Just tested this, and it works just fine with or without "Pixel Perfect".

    Also, you mention that you are monitoring if element is *moved* then this command is not for you, as it basically seems to fire (also based on name) when rectTransform is resized...

    There's also Transform related command, transform.hasChanged. But it also fires when canvas resizes... so you have to check if your RectTransform / Transform has moved at all. Not a good solution maybe.

    Then there is the simplest way, just track the RectTransform LocalPosition, store the previous frame position, then compare them, and your change is only relative to changes happening in parent space, screen scaling doesn't matter.

    Code (csharp):
    2. if (Vector3.Distance(rtra.localPosition,lastPosition) > 0.01f) Debug.Log("RectTransform Position Changed!");
    3. lastPosition = rtra.localPosition;

    The resizing of your RectTransform will in some cases trigger the above "has moved" code, just because how the position of RectTransform is calculated.

    If you set pivot to center, then move any corner, pivot moves = RectTransform moves.

    But if you have pivot in lower left corner, then move any other corner, it won't trigger.
  3. levykin


    Nov 7, 2013

    Thanks man! This helped me to understand, when "Pixel Perfect" is enabled, OnRectTransformDimensionsChange is called in much more cases.