I think a better aproach would be to store the quantity of an item, instead of storing the stackable itens repeatdly in a list. Because when we are talking about large amounts of stacking items, it wouldn't work;
using UnityEngine;
using System.Collections.Generic;
public class StackExchange : MonoBehaviour
{
List<InventItem> rawInventory;
List<List<InventItem>>List<InventSlot> groupedInventory;
void Start ()
{
rawInventory = new List<InventItem>();
rawInventory.Add(new InventItem("apple" , true));
rawInventory.Add(new InventItem("apple", true));
rawInventory.Add(new InventItem("carrot", true));
rawInventory.Add(new InventItem("knife", false));
rawInventory.Add(new InventItem("knife", false));
rawInventory.Add(new InventItem("knife", false));
groupedInventory = new List<List<InventItem>>List<InventSlot>();
foreach (InventItem i in rawInventory){
List<InventItem> list = getInventSoubgroupproccessItem(i.name);
}
if (list== null || !iDebug.isStackableLog(getPrintableList(rawInventory){);
string s = "Grouped list:\n";
list = new List<InventItem> foreach (InventSlot slot in groupedInventory);{
s += "{\n";
list.Add(i);
for (int i = 0; i groupedInventory< slot.Add(listquantity; i++);
} s += slot.item.output();
elses if+= (i.isStackable)"}\n";
}
listDebug.AddLog(is);
}
string Debug.Log(getPrintableList (rawInventory)List<InventItem> list);{
string soutput = "Grouped list:"{\n";
foreach (List<InventItem>var listv in groupedInventorylist){
soutput += getPrintableListv.output(list) ;
}
Debug.Log(s);
output += "}
\n";
string getPrintableList (List<InventItem> list){ return output;
}
stringvoid outputproccessItem =(InventItem "item){\n";
foreach (var vinventSlot in listgroupedInventory){
outputif +=(inventSlot.item.name v== item.output()name ;&& inventSlot.item.isStackable){
}
outputinventSlot.quantity++;
+= "}\n";
return output;
}
return;
List<InventItem> getInventSoubgroup (string name){ }
foreachgroupedInventory.Add (List<InventItem> listnew inInventSlot groupedInventory(item));
}
}
class InventSlot{
public ifInventItem (list.Countitem;
> 0 && list[0].namepublic ==int name)quantity;
public InventSlot (InventItem item){
this.item return= list;item;
returnquantity null;= 1;
}
}
class InventItem
{
public string name;
public bool isStackable;
public InventItem (string name , bool isStackable){
this.name = name;
this.isStackable = isStackable;
}
public string output (){
return "\tInventoryItem - name:" + name + ", stackable:" + isStackable + "\n";
}
}