Hello whoever is reading this.
I wanted to get more advanced in my programming so i tried to make a Inventory.
But there are some stuff i do not understand.
and that is why im asking this question.
i got my inventoryGUI script, which, makes the GUI.
Now how do i add each GUI.Button to a list, or at least create the “effect” of it, so the nearest empty one gets filled with the looted item?
these are my scripts (added ITEMS and ITEM script, so there would be no confusion, if you want to see “WEAPONS.CLASS” Just say so ).
InventoryGUI
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryGUI : MonoBehaviour {
public bool render = false;
public GUISkin kubeSkin;
public List<ITEM> Items = new List<ITEM>();
public int selectedIndex = -1;
public Texture2D invSlot = null;
public float posX;
public float posY;
#region slot textures
public Texture2D currentMouseIcon = null;
public Texture2D LweaponIcon = null;
public Texture2D RweaponIcon = null;
public Texture2D hotbar1 = null;
public Texture2D hotbar2 = null;
public Texture2D hotbar3 = null;
public Texture2D hotbar4 = null;
public Texture2D hotbar5 = null;
public Texture2D hotbar6 = null;
public Texture2D hotbar7 = null;
public Texture2D hotbar8 = null;
public Texture2D hotbar9 = null;
public Texture2D hotbar10 = null;
#endregion
public bool inuse = false;
// Use this for initialization
void Start () {
if (Items == null)
Items = new List<ITEM>();
}
// Update is called once per frame
void ToggleWindow()
{
render = !render;
}
void Update()
{
posX = Input.mousePosition.x;
posY = Screen.height - Input.mousePosition.y;
if(Input.GetKeyDown(KeyCode.I))
{
ToggleWindow();
}
}
void OnGUI()
{
GUI.skin = kubeSkin;
for (int i = 0; i < Items.Count; i++)
{
if (GUILayout.Toggle(selectedIndex == i, Items*._NAME, "Button"))*
-
selectedIndex = i;*
-
}*
-
#region WEAPONSLOTS*
-
//LEFT HAND*
-
if(GUI.Button(new Rect(100, 550, 50, 50), LweaponIcon)){*
-
LweaponIcon = switchButton(LweaponIcon);*
-
}*
-
//RIGHT HAND*
-
if(GUI.Button(new Rect(950, 550, 50, 50), RweaponIcon)){*
-
RweaponIcon = switchButton(RweaponIcon);*
-
}*
-
#endregion*
-
#region hotbar*
-
if(GUI.Button(new Rect(300, 550, 50, 50), hotbar1)){*
-
hotbar1 = switchButton(hotbar1);*
-
}*
-
if(GUI.Button(new Rect(350, 550, 50, 50), hotbar2)){*
-
hotbar2 = switchButton(hotbar2);*
-
}*
-
if(GUI.Button(new Rect(400, 550, 50, 50), hotbar3)){*
-
hotbar3 = switchButton(hotbar3);*
-
}*
-
if(GUI.Button(new Rect(450, 550, 50, 50), hotbar4)){*
-
hotbar4 = switchButton(hotbar4);*
-
}*
-
if(GUI.Button(new Rect(500, 550, 50, 50), hotbar5)){*
-
hotbar5 = switchButton(hotbar5);*
-
}*
-
if(GUI.Button(new Rect(550, 550, 50, 50), hotbar6)){*
-
hotbar6 = switchButton(hotbar6);*
-
}*
-
if(GUI.Button(new Rect(600, 550, 50, 50), hotbar7)){*
-
hotbar7 = switchButton(hotbar7);*
-
}*
-
if(GUI.Button(new Rect(650, 550, 50, 50), hotbar8)){*
-
hotbar8 = switchButton(hotbar8);*
-
}*
-
if(GUI.Button(new Rect(700, 550, 50, 50), hotbar9)){*
-
hotbar9 = switchButton(hotbar9);*
-
}*
-
if(GUI.Button(new Rect(750, 550, 50, 50), hotbar10)){*
-
hotbar10 = switchButton(hotbar10);*
-
}*
-
#endregion*
-
#region INVENTORY LOOP*
-
if(render)*
-
{*
-
for(int x = 50;x < 360;x += 60){*
-
for(int y = 100; y < 950; y += 60){*
-
for(int slot = 0; slot < 90; slot++){*
-
GUI.Button(new Rect(y, x, 50, 50),invSlot);*
-
}*
-
}*
-
}*
-
}*
-
#endregion*
-
if(inuse == true){*
-
GUI.Box(new Rect(posX - 25, posY - 25, 50, 50), currentMouseIcon);*
-
}*
-
}*
-
public Texture2D switchButton(Texture2D curSlot){*
-
if(curSlot == null){*
-
if(inuse == true)*
-
{*
-
curSlot = currentMouseIcon;*
-
inuse = false;*
-
}return curSlot;*
-
}*
-
else{*
-
if(inuse == false){*
-
currentMouseIcon = curSlot;*
-
curSlot = null;*
-
inuse = true;*
-
}return curSlot;*
-
}*
-
return curSlot;*
-
}*
}
ITEMS -
using System.Collections.Generic;
public class ITEMS : MonoBehaviour {
-
#region WEAPONS*
-
public List weaponsList;*
-
#region Beginnersword_01*
-
static GameObject wep01_model;*
-
static Texture2D wep01_icon;*
-
public WEAPONS wep01;*
-
#endregion*
-
#region Beginnersword_01*
-
static GameObject wep02_model;*
-
static Texture2D wep02_icon;*
-
public WEAPONS wep02;*
-
#endregion*
-
#endregion*
-
void Awake(){*
-
if (weaponsList == null)*
-
weaponsList = new List<ITEM>();*
-
wep01_model = Resources.Load<GameObject>("ITEM MODELS/WEAPONS/Sword/SWORD_MODEL") as GameObject;*
-
wep01 = new WEAPONS(1,"Sword of the Beginner",wep01_icon,001,1,wep01_model,10f,0);*
-
wep02_model = Resources.Load<GameObject>("ITEM MODELS/WEAPONS/Hammer/HAMMER_MODEL") as GameObject;*
-
wep02 = new WEAPONS(2,"Hammer of the Beginner",wep02_icon,001,1,wep02_model,10f,0);*
-
}*
-
void Start () {*
-
weaponsList.Add(wep01);*
-
weaponsList.Add(wep02);*
-
}*
}
ITEM -
using UnityEngine;
using System.Collections;
public class ITEM{
-
public int _ID;*
-
public string _NAME;*
-
public Texture2D _ICON;*
-
public int _VALUE;*
-
public ITEM(int id, string name,Texture2D icon,int value){*
-
_ID = id;*
-
_NAME = name;*
-
_ICON = icon;*
-
_VALUE = value;*
-
}*
}