Search Unity

Get size/position of a panel?

Discussion in 'UGUI & TextMesh Pro' started by carking1996, Sep 3, 2014.

  1. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Is there a way to get the size/position of a panel of GUI?
     
  2. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    anchoredPosition and sizeDelta should do it
     
  3. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    ^ That, or RectTransform.rect.
     
  4. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Is there a way to get the screen space position? As RectTransform.rect is local(I want to use rect.contains with the mouse on it).
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    You can use http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html on the position of the RectTransform (not anchoredPosition) or one of the corners (there are methods on RectTransform to get the corners).

    We can't provide the whole rect combined in screen space since it may be rotated, so it may not make sense.
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    That's true, but what about providing stuff to get the 'bounds' rather than the rect? Same as is done for any other renderer. For non-rotated objects it should be the same as the rect, and for rotated objects it would still be doing something useful.
     
  7. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,609
    Yes, none of my UI is rotated so something like that would be useful.
     
    rakkarage likes this.