Is there any reason you can’t make the button a child of the panel? If so its pretty easy to position it in the bottom
RectTransform buttonRect = GameObject.Find("Button").GetComponent<RectTransform>();
//This puts all 4 anchor points in the bottom left
buttonRect.anchorMax = Vector2.Zero;
buttonRect.anchorMin = Vector2.Zero;
float paddingX = 10; // this is how far from the bottom of the panel the button should be
float paddingY = 10; // this is how far from the left of the panel the button should be
buttonRect.sizeDelta = new Vector2(WidthofButton, HeightofButton);
buttonRect.offsetMin = new Vector2(paddingX,paddingY);
Be careful of using .transform with UI elements that have a RectTransform. Since a RectTransform inherits from Transform i think Button.transform.position is just setting variables in the RectTransform of that button. However, all those variables change meaning depending on where the anchor points are set.
I’d read up on RectTransform and its variables here:
As well as various Unity tutorials on RectTransforms.
If the button can’t be the child of the Panel. You can get the Panel’s RectTransform and use functions like
panelRectTransform.GetWorldCorners(); to get its absolute position
Thanks for your reply and no I can’t make the buttons a child of the panel because the buttons are scrollable. They are child of an empty gameobject that goes up or down according to mouse/touch movement. The panel indicates the the area in which the buttons are visible.
So basically I only need to get the bottom y value of the panel.
If I have that I can compare that to the lowest button position. If the position y of the lowest button is equal or bigger than the bottom y position of the panel that means you have scrolled all the way down in the list.
It’s really hard to find good info on this matter. Probably because most people use the standard scrollview.
RectTransformrt = goToolTipPanel.GetComponent<RectTransform>();
Vector3[] worldCorners = new Vector3[4];
RectTransformrt.GetWorldCorners(worldCorners);
World Corners are clockwise starting at the BottomLeft.
worldCorners[0] == bottom left
worldCorners[1] = Top Left
worldCorners[2] == Top right
worldCorners[3] = bottom right Pretty sure I got this info from a Unity Video tutorial
Now Assuming your Canvas is a the default Canvas that gets created and your Button is a child of that cavnas:
RectTransform buttonRect = GameObject.Find("Button")..GetComponent<RectTransform>();
// This sets the anchors of the Button to the top left of the screen.
buttonRect.anchorMin = new Vector2(0.0f, 1.0f);
buttonRect.anchorMax = new Vector2(0f, 1.0f);
// this sets the pivot ("Center") of the button to its bottom left
//otherwise the middle of the button would appear at the Panel's bottom left point.
buttonRect.anchoredPosition = new Vector2(0.0f, 0.0f);
// Now set the button's position to the Panel's Bottom left corner
buttonRect.position = worldCorners[0];
If the button is a child of another UI element its slighty more complicated… But you just set that parent object to where I said… and Make sure the button is positioned in the bottom left of that parent.
buttonRect.position.y is the y value in local space. In this case it will be relative to the parent’s transform.
if worldCorners is the corners of some OTHER object than the button and you are just checking if the button is below this object in world space;
Vector3[] buttonWorldCorners = new Vector3[4];
buttonRect.GetWorldCorners(buttonWorldCorners);
// then your code:
// Note this means the button is *above* the other object
if (buttonWorldCorners[0].y >= worldCorners[0].y )
It might help if you said why you needed the panels topY and botY position in worldSpace. (Original question). Are you trying to design a ToolTip Box that can pop up near various objects?
No just a standard scroll menu. The buttons are actually queque objects that are added when an object is added to a list(queque). Visually you could compare it to the cards list displayed in Hearthstone.