Problems inheriting from IconItemView and ItemShapeItemView to set my own icon

Hi,

I have a list of icons depending on if the item is broken or not. I want to have my own scripts to do that, but there are problems

1) IconItemView

public class IconItemViewWithIndex : IconItemView
{

public override void SetValue(ItemInfo info)
{
if (info.Item == null || info.Item.IsInitialized == false) {
Clear();
return;
}

m_Icon.enabled = true;
var iconToSet = m_MissingIcon;

if (info.Item.TryGetAttributeValue<List<Sprite>>("Icons", out var icons))
{
if (icons == null || icons.Count == 0)
{
iconToSet = m_MissingIcon;
}
else
{
if (info.Item.TryGetAttributeValue<int>("Index", out var index))
{
iconToSet = icons[index];
}
}
}

m_Icon.sprite = iconToSet;
}
}

This doesnt really do anything but if I place the same code in the main Setvalue method it works.

2) ItemShapeItemView - it works but when I drag my item is invisible

public class ItemShapeItemViewIndex : ItemShapeItemView
{

public override void SetValue(ItemInfo info)
{
if (info.Item == null || info.Item.IsInitialized == false) {
Clear();
return;
}

SetAsItemSelectable(true);
SetIcon(info);
}

/// <summary>
/// Set the icon for the item info.
/// </summary>
/// <param name="info">The item info.</param>
public void SetIcon(ItemInfo info)
{
if (info.Item == null) {
ClearIcon();
return;
}

m_Icon.enabled = true;
Sprite iconToSet = null;
if (info.Item.TryGetAttributeValue<List<Sprite>>(m_ShapeIconAttributeName, out var shapeIcons))
{
if (info.Item.TryGetAttributeValue<int>("Index", out var index))
{
iconToSet = shapeIcons[index];
}
}

if (m_UseBackupIcon && iconToSet == null) {
if (info.Item.TryGetAttributeValue<Sprite>("Icon", out var icon)) {
iconToSet = icon;
}
}

if (iconToSet == null) {
iconToSet = m_MissingIcon;
}

m_Icon.sprite = iconToSet;

ResizeToItemShape(info);
}

}

What is the right way to do this? I also replaced the original components on the prefabs with my own. If I edit your scripts with the code I posted it works but I want to inherit from then and override to keep my changes when I update the inventory.
 
Last edited:
I'm sorry I don't understand the question.
Do you want us to make a function virtual so that you can override it?

For the Drag and Drop, the itemView you see being dragged around is drawn by the cursor component that is usually set on the canvas gameobject. So you'll need to make sure that your override the itemViewDrawer over there too, or the ItemViewModule component on the itemView prefab the this drawer is using.
 
Top