Search Unity

Tooltip display.

Discussion in 'Scripting' started by Hansuke, Jun 12, 2007.

  1. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    I created a Guitexture as my menu, and when the user mouse over on the menu, it shows a tooltip.

    the tootip itself also should be a guitexture.

    i tried myself but its not working.

    Please help

    :roll:
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Make sure the the name of your tooltip is ToolTip


    Menu.js:
    Code (csharp):
    1. private var toolTip : GUITexture;
    2. function Start () {
    3.     toolTip = GameObject.Find ("ToolTip").guiTexture;
    4. }
    5.  
    6. function OnMouseEnter () {
    7.     ShowIcon (true);
    8. }
    9.  
    10. function OnMouseExit () {
    11.     ShowIcon (false);
    12. }
    13.  
    14. function ShowIcon (index : boolean) {
    15.     if (index) {
    16.         Screen.showCursor = false;
    17.         toolTip.enabled = true;
    18.     }
    19.     else {
    20.         Screen.showCursor = true;
    21.         toolTip.enabled = false;
    22.     }
    23. }


    ToolTip.js:

    Code (csharp):
    1. function Update () {
    2.     var x = Input.mousePosition.x / Screen.width;
    3.     var y = Input.mousePosition.y / Screen.height;
    4.     transform.position = Vector3 (x, y, 0);
    5. }
    Place this one onto the ToolTip guiTexture.
     
  3. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    everything works fine just have some minor problem, whenever the mouse cursor stays on the menu, the tool tip keep flickering in and out, it wont stays.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I'm not sure why it is flickering, but try this:

    Here's an updated ToolTip.js:

    Code (csharp):
    1. private var z : float;
    2. function Start () {
    3.     z = transform.position.z;
    4. }
    5.  
    6. function Update () {
    7.     var x = Input.mousePosition.x / Screen.width;
    8.     var y = Input.mousePosition.y / Screen.height;
    9.     transform.position = Vector3 (x, y, z);
    10. }
    Use this script and make the ToolTip's z position less than the Menu's z position. The tooltip will be behind the menu but this is the only way that I could find to stop the flickering. :?
     
  5. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    hmm...since it stays behind no point also :? how bout make it show below the menu? How to set a specific coord for the tooltip on display?
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Finally figured it out. Just switch tooltip's layer to "Ignore Raycast". :D
     
  7. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    it doesnt work for me by changing the ToolTip layer to ignore raycast :(
     
  8. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Here's a unity package:
     

    Attached Files:

  9. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    erm weird daniel, your package still showing flickering for me...is it related to the FPS ? i tried ur package on another PC, giving me same result..so im wondering, if it doesnt show flickering to u, why it shows to me. :eek:
     
  10. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
  11. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    yeah, ive gone tru your GuiBuilder on Unify , and you have a part bout the ContextHelpText. I'd play around with that part, but im having a problem to clean up the script...so i thought if i could get a better solution here :)
     
  12. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    All the times I've encountered flickering it was because of the Z value of the textures / text.

    Make sure that you stack your Zs in the order you want things to appear. The bigger the Z, the higher the layer where that element will be placed.

    So if your GUI elements are at Z = 0, for example, make your tooltips with a Z = 0.1 and this should stop the flickering.

    Regards,
    Afonso
     
  13. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    yes, i tried playing with the Z position as well, my tooltip i set Z=1 and for the GUI element i set Z=0.


    ive try every advise here and also tried to play out myself still cant get it to work,im confused :( .

    The thing still flicker (the check box for the GUITexture in the inspector keep turning on/off, that is why causes flicker in the screen).
     
  14. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    If you are using Daniel's code, that has to do with the "Ignore Raycast". I'll try to explain what is hapening with your code.

    1 - Once you get on top of a gui element, it calls the OnMouseEnter() function, which makes your tooltip appear.

    2 - Once your tooltip appears, the mouse is over it, so it makes OnMouseExit() function of the gui element to be called, which makes the tooltip disappear, which makes you go back to point 1, and you keep on this neverending loop.

    To solve this problem you should only have to make the tooltip be in the "Ignore Raycast" layer so that the mouse never thinks that it is on top of the tooltip.

    Another workaround could be simply making the tooltip appear with a little offset from the mouse (i.e. not appear under the mouse pointer). But if you move the mouse quickly it could show the flicker, although only momentarily.

    Regards,
    Afonso
     
  15. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    Thanks for the explanation :)
    I'll try to work around with it, if it still cant work, i might just use codes from shaunl.

    Regards,
    Ernest
     
  16. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    @Hansuke - the tooltip code is extremely simple in my GUIMethods class.

    Basically you select one (or more) guiTextures as the 'control' object. When the user mouse overs or exits that control object, the Tooltip displays.
    Using a 'control' object really simplifies things, because it means the actual tooltip itself needs no interactivity (its either visible or not).
    The control object can be used (this is not in my code, yet) to determine:
    1. Position of the Tooltip
    2. Fade in/out duration
    3. Text content (if any) and size
    4. The display period (before fadeout)

    As for the code in my GUIMethod example, it just controls a GuiTexture... i cut this out for you:
    Code (csharp):
    1.  
    2. //---This code is for your 'control' GO
    3.  
    4. //--- MOUSE ENTER
    5. void OnMouseEnter(){
    6. if(Frozen==false){
    7.     MouseIsOver = true;
    8.     if(ContextHelpText != null){
    9.     ContextHelpText.guiTexture.enabled = true;
    10.     //Put other Tooltip size/position fade stuff here
    11.         }
    12.     }
    13. }
    14.  
    15. //--- MOUSE EXIT
    16. //--- You might also want to capture mouseUp/Down and do the same function (i.e. if button, hide the tooltip on click)
    17. void OnMouseExit(){
    18. if(Frozen==false){
    19.     MouseIsOver = false;
    20.     if(ContextHelpText != null){
    21.     ContextHelpText.guiTexture.enabled = false;
    22.     }
    23.     }
    24. }
    As you can see, its ridiculously simple, so im not sure its what you're looking for.

    - Also - i would recommend using integers for guiElement Z Depths, it makes life simpler ;)

    Cheers
    Shaun