using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
using UnityEngine.UI;
public class ItemSlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField] ItemToolTip tooltip;
[SerializeField] Image image;
private Item _item;
public Item Item
{
get { return _item; }
set
{
_item = value;
if (_item == null)
{
image.enabled = false;
}
else
{
image.sprite = _item.Icon;
image.enabled = true;
}
}
}
protected virtual void OnValidate()
{
if (image == null)
image = GetComponent<Image>();
if (tooltip == null)
tooltip = FindObjectOfType<ItemToolTip>();
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Slot Detected!");
if (Item is EquippableItem)
{
tooltip.ShowTooltip((EquippableItem)Item);
}
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("You Exit");
tooltip.HideTooltip();
}
}
Item Tooltip
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class ItemToolTip : MonoBehaviour
{
[SerializeField] Text ItemNameText;
[SerializeField] Text ItemSlotText;
[SerializeField] Text ItemStatsText;
private StringBuilder sb = new StringBuilder();
public void ShowTooltip(EquippableItem item)
{
ItemNameText.text = item.ItemName;
ItemSlotText.text = item.EquipmentType.ToString();
sb.Length = 0;
AddStat(item.StrengthBonus, "Strength");
AddStat(item.AgilityBonus, "Agility");
AddStat(item.IntelligenceBonus, "Vitality");
AddStat(item.VitalityBonus, "Intelligence");
AddStat(item.StrengthPercentBonus, "Strength", isPercent: true);
AddStat(item.AgilityPercentBonus, "Agility", isPercent: true);
AddStat(item.IntelligencePercentBonus, "Vitality", isPercent: true);
AddStat(item.VitalityPercentBonus, "Intelligence", isPercent: true);
ItemStatsText.text = sb.ToString();
gameObject.SetActive(true);
}
public void HideTooltip()
{
gameObject.SetActive(false);
}
private void AddStat(float value, string statName, bool isPercent = false)
{
if (value != 0)
{
if (sb.Length > 0)
sb.AppendLine();
if (value > 0)
sb.Append("+");
if (isPercent)
{
sb.Append(value * 100);
sb.Append("% ");
}
else
{
sb.Append(value);
sb.Append(" ");
}
sb.Append(statName);
}
}
}
Equippableitems
using UnityEngine;
public enum EquipmentType
{
Weapon,
Weapon2,
Weapon3,
Weapon4,
MagicStone
}
[CreateAssetMenu]
public class EquippableItem : Item
{
public int StrengthBonus;
public int AgilityBonus;
public int IntelligenceBonus;
public int VitalityBonus;
[Space]
public float StrengthPercentBonus;
public float AgilityPercentBonus;
public float IntelligencePercentBonus;
public float VitalityPercentBonus;
[Space]
public EquipmentType EquipmentType;
}
These scripts should let a tool tip show the stats of the items in the inventory when you hover with your mouse above these but if i do this nothing happens and i think that OnPointerEnter is not detecting my mouse, what could be the reason for it?(i wrote also a Debug.Log in my OnPointerEnter method but it didnt wrote anything when i was hovering above my slot with my mouse)