Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by FiliBu5ter · Oct 08, 2016 at 02:49 PM · uibuttonevent triggeringtooltip

Creating a Tooltip when hovering over a UI Button?

Is it possible to create tooltips while hovering over a UI Button using an Event Trigger. I've looked around, but all the information I found was for older version of Unity and GUI.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by J4ra · Oct 08, 2016 at 04:06 PM

Hi, this tutorial should work in "new" Unity Ui system https://www.youtube.com/watch?v=GFNKjJ-I6rg

If you want your tooltip to display next to the cursor add this behaviour to the TooltipText GameObject:

 public Canvas myCanvas;
 public Vector3 offset;
 // Update is called once per frame
 void Update () {
     transform.position = Input.mousePosition + offset;
 }

In the inspector set myCanvas and tweak the offset.

Hope that helps,

Bye

Comment
Add comment · Show 4 · Share
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
avatar image FiliBu5ter · Oct 09, 2016 at 07:31 AM 0
Share

Thanks for the response, after some tweaking I managed to get it to work, though I'm still not sure why the canvas reference was needed.

avatar image J4ra FiliBu5ter · Oct 09, 2016 at 01:56 PM 0
Share

Oh sorry, the canvas reference is not needed. I had a little different version of this script where the reference was necessary. So get rid of that line.

avatar image Fyndhal · Feb 19, 2018 at 07:11 PM 0
Share

Hi j4ra, thanks for posting this comment. Question: How can I constrain the tooltip so that it doesn't leave the bounds of the canvas?

avatar image GafferSystems Fyndhal · Sep 05, 2018 at 05:03 PM 0
Share

Several ways to do this: $$anonymous$$ost flexible: Calculate if the position + the width of the box is outside each of the 4 borders, and move it inside.

Fastest: have different offset variable on each element that has tooltip enabled. An offset X and offset Y would be sufficient.

avatar image
1

Answer by celtcraftgames · Apr 27, 2020 at 09:55 AM

This will display a tooltip with a Y offset, that will stay within the bounds of the screen if anyone else is interested in the best solution. Just attach to your tooltip object:

 public class Tooltip : MonoBehaviour
 {
     public bool IsActive = true;
 
     Camera cam;
     Vector3 min, max;
     RectTransform rect;
     float offset = 10f;
 
     // Start is called before the first frame update
     void Start()
     {
         cam = Camera.main;
         rect = GetComponent<RectTransform>();
         min = new Vector3(0, 0, 0);
         max = new Vector3(cam.pixelWidth, cam.pixelHeight, 0);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (IsActive)
         {
             //get the tooltip position with offset
             Vector3 position = new Vector3(Input.mousePosition.x + rect.rect.width, Input.mousePosition.y - (rect.rect.height / 2 + offset), 0f);
             //clamp it to the screen size so it doesn't go outside
             transform.position = new Vector3(Mathf.Clamp(position.x, min.x + rect.rect.width/2, max.x - rect.rect.width/2), Mathf.Clamp(position.y, min.y + rect.rect.height / 2, max.y - rect.rect.height / 2), transform.position.z);
         }
             
     }
 }
 
Comment
Add comment · Share
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
avatar image
1

Answer by unity_Hc_fAdX4wuuitA · Mar 07, 2021 at 04:55 PM

I'm digging up an old subject but I went through this post while I was looking for a way to add tooltip in unity but I didn't find any answer so I tried mine, it works great so I thought I could share :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class SCR_21_ToolTips : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     public GameObject gmo_toolTip;
 
     void Start()
     {
         // I added this in case I forgot to set the tooltip object
         if (gmo_toolTip!= null)
         {
             gmo_toolTip.SetActive(false);
         }
     }
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         // Same here
         if (gmo_toolTip!= null)
         {
             gmo_toolTip.SetActive(true);
         }
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         // and same here
         if (gmo_toolTip!= null)
         {
             gmo_toolTip.SetActive(false);
         }
     }
 }


   

Now all you have to do is make some text or whatever you want as tooltip, drag this script over your button and then set the "gmo_toolTip" value in the inspector :

alt text

I was lazy so I used buttons to make the tooltip boxes (goes fast with a shape, background and text inside).

Hope this could help someone ;-)


tooltip.png (12.9 kB)
Comment
Add comment · Share
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

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

8 People are following this question.

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

Related Questions

How do I asign this script to a button successfully? 1 Answer

UI Buttons to work continuously when pressed and held for sometime 1 Answer

UI Button event triggers don't work on mobile but work in eaditor? 0 Answers

My function is called twice on single click 2 Answers

How to simulate GetKeyDown for a UI button 2 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