- Home /
Other: Solved it. I had to use EventSystem, block raycasts on the icons, and use OnBeginDrag, OnDrag and OnEndDrag. OnEndDrag I used eventData.pointerEnter to get the gameObject slot.
Get object at mouse position? (UI)
Hey guys, so I'm further working on an inventory system in my game and I've ran into a problem. Basically, what I want to do is when you are dragging an item and stop dragging it at a certain slot (an UI element), it detects which slot is at the current mouse position and performs a function to put the item there.
My current script is rather simple so far:
public void OnBeginDrag()
{
ipos = this.transform.transform.Find("Icon").position;
dragging = true;
currentTooltip = "";
sibling = this.transform.GetSiblingIndex ();
this.transform.SetAsLastSibling ();
}
public void OnDrag()
{
tooltip = "";
transform.transform.Find("Icon").position = Input.mousePosition;
}
public void OnEndDrag()
{
this.transform.transform.Find("Icon").position = ipos;
ipos = Vector2.zero;
dragging = false;
this.transform.SetSiblingIndex (sibling);
}
This is currently the script for the icon to snap back in place (as it doesn't hit a slot). How do I implement it to detect the slot the mouse points at? Thanks in advance.
Edit: Sorry, I mean detecting if the Icon transform is in the slot's bounds.
Follow this Question
Related Questions
Pointer Enter/Exit keeps triggering on UI element (Image) 3 Answers
How do i disable my buttons while dragging ScrollRect? 0 Answers
PauseMenu - Camera moves in pausemenu 0 Answers
How do i get the child of an GameObject the pointer is hovering over? 0 Answers
Get mesh name with mouse click, then change color (and other properties) of mesh 2 Answers