Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Masea1 · Dec 29, 2019 at 04:36 PM · guipositionanchortooltip

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

222 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Anchor The Objects. 1 Answer

zoom problem with GUI Box that follows a character 1 Answer

Dynamic Resizing of tooltip to match content? 1 Answer

Using GUI.tooltip to avoid executing code over GUI elements 3 Answers

GUI position not updated on build & run 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges