- Home /
Get UI Button dimensions in game via script C#
Hi, I tried to find how to get dimensions of UI button but I cannot find how. We can check rect information but they are saying dimensions from unity editor not from game (devices have got different resolution so they are differrent).
Coce i used to get it is: print ("rect = " + movementJoyStick.GetComponent>RectTransform>().rect); At the moment I'm using some math to finally get them but I'm there must be simple way to get those information. When I'm testing them in editor they are 370 but from print function they are always 500 in Update function.
Answer by flashframe · Mar 18, 2016 at 07:19 PM
Try multiplying the width/height by the ScaleFactor of your Canvas to get the adjusted size
http://docs.unity3d.com/460/Documentation/ScriptReference/Canvas-scaleFactor.html
Hi @flashframe, thanks for respond. However what i I'm using UI Scale $$anonymous$$ode: Scale With Screen Size. I actually need to know if my finger contains Rect of UI Image but they are always the same size in RectTransform so in script I get true value event if my finger is over that Rect (f.e. Rect(0,0,500,500) in editor but on resized screen in game it's Rect(0,0,370,370)) I tried also to use ray from camera but the same result, it's taking those values.
You might need to use RectTransformUtility.RectangleContainsScreenPoint
http://docs.unity3d.com/ScriptReference/RectTransformUtility.RectangleContainsScreenPoint.html
(ps. Scale Factor and Canvas Scaler are different things.)
Your answer
Follow this Question
Related Questions
Changing anchor positions on UI? 0 Answers
Help making multiple buttons do a specific set of actions in any order pressed 1 Answer
UIButton highlighted color not working 0 Answers
scrollview buttons are overlapping 0 Answers
Help check click out of UI 2 Answers