Hi,
I am attempting to make an inventory tooltip for my game. The inventory uses a vertical layout group for its rows, and each row contains a horizontal layout group to align the individual item slots.
I am using the OnPointerEnter() function on each individual item slot, which should return true only when the mouse is hovering over that slot. For some reason, however, this is not the case. I have recorded a video to demonstrate:
The first slot should only show information for the Pistol, and the second the AK-47. Other slots should just return empty strings.
As the video demonstrates, the OnPointerEnter() function seems to disagree with where the mouse position is. Perhaps I have looked at this the wrong way?
I hope that a solution is possible, and thank you for taking your time to read this. My code, which is attached to each slot (white UI box), is as follows:
Code
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventorySlot : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public string itemName = "";
public string itemDescription = "";
public string itemSellValue = "";
private bool mouseHovering = false;
private Inventory inventory;
private GameObject tooltip;
void Start() {
tooltip = GameObject.Find ("InventoryTooltip");
if (tooltip == null) {
inventory = GameObject.Find ("GameManager").GetComponent<Inventory> ();
tooltip = Instantiate (inventory.inventoryTooltip) as GameObject;
tooltip.name = ("InventoryTooltip");
HideTooltip ();
}
tooltip.transform.SetParent(GameObject.Find("UI").transform);
}
public void OnPointerEnter(PointerEventData eventData)
{
UpdateTooltipText ();
mouseHovering = true;
print (gameObject.transform.name);
}
public void OnPointerExit(PointerEventData eventData)
{
mouseHovering = false;
HideTooltip ();
}
void Update() {
if(mouseHovering) {
Vector3 position = new Vector3 (Input.mousePosition.x + 110f, Input.mousePosition.y - 50f, 0f);
tooltip.transform.position = position;
}
}
void HideTooltip() {
tooltip.transform.position = new Vector3 (Screen.width * 2, Screen.height * 2, 0f);
}
void UpdateTooltipText() {
tooltip.transform.Find ("Title").GetComponent<Text> ().text = itemName;
tooltip.transform.Find ("Description").GetComponent<Text> ().text = itemDescription;
tooltip.transform.Find ("Price").GetComponent<Text> ().text = itemSellValue;
}
}
Best regards,
Rees.