RPG库存物品堆叠C#Unity三维

本文关键字:C#Unity 三维 RPG | 更新日期: 2023-09-27 17:58:55

我对编程和统一还很陌生。我确信这个问题与统一无关,因为项目中只有脚本,没有统一对象。

话虽如此,我在更新库存中和项目的数量/堆栈时遇到了问题。所以我有一个拖放系统。当我把一个项目拖到另一个相同的项目上时,我希望它堆叠起来。据我所知,我做得很好,但出于某种原因,它将新堆栈设置为库存中与我想要堆栈的项目相同的所有项目。

例如,我在库存中有三种相同的药剂,但它们都在自己的插槽中。我把一种药剂拖到另一种药剂上。我现在在2号插槽上有一堆两种药剂。但是槽3中的药剂也有一堆2个药剂。我不知道这个问题是从哪里来的。

首先,我会努力让一切正常运转,然后我会让事情变得更有效率。这意味着一旦我弄清楚了这一点,我就会将其转换为baseItem类。

这是我的物品类

public class Item{
private string itemName;
private int itemID;
private int itemStack;
private int itemMaxStack;

#region Getters and Setters
public string Name
{
    get { return itemName; }
}
public int ID
{
    get { return itemID; }
}
public int Stack
{
    get { return itemStack; }
    set { itemStack = value; }
}
public int MaxStack
{
    get { return itemMaxStack; }
}

 #region Constructors
public Item ()
{
    itemName = string.Empty;
    itemID = 0;
    itemStack = 0;
    itemMaxStack = 0;
}
public Item (string name, int id,  int stack,
    int maxstack)
{
    itemName = name;
    itemID = id;
    itemStack = stack;
    itemMaxStack = maxstack;
}
#endregion

接下来是我的通用库存类

public class Inventory : MonoBehaviour 
{
    public int slotsX; // Number of slots on the X axis. Total slots in inventory is slotsX * slotsY
    public int slotsY; // Number of slots on the Y axis. Total slots in inventory is slotsX * slotsY
    private int slotWidth, slotHeight; // The Width and Height of the slots.
    private int iconWidth, iconHeight; // The Width and Height of the icons in the slots.
    private int iconPaddingX, iconPaddingY; // Adds padding to icons in the inventory so that it is center on the X and Y.
    public GUISkin skin; // The GUI skin for the inventory
    public List<Item> slots = new List<Item>(); // List Array that holds all the slots as Items
    public List<Item> inventory = new List<Item>(); // List Array that holds all the Items;
    private ItemDatabase database; // The database of all the items in the game
    private bool draggingItem; // Checks to see if the item is being dragged in the Inventory
    private string tooltip; // The string that contains all the content displayed in the tooltip.
    private int prevIndex; // Holds index of previous dragged item used for swapping items
    private Item draggedItem; // Holds the Item thats is being dragged in the inventory.

    // Use this for initialization
    void Start () 
    {
        database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
        slotWidth = slotHeight = 50;
        iconWidth = iconHeight = 40;
        iconPaddingX = 6;
        iconPaddingY = 6;
        InitializeSlots();
        AddItem(1);
        AddItem(2);
        AddItem(2);
        AddItem(2);
    }

