【发布时间】:2014-04-19 00:19:52
【问题描述】:
总的来说,我对编程和统一性还是很陌生。考虑到项目中只有脚本没有统一对象,我确信这个问题与统一没有任何关系。
话虽如此,我无法更新库存中的数量/堆栈和项目。所以我有一个拖放系统。当我将项目拖到另一个相同的项目上时,我希望它堆叠。据我所知,我做得对,但由于某种原因,它将新堆栈设置为库存中与我要堆叠的物品相同的所有物品。
例如,我在库存中有三种相同的药水,但它们都在各自的插槽中。我将一种药水拖到另一种药水上。我现在在插槽 2 上有一堆两种药水。但是插槽 3 中的药水也有一叠 2 种药水。我不知道这个问题是从哪里来的。
首先,我试图让一切正常工作,然后我会让事情变得更有效率。这意味着一旦我弄清楚这一点,我就会将其转换为 baseItem 类。
这是我的 Item 类
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
接下来是我的通用 Inventory 类
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;
}
【问题讨论】:
-
你应该跟踪你的物品 ID 可以拥有的最大堆叠,然后在减去光标处的“持有”数量的同时填充相同类型的库存槽,直到它达到 0。当仍然不是 0,从头到尾填充并丢弃其余部分(或您计划的任何内容)。记得在达到 0 或堆叠物品后中断(您似乎在 StackItem(I) 忘记了)