- Home /
Hover over text VERY glitchy
Hi, so I've been trying to fix this error for a while now. Basically, I developed an inventory system and I'm trying to setup the item's name to appear while you are hovering over the item and go away when the mouse leaves. Currently, the text appears consistently over any UI element. I've tried EventManagers with OnPointerEnter, OnMouseEnter functions, and nothing seems to work. Any ideas?
A code sample would help. You probably just need to check for object tags or use layers to differentiate between different kinds of UI objects.
back on unity 5.5.4 since there wasnt a VariableGridLayoudGroup, it is, a GridLayout that accept objects of different size, and developing a custom component, I had to implement a simple component that handle drag & drop, this also show the tag of the object when hovering pointer.
note that only works if canvas is set up to overlay
, otherwise you will need a different way to calculate the Label position
public class DragandDropUI : $$anonymous$$onoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler{
public static GameObject itemBeingDragged;
Vector3 startPosition;
public void OnBeginDrag(PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
GetComponent<Image>().raycastTarget = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
itemBeingDragged = null;
transform.position = startPosition;
GetComponent<Image>().raycastTarget = true;
}
public bool hovering = false;
public void OnPointerEnter(PointerEventData eventData){
hovering = true;
}
public void OnPointerExit(PointerEventData eventData){
hovering = false;
}
void OnGUI()
{
if (hovering)
{
GUI.Label (new Rect (Input.mousePosition.x, Screen.currentResolution.height- Input.mousePosition.y, 200f, 50f), transform.tag);
}
}
}
back on unity 5.5.4 since there wasnt a VariableGridLayoudGroup, it is, a GridLayout that accept objects of different size, and developing a custom component, I had to implement a simple component that handle drag & drop, this also show the tag of the object when hovering pointer, note that only works if canvas is set up to overlay
, otherwise you will need a different way to calculate the Label position
public class DragandDropUI : $$anonymous$$onoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler{
public static GameObject itemBeingDragged;
Vector3 startPosition;
public void OnBeginDrag(PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
GetComponent<Image>().raycastTarget = false;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
itemBeingDragged = null;
transform.position = startPosition;
GetComponent<Image>().raycastTarget = true;
}
public bool hovering = false;
public void OnPointerEnter(PointerEventData eventData){
hovering = true;
}
public void OnPointerExit(PointerEventData eventData){
hovering = false;
}
void OnGUI()
{
if (hovering)
{
GUI.Label (new Rect (Input.mousePosition.x, Screen.currentResolution.height- Input.mousePosition.y, 200f, 50f), transform.tag);
}
}
}