     void InitializeSlots ()
    {
        for (int i = 0; i < (slotsX*slotsY); i++)
        {
            slots.Add(new Item());
            inventory.Add(new Item());
        }
    }
    void OnGUI()
    {
        e = Event.current;
        tooltip = "";
        GUI.skin = skin;
        if (showInventory)
        {
            DrawInventory();
        }

    void DrawInventory ()
    {
        int i = 0;
        for (int y = 0; y < slotsY; y++)
        {
            for (int x = 0; x < slotsX; x++)
            {
                Rect slotRect = new Rect(x * slotWidth, y * slotHeight, slotWidth, slotHeight);
                Rect itemRect = new Rect(x * slotWidth+iconPaddingX, y * slotHeight+iconPaddingY, iconWidth, iconHeight);
                Rect labelRect = new Rect(x * slotWidth, y * slotHeight, iconWidth, iconHeight);
                GUI.Box(slotRect, "", skin.GetStyle("inventory-slot"));
                slots[i] = inventory[i];
                if (slots[i].Stackable && slots[i].Stack > 1)
                {
                    GUI.Label(labelRect, slots[i].Stack.ToString(), skin.GetStyle("stack-label"));
                }
                if (slots[i].Name != string.Empty)
                {
                    GUI.DrawTexture(itemRect, slots[i].Icon);
                    //print(slots[i].itemName + "in slot " + (i+1) + " has a total of " + slots[i].itemStack + " stackes.");
                    if (slots[i].Stackable == true)
                    {
                        //InventoryContains(slots[i].itemID);
                    }
                    // Check to see if mouse is hovering over a slot.
                    if (slotRect.Contains(e.mousePosition))
                    {
                        // Show tooltip
                        GenerateTooltip(slots[i]);
                        // If right mouse click and if not dragging an item and if the item is equal to a consumable 
                        // or key then use item.
                        if (e.button == 1 && e.type == EventType.mouseUp && !draggingItem && (slots[i].Type == Item.ItemType.Consumable || slots[i].Type == Item.ItemType.Key))
                        {
                            //inventory[i] = UseConsumable(inventory[i]); // Check UseConsumable method for definition
                        } 
                        // If left mouse click and if dragging and if not draggingItem = false
                        // Then drag item
                        if (e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
                        {
                            DragItem(i); // Check DragItem method for definition
                        }
                        if (e.isMouse && e.type == EventType.MouseDown && e.clickCount == 2)
                        {
                            print(inventory[i].Stack + " | " + slots[i].Stack);
                        }
                        // If dragging and item and if left mouse click is in up postion
                        if (e.button == 0 && e.type == EventType.mouseUp && draggingItem)
                        {
                            // Stack Items
                            if (slots[i].ID == draggedItem.ID)
                            {
                                StackItem(i);
                            }
                            //Swap Item locations
                            else
                            {
                                inventory[prevIndex] = inventory[i];
                                inventory[i] = draggedItem;
                                draggingItem = false;
                                draggedItem = null;
                            }
                        }
                        else
                        {
                        }
                    }
                }
                else
                {
                    if (slotRect.Contains(e.mousePosition))
                    {
                        if (e.type == EventType.mouseUp && draggingItem)
                        {
                            inventory[i] = draggedItem;
                            draggingItem = false;
                            draggedItem = null;
                        }
                    }
                }

                i++;
            }
        }
    }
     void StackItem (int index)
    {
        int newStack = 0;
        newStack = slots[index].Stack + draggedItem.Stack;
        draggedItem = null;
        draggingItem = false;
        inventory[index].Stack = newStack;
    }
//Adds item by Item ID from the itemDatabase
void AddItem (int id)
{
    for (int i = 0; i < inventory.Count; i++)
    {
        if (inventory[i].Name == string.Empty)
        {
            for (int j = 0; j < database.items.Count; j++)
            {
                if (database.items[j].ID == id)
                {
                    inventory[i] = database.items[j];
                    return;
                }
            }

        }
    }
}

抱歉,只评论了更重要的内容。

编辑:

这是项目数据库类

public class ItemDatabase : MonoBehaviour {
        public List<Item> items = new List<Item>();

        void Start ()
        {
            items.Add(new Item("Base Potion", 1, 1, 10));
            items.Add(new Item("Magic Potion", 2, 1, 10));
        }
    }

编辑2:问题确实是我引用了这个班。我在该类中添加了一个Clone方法,并进行了一些其他修改,然后一切都正常

 public object Clone ()
{
    Item item = new Item(Name, ID,  Quantity, MaxStack);
    return item;
}

RPG库存物品堆叠C#Unity三维

我对代码有点迷失(不是你的错),但我认为问题是通过引用/值错误。(如果你愿意,你可以用谷歌搜索,但我会尽力解释)

默认情况下,C#会通过引用进行传递。意思是这个代码:

MyClass X = new MyClass();
MyClass Y = X;
X.Value = 0;
Y.Value = 10;
Console.WriteLine(X.Value);
Console.WriteLine(Y.Value);

将打印:

10 
10

这是因为Y基本上成为了访问X的一种新方式,所以如果Y改变了,X也会改变。我认为这就是问题所在,当你试图堆叠项目时,这些项目不是单独的对象,而是在某个时候,它们最终成为对一个对象的引用,因此,当你增加一个项目的数量时,另一个项目也会增加。

编辑:完全不确定,但问题可能是在从项目数据库添加新项目时。我认为,也许项目数据库中的项目是一个值,当您将新项目添加到库存中时,它们将成为引用——数据库中项目的副本。