Question by
student234ktk · Apr 13, 2019 at 07:17 AM ·
uibounds
How to Keeping UI element Within Screen Boundaries ?
I made coins counter on upper right corner of screen. The problem is when coins increase the the coin button move outside of the screen. I spend day to fix it but fail. I can bound gameobject with transform property but UI element have RectTransform property. If someone teach me how to bound UI element inside screen, I will be thankful to him.
public Button icon_key_topbar;
public Button innerButton;
public Text text;
float maxX = 175.5f;
float innermaxX = 32.5f;
float textMaxX = 50f;
void Update()
{
Vector2 position = icon_key_topbar.GetComponent<RectTransform>().localPosition;
Vector2 innerposition = innerButton.GetComponent<RectTransform>().localPosition;
Vector2 textposition = text.GetComponent<RectTransform>().localPosition;
if (position.x > maxX)
{
position.x = maxX;
}
if (innerposition.x > innermaxX)
{
innerposition.x = innermaxX;
}
if (textposition.x > textMaxX)
{
textposition.x = textMaxX;
}
icon_key_topbar.GetComponent<RectTransform>().localPosition
= position;
innerButton.GetComponent<RectTransform>().localPosition
= innerposition;
text.GetComponent<RectTransform>().localPosition
= textposition;
Debug.Log(position.x + " " + position.y + " " + position.y);
Debug.Log(innerposition.x + " " + innerposition.y + " " + innerposition.y);
Debug.Log(textposition.x + " " + textposition.y + " " + textposition.y);
unity.png
(72.2 kB)
Comment
Your answer
Follow this Question
Related Questions
Using GetAxis with UI Buttons 0 Answers
Screen layout? 1 Answer
Set Unity UI scroll view elements height properly in different resolutions 0 Answers
Mask(Matte) not working on Opengl mode(pc platform) 1 Answer
How to render text on top of the objects that are generated on the map using a script? 1 Answer