Thinking about in Game Tool Tips for Game Objects of Designer choice

I tried this script but seems my tooltip canvas can never be enabled.

The debug.logs come thru but canvas does not show while over game object with this script.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class MouseOverPopUps : MonoBehaviour
{
    // Place this script on any gameObject you want a tool tip for or MouseOver event
    // I am working on thoughts for in game tool tips
    // thinking of making preFabs for differant gameObject types, 
    // enemies, observers, quest givers..., differant house types...
    [SerializeField] string popUpMessage = "Enter text title for this popup";
    [SerializeField] TextMeshProUGUI textBox = null;
    [SerializeField] Canvas inGameToolTipCanvas = null;
    private void Awake() {
        
    }

    void OnMouseOver()
    {
        //If your mouse hovers over the GameObject with the script attached, output this message
        Debug.Log($"Mouse is Over GameObject: {this.name} and its tag is {this.gameObject.tag}");
        //if(textBox != null)
            inGameToolTipCanvas.enabled = true;
            textBox.text = $"Mouse over: {this.name} and its popUpMessage is {popUpMessage}";
    }

    void OnMouseExit()
    {
        //The mouse is no longer hovering over the GameObject so output this message each frame
        Debug.Log($"Mouse has Exited the GameObject: {this.name} and its tag is {this.gameObject.tag}");
        //if(textBox != null)
            inGameToolTipCanvas.enabled = false;
            textBox.text = "";
    }

}

Any ideas?

Are there any game objects that can have text?

Thanks

Here’s my approach, based on a World Of Warcraft style tooltip, which appears in the lower right corner of the screen.
I started with a prefab. The root is a GameObject with a MouseOverTooltipUI script (I’ll get to that). Then a Canvas under the prefab, then the graphic behind the text, and finally the text.

Next, I put a script on the root of the MouseOverTooltip prefab

using UnityEngine;
using TMPro;

public class MouseOverTooltipUI : MonoBehaviour
{
    [SerializeField] private Canvas canvas;
    [SerializeField] private TextMeshProUGUI text;
    
    public static MouseOverTooltipUI Find()
    {
        return FindObjectOfType<MouseOverTooltipUI>();
    }

    private void Awake()
    {
        Hide();
    }

    public void ShowTooltip(string message)
    {
        Show();
        text.text = message;
    }

    void Show()
    {
        canvas.gameObject.SetActive(true);
    }

    public void Hide()
    {
        canvas.gameObject.SetActive(false);
    }
}

This prefab should be in each of the scenes.
Now on any of the items you want to generate a mouse over tooltip, add this script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseOverToolTip : MonoBehaviour
{
    private MouseOverTooltipUI tooltip;
    [SerializeField] private string message = "You really should put a message here!";

    private void Awake()
    {
        tooltip = MouseOverTooltipUI.Find();
    }

    private void OnMouseEnter()
    {
        tooltip.ShowTooltip(message);
    }

    private void OnMouseExit()
    {
        tooltip.Hide();
    }
}

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms