hey there,
So I’m trying to create an inventory system, I’ve gotten to the point now where icons drag and drop into the inventory, but due to the fact i wish to make it both X and Y, i have the issue where once an item goes in, all slots become that item…
Can someone look at this code and help me fix it please:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerHUD : MonoBehaviour {
//ActionBar
public SpellDataBase[] ActionBar = new SpellDataBase[12];
public List<SpellDataBase> _SpellBook = new List<SpellDataBase>();
public ItemDataBase[] InventoryBars = new ItemDataBase[1];
public List<ItemDataBase> _ItemList = new List<ItemDataBase>();
private SpellDataBase DraggedSpell;
private ItemDataBase DraggedItems;
private ItemDataBase DraggedItems2;
private int xx;
private int yy;
void Start () {
CurrentScore = PlayerPrefs.GetInt("CurrentScore");
CharactersName = PlayerPrefs.GetString("CharactersName");
#region Spells
_SpellBook.Add(new SpellDataBase.FireBall(this));
_SpellBook.Add(new SpellDataBase.IceBall(this));
#endregion
// Divide...
#region Items
_ItemList.Add(new ItemDataBase.Wood(this));
_ItemList.Add(new ItemDataBase.Wheat(this));
#endregion
}
void OnGUI () {
//if(Triggerinventory){
#region Inventory
int Inventoryx =0;
int _Offset = 0;
foreach (ItemDataBase Inventory_Item in InventoryBars)
{
Texture2D icon = Resources.Load("emptyactionbutton") as Texture2D;
if(Inventory_Item != null)
icon = Inventory_Item.icon;
for(int xx = 0; xx < 5; xx++){
for(int yy = 0; yy < 5; yy ++){
Rect Inventory_icon_rect_Items = new Rect(Screen.width/2 + ( xx * 60), 50 + (yy * 60), 60, 60);
GUI.DrawTexture(Inventory_icon_rect_Items,icon);
_Offset += 54;
if (DraggedItems != null && Inventory_icon_rect_Items.Contains(Event.current.mousePosition) && !Input.GetButton("Fire1"))
{
InventoryBars[Inventoryx] = DraggedItems;
DraggedItems = null;
}
else if (DraggedItems == null && Inventory_icon_rect_Items.Contains(Event.current.mousePosition) && Input.GetButtonDown("Fire1"))
{
DraggedItems = Inventory_Item;
InventoryBars[Inventoryx] = null;
}
}
}
// Inventoryx++;
}
//}
#endregion
// _Dragged Items
#region Dragged Items
if (DraggedItems != null)
{
Rect rec = new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 0, 0);
//Rect rec = mouse_pointer;
rec.width = DraggedItems.icon.width;
rec.x -= DraggedItems.icon.width / 2;
rec.y -= DraggedItems.icon.height / 2;
rec.height = DraggedItems.icon.height;
GUI.DrawTexture(rec, DraggedItems.icon);
}
//Whenever we release the drag,"delete" the spell we are moving
if (!Input.GetButton("Fire1"))
DraggedItems = null;
#endregion
}
}
As you can see it basically allows the user to drag items from the item script, which is this:
using UnityEngine;
using System.Collections;
public abstract class ItemDataBase {
private string ItemName;
private int Value;
private string ItemType; // This will be the type of item, this will range from, ( uncommon, common, good, rare, epic, heroic, legendary )
public Texture2D icon;
private PlayerHUD owner;
public abstract void ItemClicked();
#region Misc Items
public class Wood : ItemDataBase
{
public Wood(PlayerHUD owner)
{
this.ItemName = "Wood";
this.Value = 10;
this.ItemType = "Common";
this.owner = owner;
this.icon = Resources.Load("WoodIcon") as Texture2D;
}
public override void ItemClicked()
{
Debug.Log(ItemName +"Value: " + Value + "Item Type: " + ItemType + "Description: " + "Smells kinda woody...");
}
}
public class Wheat: ItemDataBase
{
public Wheat(PlayerHUD owner)
{
this.ItemName = "Wheat";
this.ItemType = "Common";
this.Value = 2;
this.owner = owner;
this.icon = Resources.Load("wheat") as Texture2D;
}
public override void ItemClicked()
{
Debug.Log("Some mighty fine wheat...");
}
}
#endregion
#region Weapons
#endregion
#region Armour
#endregion
#region Consumables
#endregion
}
Anyways, it only works if i make inventory bars 1, which i can understand. This will make each box that item though, when i take out the for loops for xx and yy i just get a set of bars going in a straight line… can someone tell me how i can alter this to make a loot window which can be usable to the draggeditems part etc.
Many thanks,
Ice