Displaying Enemy Information on Right Mouse Click

Suppose I want to display the enemy information to our player before he decides to attack an Enemy or an NPC for example. When we right click an enemy, I want to be able to see something such as 'Attack Enemy: ’ followed by their combat level. So far, I implemented an Enemy Level Display, and thanks to Brian, we managed to get it to work, but I also want to be able to right click on my enemy and have a UI (preferably with our existing Tooltip, discussed in the inventory course) that displays his combat level for instance (and maybe verify the color of the text, based on the difference of combat level between our enemy and our player), so our player can get an idea of what he’s getting himself into. How can we do that?

The easiest way to do this is quite literally with our current Tooltip spawner setup. The tricky part about this is that the tooltip tends to get in the way of the game view and can actually interfere with attacking the enemy. While the tooltip from the Inventory course works great for adding a tooltip to UI, it’s not as great at adding a tooltip to 3d in game objects.

I came up with a better solution for another game of mine, borrowing from games that like to put the tooltips in one corner of the game.

Here are the three scripts I use for this setup: The first is MouseOverTooltipUI. This script will be put on a customized UI window in one of the corners of the screen. The customized window will need a TextMeshProText for the title, the information text, and an image. The script itself will be on the top level of the window, with the balance of the window on a GameObject under that top level GameObject. (it won’t work if it’s on the same GameObject as the window itself).

MouseOverUI.cs
using UnityEngine;
using UnityEngine.UI;
using TMPro;

namespace TkrainDesigns.UI
{
    public class MouseOverTooltipUI : MonoBehaviour
    {
        [SerializeField] private GameObject window;
        [SerializeField] private TextMeshProUGUI title;
        [SerializeField] private TextMeshProUGUI text;
        [SerializeField] private Image image;

        public static MouseOverTooltipUI Find()
        {
            return FindObjectOfType<MouseOverTooltipUI>();
        }

        private void Awake()
        {
            Hide();
        }

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

        void Show()
        {
            window.SetActive(true);
            transform.SetAsLastSibling();
        }

        public void Hide()
        {
            window.SetActive(false);
        }

        public void SetTitle(string titleToSet)
        {
            title.text = titleToSet;
        }

        public void SetImage(Sprite spriteToSet)
        {
            image.sprite = spriteToSet;
            image.gameObject.SetActive(spriteToSet != null);
        }
    }
}

Next is an abstract class that forms the backbone of the alternate tooltip system. It handles finding the tooltip window and displaying it. A subclass of this is required to handle the actual getting of the tooltip information and attaching to whatever GameObject you want to have a tooltip for.

MouseOverTooltip.cs
using System;
using UnityEngine;
using UnityEngine.UI;

namespace TkrainDesigns.UI
{
    public abstract class MouseOverToolTip : MonoBehaviour
    {
        private MouseOverTooltipUI tooltip;


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

        protected abstract string GetTooltipMessage();
        protected abstract Sprite GetSprite();
        protected abstract string GetTooltipTitle();

        private void OnMouseEnter()
        {
            tooltip.SetTitle(GetTooltipTitle());
            tooltip.SetImage(GetSprite());
            tooltip.ShowTooltip(GetTooltipMessage());
        }

        private void OnMouseOver()
        {
            tooltip.ShowTooltip(GetTooltipMessage());
        }

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

Finally, the abstract class is subclassed to display information on the character in question. This component is what is put on the Character itself. (You can similarly subclass MouseOverTooltip for other in game items like Pickups, as long as they give the needed information).

CharacterMouseOver.cs
using System.Text;
using RPG.Attributes;
using RPG.Stats;
using UnityEngine;

namespace TkrainDesigns.UI
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(Health))]
    [RequireComponent(typeof(BaseStats))]
    public class CharacterMouseOver : MouseOverToolTip
    {
        [SerializeField] private Sprite sprite;
        [SerializeField] private string displayName = "Brigand";

        protected override string GetTooltipMessage()
        {
            Health health = GetComponent<Health>();
            BaseStats stats = GetComponent<BaseStats>();
            Experience experience = GetComponent<Experience>();
            StringBuilder builder = new StringBuilder();
            builder.Append($"Level {stats.GetLevel()}\n");
            if (experience)
            {
                builder.Append($"XP: {experience.GetExperience()}/{stats.GetStat(EStat.XPNeeded)}\n");
            }

            if (health.IsAlive()) builder.Append($"Health: {health.GetCurrentValue()}/{health.GetMaxValue()}\n");
            else
            {
                builder.Append($"Dead.");
            }


            return builder.ToString();
        }

        protected override Sprite GetSprite()
        {
            return sprite;
        }

        protected override string GetTooltipTitle()
        {
            return displayName;
        }
    }
}

Privacy & Terms