Search Unity

RectTransform.rect contains a point, despite world corners are zero.

Discussion in 'UGUI & TextMesh Pro' started by Ziya, Jun 21, 2017.

  1. Ziya

    Ziya

    Joined:
    Aug 30, 2013
    Posts:
    65
    I have:
    1. GameObject with the Canvas component. Let's call it just "Canvas". Render Mode set to World space.
    2. Main camera set to orthographic.
    3. Code to get:
    3.1. World Corners of RectTransform of Canvas.
    3.2. Local position of mouse on RectTransform.
    3.3. Bool showing if RectTransform contains the position of mouse.

    Canvas is placed inside a parent GameObject. I change scale of the parent and values of 3 fields change accordingly. 3 fields are listed above in the points 3.1, 3.2, 3.3.

    Issue:
    Values of the 3 fields change properly, when changing the scale of the parent except one case. One case is when I set the scale of the parent to Vector.zero in edtior at runtime. When I do it, values show accordingly to the scale of the next child of the parent which is in my case is Canvas. Briefly, RectTransform.InverseTransformPoint doesn't consider zero scale, it just passes it next to first non zero scale child.

    To reproduce:
    1. Create Canvas.
    2. Set Canvas Render mode to world space.
    3. Set camera to orthographic.
    4. Set Canvas to parent.
    5. Scale Canvas to obtain some visible amount of screen.
    6. Add code to check, if Canvas's RectTransform.rect contains a mouse click to show it by Debug.Log.
    7. Set parent's scale to zero.
    8. Run the game.
    9. Click on Canvas as if it was visible when parent's scale was Vector.one.
    10. You see Debug.Log shows true.

    Is it a bug? If not, how to fix it?