Draggaing inventory item to equipment slot causes items to disappear in grid. Happens in fresh demo scene

geoffd

New member
I'm seeing odd behaviour when dragging an item from the inventory grid to equipment slots in the demo scene. Please note, this is easily recreated from the demo scene in a fresh project.

- Create new project (I've used Unity 2020.3.2 and 2021.1.7), import UIS 1.1.8, open demo scene
- Add a great sword to player Inventory
- Start the game and open the inventory
- Drag the wooden sword to the weapon slot and release - it's equipped no problem

- Now, drag the great sword over the weapon slot without releasing. Oddly, it appears now to have 2 wooden swords in your inventory (this isn't a huge problem, just strange). Also, although the image is updated to show a wooden sword, you can still see the original slot the greatsword occupied showing 15 damage.
- See screen grab 1

- Now (still without releasing the button) drag the great sword off of the weapon slot. Note, it still appears that you have 2 wooden swords in the inventory grid. The swap of the views was not reset. I would expect the extra wooden sword to disappear since I'm no longer previewing a swap
- See screen grab 2

- This is the biggest problem. Now drag and drop the greatsword over the wooden sword, notice instead of being placed in the inventory grid, the wooden sword completely disappears. The item is still there, if you close the inventory and reopen it or move any items around that would cause a redraw, you'll see the wooden sword again. However, it completely disappears after being unequipped in place of the great sword.
- See screen grab 3
 

Attachments

  • ScreenGrab1.PNG
    ScreenGrab1.PNG
    727 KB · Views: 6
  • ScreenGrab2.PNG
    ScreenGrab2.PNG
    620.8 KB · Views: 6
  • ScreenGrab3.PNG
    ScreenGrab3.PNG
    863.8 KB · Views: 6
Thank you for reporting those bugs.

1) I knew about about that one. The drag preview only swaps the Icon of the Item View. I will need to find a way to swap the entire ItemView, I'm not quite sure how I will go about it yet though.

2) Good catch, I didn't know about that one my guess is that I can fix it by listening to the OnPointerExit function

3) That's a really weird one. I don't know how that one occurs. The items should be switched and the item grid should be redrawn. I'll need to dig in this one further to understand it and fix it.

I'm adding all of these issues in my TODO list in high priority. I'll let you know once I have them fixed
 
For the first two issues I was able to fix them using the following code change to the "DropHoverIconPreviewItemView"

Code:
/// <summary>
/// Select with a drop handler.
/// </summary>
/// <param name="dropHandler">The drop handler.</param>
public virtual void SelectWith(ItemViewDropHandler dropHandler)
{
    var dropIndex =
        dropHandler.ItemViewSlotDropActionSet.GetFirstPassingConditionIndex(dropHandler);
    m_ColorFilter.color = dropIndex == -1 ? m_NoConditionsPassed : m_ConditionsPassed;
    var sourceItemInfo = dropHandler.SlotCursorManager.SourceItemViewSlot.ItemInfo;
    PreviewIcon(sourceItemInfo);
    var sourceBoxModules = dropHandler.SlotCursorManager.SourceItemViewSlot.ItemView.Modules;
    for (int i = 0; i < sourceBoxModules.Count; i++) {
        if (sourceBoxModules[i] is ItemViewModule itemViewModule) {
            itemViewModule.SetValue(ItemInfo);
        }
    }
    m_ColorFilter.enabled = true;
}
/// <summary>
/// Preview the icon.
/// </summary>
/// <param name="itemInfo">The item info.</param>
protected virtual void PreviewIcon(ItemInfo itemInfo)
{
    if (itemInfo.Item != null && itemInfo.Item.TryGetAttributeValue<Sprite>("Icon", out var icon)) {
        m_ItemIcon.sprite = icon;
        m_ItemIcon.enabled = true;
        return;
    }
    m_ItemIcon.enabled = false;
}
/// <summary>
/// Deselect with the item view drop handler.
/// </summary>
/// <param name="dropHandler">The drop handler.</param>
public virtual void DeselectWith(ItemViewDropHandler dropHandler)
{
    var sourceBoxModules = dropHandler.SlotCursorManager.SourceItemViewSlot.ItemView.Modules;
    
    for (int i = 0; i < sourceBoxModules.Count; i++) {
        if (sourceBoxModules[i] is ItemViewModule itemViewModule) {
            itemViewModule.SetValue(ItemInfo.None);
        }
    }
    Select(false);
}

For the last issue, there is a problem in the order the things are getting drawn. Both Items think they should be drawn in the same slot.
Adding some logs I saw that I was drawing the entire grid 6 times just by dropping the item in the slot.

So I changed the code so that it cannot draw more than once in a frame. Now instead of drawing when Draw() is called, it sets a boolean to HasToDraw to true and in LateUpdate if that boolean is true than I draw the grid. It seems to work ok for now but I will need to do some more testing to make sure I didn't break anything.

If you wish to test it out yourself do the following changes:

In InventoryGrid:
Code:
 /// <summary>
 /// Cleanup the Inventory Grid Indexer every frame.
 /// </summary>
 protected override void LateUpdate()
 {
     base.LateUpdate();
     if (m_UseGridIndex) {
         m_InventoryGridIndexer.Cleanup();
     }
 }

In the ItemViewSlotsContainerBase class:
Code:
//Add this at the top with the other members of the class.
protected bool m_HasToDraw;

/// <summary>
/// Draw the item view slots.
/// </summary>
public void Draw()
{
    m_HasToDraw = true;
}

/// <summary>
/// Draw is late update if necessary.
/// </summary>
protected virtual void LateUpdate()
{
    if (m_HasToDraw) {
        DrawInternal();
        OnDraw?.Invoke();
        m_HasToDraw = false;
    }
}

Thanks to your bug report the asset should now be a lot more performant
 
I did see the multiple draws too as I attempted to debug the problem myself, I should have mentioned that with my report. I'm glad you were able to so quickly identify the issues though, this seems to be operating more in line with what I would expect. Outstanding work, thank you for the quick response.
 
Top