Recently I found some bugs while reading the source code, please fix them, thanks!
1. UltimateInventorySystem\Editor\VisualElements\ControlTypes\ItemAmountsControl.cs
line: 121
fix:
2. UltimateInventorySystem\Editor\VisualElements\ItemCollectionField.cs
fix:
1. UltimateInventorySystem\Editor\VisualElements\ControlTypes\ItemAmountsControl.cs
line: 121
Code:
/// <summary>
/// Remove the element at the index.
/// </summary>
/// <param name="index">The index to remove at.</param>
protected override void Remove(int index)
{
if (OnRemoveItem != null) {
if (index < 0 || index >= m_ObjectAmounts.Count) {
return;
}
var itemAmount = m_ObjectAmounts[index];
OnRemoveItem?.Invoke(itemAmount, m_ReorderableList.ItemsSource.Count);
} else {
base.Remove(index);
}
m_ItemFieldContainer.Clear();
}
Code:
/// <summary>
/// Remove the element at the index.
/// </summary>
/// <param name="index">The index to remove at.</param>
protected override void Remove(int index)
{
if (OnRemoveItem != null) {
if (index < 0 || index >= m_ObjectAmounts.Count) {
return;
}
var itemAmount = m_ObjectAmounts[index];
OnRemoveItem?.Invoke(itemAmount, index);
} else {
base.Remove(index);
}
m_ItemFieldContainer.Clear();
}
2. UltimateInventorySystem\Editor\VisualElements\ItemCollectionField.cs
Code:
if (Application.isPlaying) {
m_ItemAmountsView.OnAddItem += (item, index) =>
{
m_ItemCollection.AddItem(item);
};
m_ItemAmountsView.OnRemoveItem += (itemAmount, index) =>
{
m_ItemCollection.RemoveItem((ItemInfo)itemAmount);
};
m_ItemAmountsView.OnChange += (index, previousItemAmount, newItemAmount) =>
{
var allItemStack = m_ItemCollection.GetAllItemStacks();
if (index <= 0 || index >= allItemStack.Count) {
return;
}
//get that index in the item stacks at runtime
var itemStackToEdit = allItemStack[index];
// This can happen id an Item was added or removed.
if (itemStackToEdit.Item != newItemAmount.Item) {
return;
}
var difference = newItemAmount.Amount - itemStackToEdit.Amount;
if (difference < 0) {
m_ItemCollection.RemoveItem(new ItemInfo(-difference, itemStackToEdit));
}else if (difference > 0) {
m_ItemCollection.AddItem(new ItemInfo(difference, itemStackToEdit));
}
};
}
Code:
if (Application.isPlaying) {
m_ItemAmountsView.OnAddItem += (item, index) =>
{
m_ItemCollection.AddItem(item);
};
m_ItemAmountsView.OnRemoveItem += (itemAmount, index) =>
{
var allItemStack = m_ItemCollection.GetAllItemStacks();
if (index < 0 || index >= allItemStack.Count)
{
return;
}
//get that index in the item stacks at runtime
var itemStackToEdit = allItemStack[index];
// This can happen id an Item was added or removed.
if (itemStackToEdit.Item != itemAmount.Item)
{
return;
}
m_ItemCollection.RemoveItem((ItemInfo)itemStackToEdit);
};
m_ItemAmountsView.OnChange += (index, previousItemAmount, newItemAmount) =>
{
var allItemStack = m_ItemCollection.GetAllItemStacks();
if (index < 0 || index >= allItemStack.Count) {
return;
}
//get that index in the item stacks at runtime
var itemStackToEdit = allItemStack[index];
// This can happen id an Item was added or removed.
if (itemStackToEdit.Item != newItemAmount.Item) {
return;
}
var difference = newItemAmount.Amount - itemStackToEdit.Amount;
if (difference < 0) {
m_ItemCollection.RemoveItem(new ItemInfo(-difference, itemStackToEdit));
}else if (difference > 0) {
m_ItemCollection.AddItem(new ItemInfo(difference, itemStackToEdit), itemStackToEdit);
}
};
}
Last edited: