How to get selected itemview

Hi there!

What would be the easiest way to get the gameobject that is currently highlighted (i.e., hovered over with a mouse or selected with a gamepad/keyboard) and contains an itemview? For example - in the crafting window, how would I return the gameobject circled in red in this image.

I was checking out some of the drag and hover interfaces, but not sure which one to use or how to implement it.



Staff member

There are multiple ways you could go about getting this information (using the Unity UI event system, listening to the Grid select item events, etc...)

But I think the easiest solution in this specific use case is getting a reference of the GridEventSystem component and call the "GetSelectedButton" or "GetSelectedButtonIndex" function. You can then use the button or the index to find the Item View Slot and from there the Item View.

So something like that:
var buttonIndex = m_Grid.GridEventSystem.GetSelectedButtonIndex();
var view = m_ViewDrawer.Views[buttonIndex];

Note the this solution is only for grids (Item Info Grid or Recipe Grid). For Item View Slot Containers (which is not the case of the Recipe Grid) you can use the "GetSelectedSlot" function of the item view slot container. This will return the Item View Slot.

I just realised Get Selected Button Index was not public. I just changed it to public.

I also added these functions to the GridGeneric class:
/// <summary>
/// Get the selected element.
/// </summary>
/// <returns>The selected element.</returns>
public T GetSelectedElement()
    var selectedIndex = GridEventSystem.GetSelectedButtonIndex();
    if (selectedIndex < 0 || selectedIndex >= m_Elements.Count) { return default; }
    return GetElementAt(selectedIndex);
/// <summary>
/// Get the selected View.
/// </summary>
/// <returns>The selected View.</returns>
public View<T> GetSelectedView()
    var selectedIndex = GridEventSystem.GetSelectedButtonIndex();
    return GetBoxAt(selectedIndex);

I hope that helps!

Unfortunatly it didn't make the update we made this morning. But it will be part of the next update
Thanks! Not sure if my code is correct here, but I'm getting an odd result...

var buttonIndex = GameObject.FindObjectOfType<CraftingRecipeGrid>().GridEventSystem.GetSelectedButtonIndex();
var view = GameObject.FindObjectOfType<CraftingRecipeViewDrawer>().Views[buttonIndex];

The first time I click on Invisibility Potion - it seems to work correctly, but it spits out two results for the buttonIndex: 1 and then -1, and then every other time I click on it, I get a -1. There is only one instance of the CraftingRecipeGrid and CraftingRecipeViewDrawer in my scene.


Not sure this terribly matters, but this code is in the CanCraftInternal method in a custom crafting processor. I'm basically trying to show an additional element when the player hasn't unlocked a tech required to craft something. So I want to get the button so I can enable a popup near it.

One other question, is there a better place to put this so that I can get access to the button when you hover over it, not just click it?


Staff member
That's odd, I'll need to look at this in more detail.

But for your use case I would recommend another approach.

You can either listen to the select/deselect event on the Grid using one of these events where the int is the slot index:

Or you can detect directly on the Item View Slot by adding a custom component that either inherits the Unity UI interfaces like:
IPointerDownHandler, IPointerUpHandler,
IPointerEnterHandler, IPointerExitHandler,
ISelectHandler, IDeselectHandler, IPointerClickHandler

Or by using the interface for a custom Item View Module:
There are other useful interfaces for Item Views you can find them here:

I hope that helps!