Hi im currently making an inventory for my game. i am in need of some help.
i cant seem to have piles of the same item so if the loot spawn a pile of 4 cubes and a pile of 2 they dont combine also they dont stack in the inventory they are classed as separate items how can i solve this?
also id like to add tool tips to the items so instead of it just using the icon when you wave over it, it will show all of the contents of the item class
also id like to be able to drag items around to different slots as well as move them to the hotbar
i know this is a lot to ask but id really like some time changing my scripts to allow this
Inventory Gui
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InvGUI : MonoBehaviour {
float native_width = 800;
float native_height = 600;
private bool InvToggle = false;
private Rect InventoryWindow = new Rect(300,100,300,350);
static public Dictionary<int,Texture2D> InventoryNameDictionary = new Dictionary<int,Texture2D> ()
{
{0, ItemClass.EmptyIcon},
{1, ItemClass.EmptyIcon},
{2, ItemClass.EmptyIcon},
{3, ItemClass.EmptyIcon},
{4, ItemClass.EmptyIcon},
{5, ItemClass.EmptyIcon},
{6, ItemClass.EmptyIcon},
{7, ItemClass.EmptyIcon},
{8, ItemClass.EmptyIcon},
};
static public List<int> dictionaryAmounts = new List<int>()
{
0,
0,
0,
0,
0,
0,
0,
0,
0
};
ItemClass itemObject = new ItemClass();
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.I))
{
InvToggle = !InvToggle;
}
}
void OnGUI()
{
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
GUILayout.BeginArea (new Rect (200, 500, 500, 400));
GUILayout.BeginHorizontal ();
GUILayout.Button ("Slot1", GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button ("Slot2", GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button ("Slot3", GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button ("Slot4", GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button ("Slot5", GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button ("Slot6", GUILayout.Height (75), GUILayout.Width(75));
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
if (InvToggle == true)
{
InventoryWindow = GUI.Window(0, InventoryWindow , InventoruWindowMethod,"Inventory");
}
}
void InventoruWindowMethod (int Windowid = 0)
{
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
GUI.DragWindow(new Rect(0, 0, 1000, 20));
GUILayout.BeginArea (new Rect (40, 30, 300, 350));
//First Row
GUILayout.BeginHorizontal ();
GUILayout.Button (InventoryNameDictionary[0], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button (InventoryNameDictionary[1], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button (InventoryNameDictionary[2], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.EndHorizontal ();
//First Row Amounts
GUILayout.BeginHorizontal ();
GUILayout.Box (dictionaryAmounts [0].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.Box (dictionaryAmounts [1].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.Box (dictionaryAmounts [2].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.EndHorizontal ();
//Second Row
GUILayout.BeginHorizontal ();
GUILayout.Button (InventoryNameDictionary[3], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button (InventoryNameDictionary[4], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button (InventoryNameDictionary[5], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.EndHorizontal ();
//Second Row Amounts
GUILayout.BeginHorizontal ();
GUILayout.Box (dictionaryAmounts [3].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.Box (dictionaryAmounts [4].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.Box (dictionaryAmounts [5].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.EndHorizontal ();
//Third Row
GUILayout.BeginHorizontal ();
GUILayout.Button (InventoryNameDictionary[6], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button (InventoryNameDictionary[7], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.Button (InventoryNameDictionary[8], GUILayout.Height (75), GUILayout.Width(75));
GUILayout.EndHorizontal ();
//Third Row Amounts
GUILayout.BeginHorizontal ();
GUILayout.Box (dictionaryAmounts [6].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.Box (dictionaryAmounts [7].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.Box (dictionaryAmounts [8].ToString (), GUILayout.Height (20), GUILayout.Width(75));
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
}
}
ItemClass
using UnityEngine;
using System.Collections;
public class ItemClass : MonoBehaviour {
//Icons
public static Texture2D EmptyIcon;
public static Texture2D CubeIcon ;
public static Texture2D SphereIcon ;
public Texture2D iEmptyIcon;
public Texture2D iCubeIcon ;
public Texture2D iSphereIcon ;
//Items
public ItemCreatorClass CubeItem = new ItemCreatorClass(0,"Cube",CubeIcon,"Description");
public ItemCreatorClass SphereItem = new ItemCreatorClass(1,"Sphere",SphereIcon,"Description");
void Start()
{
EmptyIcon = iEmptyIcon;
CubeIcon = iCubeIcon;
SphereIcon = iSphereIcon;
}
public class ItemCreatorClass
{
public int ItemID;
public string ItemName;
public Texture2D ItemIcon;
public string ItemDescription;
public ItemCreatorClass(int ID,string Name,Texture2D Icon,string Desc)
{
ItemID = ID;
ItemName = Name;
ItemIcon = Icon;
ItemDescription = Desc;
}
}
}
Loot
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Loot : MonoBehaviour {
ItemClass itemObject = new ItemClass ();
public bool fixedLoot;
public List<Texture2D> LootDictionary = new List<Texture2D> ()
{
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon}
};
public List<int> LootAmounts = new List<int>()
{
0,
0,
0,
0,
0
};
public Texture2D firstLoot = ItemClass.EmptyIcon;
public Texture2D secondLoot = ItemClass.EmptyIcon;
public Texture2D thirdLoot = ItemClass.EmptyIcon;
public Texture2D fourthLoot = ItemClass.EmptyIcon;
public Texture2D fifthLoot = ItemClass.EmptyIcon;
public Texture2D sisthLoot = ItemClass.EmptyIcon;
public int firstLootAmount = 0;
public int secondLootAmount = 0;
public int thirdLootAmount = 0;
public int fourthLootAmount = 0;
public int fifthLootAmount = 0;
public int sisthLootAmount = 0;
void Start()
{
// Display Disctionary
if (fixedLoot == false)
{
//random
LootDictionary[0] = LootRandomizer();
LootAmounts[0] = amountRandomizer();
LootDictionary[1] = LootRandomizer();
LootAmounts[1] = amountRandomizer();
}
else
{
//fixed
LootDictionary[0] = firstLoot;
LootAmounts[0] = firstLootAmount;
LootDictionary[1] = secondLoot;
LootAmounts[1] = secondLootAmount;
}
}
public Texture2D LootRandomizer ()
{
ItemClass items = new ItemClass ();
Texture2D returnstring = ItemClass.EmptyIcon;
int randomNumber = Random.Range (0, 1);
switch (randomNumber)
{
case 0:
returnstring = items.CubeItem.ItemIcon;
break;
case 1:
returnstring = items.SphereItem.ItemIcon;
break;
default:
returnstring = ItemClass.EmptyIcon;
break;
}
return returnstring;
}
public int amountRandomizer()
{
int returnAmount = 0;
returnAmount = Random.Range (1, 11);
return returnAmount;
}
}
Loot Gui
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LootGUI : MonoBehaviour {
float native_width = 800;
float native_height = 600;
private bool InvToggle = false;
private Rect InventoryWindow = new Rect(300,100,300,350);
public Camera camera;
private Loot Loot;
//private Ray mouseRay;
private RaycastHit RayHit;
private List<Texture2D> LootDictionary = new List<Texture2D> ()
{
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon},
{ItemClass.EmptyIcon}
};
private List<int> LootAmounts = new List<int>()
{
0,
0,
0,
0,
0,
0
};
void Update()
{
Ray mouseRay = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
if(Input.GetButtonDown("Loot"))
{
Physics.Raycast(mouseRay, out RayHit);
if (RayHit.collider.transform.tag == "Loot")
{
Loot = RayHit.collider.gameObject.GetComponent<Loot>();
LootDictionary = Loot.LootDictionary;
LootAmounts = Loot.LootAmounts;
InvToggle = true;
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
InvToggle = false;
}
}
void OnGUI()
{
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
if (InvToggle == true)
{
InventoryWindow = GUI.Window(1, InventoryWindow , InventoruWindowMethod,"Loot");
}
}
void InventoruWindowMethod (int Windowid = 1)
{
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
GUI.DragWindow(new Rect(0, 0, 10000, 20));
GUILayout.BeginArea (new Rect (40, 30, 300, 375));
GUILayout.BeginHorizontal();
if (GUILayout.Button (LootDictionary[0],GUILayout.Height(75), GUILayout.Width(75)))
{
if (LootDictionary[0] != ItemClass.EmptyIcon && LootAmounts[0] !=0)
{
InvGUI.InventoryNameDictionary[0] = LootDictionary[0];
if(LootAmounts[0] !=0)
{
LootAmounts[0] -=1;
InvGUI.dictionaryAmounts[0] +=1;
}
}
if (LootAmounts[0] == 0)
{
LootDictionary[0] = ItemClass.EmptyIcon;
}
}
if (GUILayout.Button (LootDictionary[1],GUILayout.Height(75), GUILayout.Width(75)))
{
if (LootDictionary[1] != ItemClass.EmptyIcon && LootAmounts[1] !=0)
{
InvGUI.InventoryNameDictionary[1] = LootDictionary[1];
if(LootAmounts[1] !=0)
{
LootAmounts[1] -=1;
InvGUI.dictionaryAmounts[1] +=1;
}
}
if (LootAmounts[1] == 0 )
{
LootDictionary[1] = ItemClass.EmptyIcon;
}
}
if (GUILayout.Button (LootDictionary[2],GUILayout.Height(75), GUILayout.Width(75)))
{
if (LootDictionary[2] != ItemClass.EmptyIcon && LootAmounts[2] !=0)
{
InvGUI.InventoryNameDictionary[2] = LootDictionary[2];
if(LootAmounts[2] !=0)
{
LootAmounts[2] -=1;
InvGUI.dictionaryAmounts[2] +=1;
}
}
if (LootAmounts[2] == 0 )
{
LootDictionary[2] = ItemClass.EmptyIcon;
}
}
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal();
GUILayout.Box (LootAmounts [0].ToString (), GUILayout.Height (30), GUILayout.Width(75));
GUILayout.Box (LootAmounts [1].ToString (), GUILayout.Height (30), GUILayout.Width(75));
GUILayout.Box (LootAmounts [2].ToString (), GUILayout.Height (30), GUILayout.Width(75));
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
}
}