Search Unity

Multi-use flexible tooltip with examples and code

Discussion in 'UGUI & TextMesh Pro' started by drHogan, Sep 30, 2015.

  1. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi guys,

    last few days i have been struggling with making a tooltip that could resize according to the contents, didn't go out of screen, and allowed to insert multiple lines of text.

    http://www.hammerandravens.com/multi-use-tooltip-system-in-unity3d/

    Here's the result of my effort (Includes the C# code), feel free to use it and if you find something that can be further improved please let me know! (on field tests it performed very well already)

    Cheers!
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Interesting effort @drHogan will see about adding it as another tooltip alternative in the UI Extensions project, if that's ok with you :D
     
  3. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Yeah no problem for me!
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Thanks for posting this :). I'm working on the built in support for it and its nice for me to see how you the users are implementing tooltips to get a better understanding of a common usage.
     
    jcredible and SimonDarksideJ like this.
  5. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    No prob phil, glad to help! I think the main features i implemented are among the most needed basic ones, i.e. easily resizable, not going out of screen and with a certain flexibility independent from screen size and resolution. Then of course thousands of other minor things can be nice and handy, but at least for us this one does already almost everything we need (i might add some fancier animation when i have time, but that's it).

    Cheers!
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Yeah, it's certainly one of the best tooltip implementations I've seen to date.

    Fantastic looking game as well BTW :D
     
  7. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Thanks a lot Simon! ;)
     
  8. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Quick Q @drHogan
    Putting the tooltip in the UI Extensions project, but following your article for implementation, what are you using to activate/deactivate the tooltip?
    Guessing you are using another script to set and unset the tooltip based off the pointer.
     
  9. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi Simon, yes, I just rewrote a manager for that. The former version used to attach EventTrigger to the object and register a OnPointerEnter callback that had of the UIElement GameObject as parameter.

    In the new version basically I have a singleton manager for the mouse pointer with a method that, called from the UI scripts attaches a short script to a given object and assigns it a OnPointerEnter and OnPointerExit callback (from the calling script).
    The short script reimplements IPointerEnterHandler and IPointerExitHandler.

    OnPointerEnter calls on SetTooltip(gameobject.name) and OnPointerExit on HideTooltip();

    Cheers!
     
  10. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
  11. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Thanks, will check that out. #AwesomeSauce
     
  12. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Quick Q @drHogan
    Do you have a quick sample project using your three scripts (still curious why it's three).

    Because your Pointer and Handler scripts do not call
    "OnPointerEnter calls on SetTooltip(gameobject.name) and OnPointerExit on HideTooltip();"

    Unless you are configuring it through the editor manually?
     
  13. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    I don't have a project as the thing is part of my whole game, but that's a scheme of the management :

    Singleton main UI manager (has the public variable to access the Tooltip) - TacticalGUIManager.
    SingleTon Mouse manager (one of the three scripts) - MousePointerHandler

    The tooltip is a gameobject in the scene with it's own script. On init it does TacticalGUIManager.tgm.tooltipScript=this;

    All done through code, so for example I am in the

    ProvincePanelManager script. I need to have the CloseButton of the panel accessing the tooltip. In the init of the panel i use MouserPointerHandler.mph.AddMouseTracker(CloseButton.gameObject,OnPointerEnter,OnPointerExit); so that the button gets the script and the callbacks

    Then still in ProvincePanelManager, I implement
    Code (CSharp):
    1. OnPointerEnter(GameObject hovered) {
    2. switch(hovered.nam){
    3. case "CloseButton" :
    4. TacticalGUIManager.tgm.tooltipScript.SetTooltip("Close the panel");
    5. break;
    6. }
    7. }
    I keep the three scripts separate because it looks cleaner to me.

    One handles the tooltip from inside.
    One gets attached to the UIElements
    One makes handling the two above clean and fast from every script

    I hope this made it a bit clearer,
    Cheers!
     
  14. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Ok, will have a play though and see what I can break out. Obviously the missing bit here is your GUIManager.
    Will see what I can do when pulling it in to the UI Extensions project
     
  15. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Yep, but see the GUIManager just a sigleton that makes all the UI scripts (one script per different panel/window I use in the game) accessible quickly and just keeps tabs of the open/close status of each, plus it grants access to the tooltip from everywhere.

    On alternative solution might be having the tooltip script store instead in the MousePointerHandler, might help in making for a more compact solution but logic wise would make a bit less sense.Otherwise I can setup a scene next week if you want with a surrogate of my GUI Manager.