Create tooltip on unity3d c#

Hai guys, i apologize if you think i asking a silly question. But i really don’t know how to create tooltip when the mouse is hover the object like this image:

18288-capture.png

Could you guys give me some reference or an example? Thank you very much!

2 Answers

2

You can check out the builtin functionality GUI.tooltip in the script reference.

Hai sir, i already could show the tooltip, but once i show the tooltip, the button function won’t run… why is it like that sir?

Here is the code:

public virtual void TurnOnGUI()
    {
        Rect textRect = new Rect(10, Screen.height - screenTextHeight, textWidth, textHeight);
        Rect buttonRect = new Rect(0, Screen.height - buttonHeight * 3, buttonWidth, buttonHeight);
        Rect _buttonRect = new Rect(0, Screen.height - buttonHeight * 2, buttonWidth, buttonHeight);
        Rect _buttonRect_ = new Rect(0, Screen.height - buttonHeight * 1, buttonWidth, buttonHeight);
        Rect tooltipRect = new Rect(100 + 10, Screen.height - buttonHeight * 3 + 5, buttonWidth, buttonHeight);
        Rect _tooltipRect = new Rect(100 + 10, Screen.height - buttonHeight * 2 + 5, buttonWidth, buttonHeight);
        Rect _tooltipRect_ = new Rect(100 + 10, Screen.height - buttonHeight * 1 + 5, buttonWidth, buttonHeight);

        label1.normal.textColor = Color.red;

        //Text Field
        if (GUI.Button(textRect, ""))
        {

        }

        //ToolTip Text
        //GUI.Button(buttonRect, new GUIContent("Move", "Move the Player"));
        //GUI.Label(tooltipRect, GUI.tooltip, label1);

        GUI.tooltip = null;
        //Move Button
        if (GUI.Button(buttonRect, "Move"))
        {
            if (!moving)
            {
                GameManager.instance.RemoveTileHighlights();
                moving = true;
                attacking = false;
                GameManager.instance.HighlightTilesAt(gridPosition, Color.blue, movementPerActionPoint);
            }

            else
            {
                moving = false;
                attacking = false;
                GameManager.instance.RemoveTileHighlights();
            }
        }

        //ToolTip Text
        //GUI.Button(_buttonRect, new GUIContent("Attack", "Attack the Player"));
        //GUI.Label(_tooltipRect, GUI.tooltip, label1);

        GUI.tooltip = null;
        //Attack Button
        if (GUI.Button(_buttonRect, "Attack"))
        {
            if (!attacking)
            {
                GameManager.instance.RemoveTileHighlights();
                moving = false;
                attacking = true;
                GameManager.instance.HighlightTilesAt(gridPosition, Color.red, attackRange);
            }

            else
            {
                moving = false;
                attacking = false;
                GameManager.instance.RemoveTileHighlights();
            }
        }

        //ToolTip Text
        //GUI.Button(_buttonRect_, new GUIContent("End Turn", "End Turn the Player"));
        //GUI.Label(_tooltipRect_, GUI.tooltip, label1);

        GUI.tooltip = null;
        //End Turn Button
        if (GUI.Button(_buttonRect_, "End Turn"))
        {
            GameManager.instance.RemoveTileHighlights();
            actionPoints = 2;
            moving = false;
            attacking = false;
            GameManager.instance.NextTurn();
        }
	}

And here is the image (the tooltip has shown, but the “Move” button function won’t run, won’t show the blue grid for movement):

18340-untitled.jpg

And when i delete the tooltip, the “Move” button function run perfectly as like shown at the image below:

18341-untitled1.jpg