- Home /
[C#]Inventory script help.
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[i]._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<ITEM> 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;
}
}
Here is a great tutorial, that $$anonymous$$ches you how to create an inventory system from scratch, with the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ and its all done in C#
Answer by Ouija · Jan 30, 2014 at 10:59 PM
Hey, I am not an advanced programmer, but check out bergzerg on youtube. I think that is his name. He does a full inventory system. By the time I was done, i was picking up items, viewing them in the inventory, and also for things like weapons and items you pick up, you can put them on your character as well. It's a good rig, try it out! He does it step by step explaining everything. I would show you the code if I knew where I put it.... hmmm
Answer by james_170482 · Jan 30, 2014 at 11:14 PM
Hello,
I'm Not Sure I completely understand but when i want to dynamically create buttons for an unknown amount of items i do something like this.
public class Inventory : MonoBehaviour
{
public List< string > Items = new List< string >();
//Add Items To List Etc......
void OnGUI()
{
if( Items.Count > 0 )
{
for( int i = 0; i < Items.Count; i++ )
{
if( GUI.Button( new Rect( 50, i * 50, 50, 50 ), Items[ i ].ToString()))
{
print( "We Clicked Item " + Items[ i ] );
}
}
}
}
}
Hope this is some help.
Answer by ik1602 · Feb 06, 2015 at 10:02 PM
Since I am making second advanced version, it would really use your opinion on my first asset http://goo.gl/6Bq4Ll this may give you an idea. DIABOLIC RPG INVENTORY-FREE
p.s. I intent to post same response trough the forum whenever similar questions are ask, since I need opinions on my second asset which is advanced version of this one. I hope I am not spamming, if someone thinks that I do please let me know I'll stop immediately.Thanks.