- Home /
Tooltip Align and Position
Hello guys. I've been struggling with tooltip aligning and positioning lately and I need your help since anchor and position stuff could be really hard to understand in the beginning.
To begin with, I have a Hover script that I tie to the components which I want them to have tooltips. And I basically set the texts that will be shown inside the tooltip in the Inspector. Hover script refers to the tooltip when hovered, which the tooltip then displays the texts as well as scaling itself to fit the content. This is all working correctly (though the scaling sometimes doesn't refresh, it is another story). All the mess is starting from now on: I have also another variable in Hover script which is for the align of the tooltip such as top-left, bottom-left and a few more. What I want to achieve is that the tooltip should align itself according to that variable in Hover script. Let's say, it is set to top-left meaning I'll see the tooltip at the top-left of the object I am hovering. But, of course not over it but near it.
This is the positioning code inside the Tooltip script occurs when something is hovered:
public void SetPos(TooltipAlign align, RectTransform rectTransform) {
Vector2 newPos = Vector2.zero;
switch (align) {
case TooltipAlign.BottomLeft:
this.rectTransform.anchorMin = Vector2.one;
this.rectTransform.anchorMax = Vector2.one;
this.rectTransform.pivot = Vector2.one;
newPos.x = rectTransform.position.x - spacing;
newPos.y = rectTransform.position.y - spacing;
break;
case TooltipAlign.TopLeft:
this.rectTransform.anchorMin = new Vector2(1, 0);
this.rectTransform.anchorMax = new Vector2(1, 0);
this.rectTransform.pivot = new Vector2(1, 0);
newPos.x = rectTransform.position.x - spacing;
newPos.y = rectTransform.position.y + rectTransform.rect.height + spacing;
break;
case TooltipAlign.BottomRight:
this.rectTransform.anchorMin = new Vector2(0, 1);
this.rectTransform.anchorMax = new Vector2(0, 1);
this.rectTransform.pivot = new Vector2(0, 1);
newPos.x = rectTransform.position.x + rectTransform.rect.width + spacing;
newPos.y = rectTransform.position.y - spacing;
break;
case TooltipAlign.TopRight:
this.rectTransform.anchorMin = Vector2.zero;
this.rectTransform.anchorMax = Vector2.zero;
this.rectTransform.pivot = Vector2.zero;
newPos.x = rectTransform.position.x + rectTransform.rect.width + spacing;
newPos.y = rectTransform.position.y + rectTransform.rect.height + spacing;
break;
}
this.rectTransform.position = newPos;
}
rectTransform is Rect Transform of the hovered object. And spacing is 1f for, as you could guess, spacing purposes.
This partially works but not in all scenarios where the hovered objects' anchors and pivots may vary.
By the by, should I be using transform while setting the position of an UI object or rectTransform is already a go-to? Or are they entirely the same anyway?
Any idea to make this work for all scenarios and maybe make it cleaner? Thanks in advance.
Your answer
![](https://koobas.hobune.stream/wayback/20220612220749im_/https://answers.unity.com/themes/thub/images/avi.jpg)