Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

help with inventory

Discussion in 'Scripting' started by PvTGreg, Oct 23, 2014.

  1. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    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
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class InvGUI : MonoBehaviour {
    6.     float native_width = 800;
    7.     float native_height = 600;
    8.  
    9.     private bool InvToggle = false;
    10.     private Rect InventoryWindow = new Rect(300,100,300,350);
    11.    
    12.  
    13.  
    14.     static public Dictionary<int,Texture2D> InventoryNameDictionary = new Dictionary<int,Texture2D> ()
    15.     {
    16.         {0, ItemClass.EmptyIcon},
    17.         {1, ItemClass.EmptyIcon},
    18.         {2, ItemClass.EmptyIcon},
    19.         {3, ItemClass.EmptyIcon},
    20.         {4, ItemClass.EmptyIcon},
    21.         {5, ItemClass.EmptyIcon},
    22.         {6, ItemClass.EmptyIcon},
    23.         {7, ItemClass.EmptyIcon},
    24.         {8, ItemClass.EmptyIcon},
    25.     };
    26.  
    27.     static public List<int> dictionaryAmounts = new List<int>()
    28.     {
    29.         0,
    30.         0,
    31.         0,
    32.         0,
    33.         0,
    34.         0,
    35.         0,
    36.         0,
    37.         0
    38.  
    39.  
    40.     };
    41.  
    42.  
    43.     ItemClass itemObject = new ItemClass();
    44.     // Update is called once per frame
    45.     void Update ()
    46.     {
    47.         if (Input.GetKeyDown(KeyCode.I))
    48.         {
    49.             InvToggle = !InvToggle;
    50.         }
    51.     }
    52.  
    53.  
    54.     void OnGUI()
    55.     {
    56.         float rx = Screen.width / native_width;
    57.         float ry = Screen.height / native_height;
    58.         GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
    59.         GUILayout.BeginArea (new Rect (200, 500, 500, 400));
    60.         GUILayout.BeginHorizontal ();
    61.         GUILayout.Button ("Slot1", GUILayout.Height (75), GUILayout.Width(75));
    62.         GUILayout.Button ("Slot2", GUILayout.Height (75), GUILayout.Width(75));
    63.         GUILayout.Button ("Slot3", GUILayout.Height (75), GUILayout.Width(75));
    64.         GUILayout.Button ("Slot4", GUILayout.Height (75), GUILayout.Width(75));
    65.         GUILayout.Button ("Slot5", GUILayout.Height (75), GUILayout.Width(75));
    66.         GUILayout.Button ("Slot6", GUILayout.Height (75), GUILayout.Width(75));
    67.         GUILayout.EndHorizontal ();
    68.         GUILayout.EndArea ();
    69.         if (InvToggle == true)
    70.         {
    71.             InventoryWindow = GUI.Window(0, InventoryWindow , InventoruWindowMethod,"Inventory");
    72.        
    73.  
    74.         }
    75.  
    76.     }
    77.                                        
    78.     void InventoruWindowMethod (int Windowid = 0)
    79.     {
    80.         float rx = Screen.width / native_width;
    81.         float ry = Screen.height / native_height;
    82.         GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
    83.         GUI.DragWindow(new Rect(0, 0, 1000, 20));
    84.         GUILayout.BeginArea (new Rect (40, 30, 300, 350));
    85.  
    86.         //First Row
    87.         GUILayout.BeginHorizontal ();
    88.         GUILayout.Button (InventoryNameDictionary[0], GUILayout.Height (75), GUILayout.Width(75));
    89.         GUILayout.Button (InventoryNameDictionary[1], GUILayout.Height (75), GUILayout.Width(75));
    90.         GUILayout.Button (InventoryNameDictionary[2], GUILayout.Height (75), GUILayout.Width(75));
    91.         GUILayout.EndHorizontal ();
    92.         //First Row Amounts
    93.         GUILayout.BeginHorizontal ();
    94.         GUILayout.Box (dictionaryAmounts [0].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    95.         GUILayout.Box (dictionaryAmounts [1].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    96.         GUILayout.Box (dictionaryAmounts [2].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    97.         GUILayout.EndHorizontal ();
    98.  
    99.         //Second Row
    100.         GUILayout.BeginHorizontal ();
    101.         GUILayout.Button (InventoryNameDictionary[3], GUILayout.Height (75), GUILayout.Width(75));
    102.         GUILayout.Button (InventoryNameDictionary[4], GUILayout.Height (75), GUILayout.Width(75));
    103.         GUILayout.Button (InventoryNameDictionary[5], GUILayout.Height (75), GUILayout.Width(75));
    104.         GUILayout.EndHorizontal ();
    105.         //Second Row Amounts
    106.         GUILayout.BeginHorizontal ();
    107.         GUILayout.Box (dictionaryAmounts [3].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    108.         GUILayout.Box (dictionaryAmounts [4].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    109.         GUILayout.Box (dictionaryAmounts [5].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    110.         GUILayout.EndHorizontal ();
    111.  
    112.         //Third Row
    113.         GUILayout.BeginHorizontal ();
    114.         GUILayout.Button (InventoryNameDictionary[6], GUILayout.Height (75), GUILayout.Width(75));
    115.         GUILayout.Button (InventoryNameDictionary[7], GUILayout.Height (75), GUILayout.Width(75));
    116.         GUILayout.Button (InventoryNameDictionary[8], GUILayout.Height (75), GUILayout.Width(75));
    117.         GUILayout.EndHorizontal ();
    118.         //Third Row Amounts
    119.         GUILayout.BeginHorizontal ();
    120.         GUILayout.Box (dictionaryAmounts [6].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    121.         GUILayout.Box (dictionaryAmounts [7].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    122.         GUILayout.Box (dictionaryAmounts [8].ToString (), GUILayout.Height (20), GUILayout.Width(75));
    123.         GUILayout.EndHorizontal ();
    124.        
    125.  
    126.         GUILayout.EndArea ();
    127.  
    128.     }
    129.  
    130.  
    131. }
    ItemClass
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ItemClass : MonoBehaviour {
    5.  
    6.     //Icons
    7.     public static Texture2D EmptyIcon;
    8.     public static Texture2D CubeIcon ;
    9.     public static Texture2D SphereIcon ;
    10.  
    11.     public Texture2D iEmptyIcon;
    12.     public Texture2D iCubeIcon ;
    13.     public Texture2D iSphereIcon ;
    14.  
    15.    
    16.  
    17.     //Items
    18.     public ItemCreatorClass CubeItem = new ItemCreatorClass(0,"Cube",CubeIcon,"Description");
    19.     public ItemCreatorClass SphereItem = new ItemCreatorClass(1,"Sphere",SphereIcon,"Description");
    20.  
    21.  
    22.  
    23.     void Start()
    24.     {
    25.         EmptyIcon = iEmptyIcon;
    26.         CubeIcon = iCubeIcon;
    27.         SphereIcon = iSphereIcon;
    28.     }
    29.  
    30.  
    31.     public class ItemCreatorClass
    32.     {
    33.         public int ItemID;
    34.         public string ItemName;
    35.         public Texture2D ItemIcon;
    36.         public string ItemDescription;
    37.        
    38.         public ItemCreatorClass(int ID,string Name,Texture2D Icon,string Desc)
    39.         {
    40.             ItemID = ID;
    41.             ItemName = Name;
    42.             ItemIcon = Icon;
    43.             ItemDescription = Desc;
    44.         }
    45.     }
    46. }
    47.  
    Loot
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Loot : MonoBehaviour {
    6.  
    7.     ItemClass itemObject = new ItemClass ();
    8.  
    9.     public bool fixedLoot;
    10.  
    11.  
    12.  
    13.     public List<Texture2D> LootDictionary = new List<Texture2D> ()
    14.     {
    15.         {ItemClass.EmptyIcon},
    16.         {ItemClass.EmptyIcon},
    17.         {ItemClass.EmptyIcon},
    18.         {ItemClass.EmptyIcon},
    19.         {ItemClass.EmptyIcon},
    20.         {ItemClass.EmptyIcon}
    21.     };
    22.     public List<int> LootAmounts = new List<int>()
    23.     {
    24.         0,
    25.         0,
    26.         0,
    27.         0,
    28.         0
    29.     };
    30.  
    31.  
    32.     public Texture2D firstLoot = ItemClass.EmptyIcon;
    33.     public Texture2D secondLoot = ItemClass.EmptyIcon;
    34.     public Texture2D thirdLoot = ItemClass.EmptyIcon;
    35.     public Texture2D fourthLoot = ItemClass.EmptyIcon;
    36.     public Texture2D fifthLoot = ItemClass.EmptyIcon;
    37.     public Texture2D sisthLoot = ItemClass.EmptyIcon;
    38.    
    39.     public int firstLootAmount = 0;
    40.     public int secondLootAmount = 0;
    41.     public int thirdLootAmount = 0;
    42.     public int fourthLootAmount = 0;
    43.     public int fifthLootAmount = 0;
    44.     public int sisthLootAmount = 0;
    45.  
    46.  
    47.     void Start()
    48.     {
    49.         // Display Disctionary
    50.         if (fixedLoot == false)
    51.         {
    52.             //random
    53.             LootDictionary[0] = LootRandomizer();
    54.             LootAmounts[0] = amountRandomizer();
    55.            
    56.             LootDictionary[1] = LootRandomizer();
    57.             LootAmounts[1] = amountRandomizer();
    58.         }
    59.         else
    60.         {
    61.             //fixed
    62.             LootDictionary[0] = firstLoot;
    63.             LootAmounts[0] = firstLootAmount;
    64.            
    65.             LootDictionary[1] = secondLoot;
    66.             LootAmounts[1] = secondLootAmount;
    67.         }
    68.        
    69.     }
    70.  
    71.     public Texture2D LootRandomizer ()
    72.     {
    73.         ItemClass items = new ItemClass ();
    74.         Texture2D returnstring = ItemClass.EmptyIcon;
    75.         int randomNumber = Random.Range (0, 1);
    76.        
    77.         switch (randomNumber)
    78.         {
    79.         case 0:
    80.             returnstring = items.CubeItem.ItemIcon;
    81.             break;
    82.            
    83.         case 1:
    84.             returnstring = items.SphereItem.ItemIcon;
    85.             break;
    86.  
    87.            
    88.         default:
    89.             returnstring = ItemClass.EmptyIcon;
    90.             break;                  
    91.         }
    92.         return returnstring;
    93.     }
    94.     public int amountRandomizer()
    95.     {
    96.         int returnAmount = 0;
    97.         returnAmount = Random.Range (1, 11);
    98.        
    99.         return returnAmount;
    100.     }
    101. }
    102.  


    Loot Gui
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class LootGUI : MonoBehaviour {
    6.     float native_width = 800;
    7.     float native_height = 600;
    8.     private bool InvToggle = false;
    9.     private Rect InventoryWindow = new Rect(300,100,300,350);
    10.    
    11.  
    12.     public Camera camera;
    13.     private Loot Loot;
    14.    
    15.     //private Ray mouseRay;
    16.     private RaycastHit RayHit;
    17.  
    18.  
    19.     private List<Texture2D> LootDictionary = new List<Texture2D> ()
    20.     {
    21.         {ItemClass.EmptyIcon},
    22.         {ItemClass.EmptyIcon},
    23.         {ItemClass.EmptyIcon},
    24.         {ItemClass.EmptyIcon},
    25.         {ItemClass.EmptyIcon},
    26.         {ItemClass.EmptyIcon}
    27.     };
    28.  
    29.     private List<int> LootAmounts = new List<int>()
    30.     {
    31.         0,
    32.         0,
    33.         0,
    34.         0,
    35.         0,
    36.         0
    37.     };
    38.  
    39.  
    40.     void Update()
    41.     {
    42.         Ray mouseRay = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    43.  
    44.         if(Input.GetButtonDown("Loot"))
    45.         {
    46.             Physics.Raycast(mouseRay, out RayHit);
    47.             if (RayHit.collider.transform.tag == "Loot")
    48.             {
    49.                 Loot = RayHit.collider.gameObject.GetComponent<Loot>();
    50.                 LootDictionary = Loot.LootDictionary;
    51.                 LootAmounts = Loot.LootAmounts;
    52.                 InvToggle = true;
    53.             }
    54.         }
    55.  
    56.         if (Input.GetKeyDown(KeyCode.Escape))
    57.         {
    58.             InvToggle = false;
    59.         }
    60.     }
    61.  
    62.  
    63.  
    64.  
    65.  
    66.     void OnGUI()
    67.  
    68.     {
    69.         float rx = Screen.width / native_width;
    70.         float ry = Screen.height / native_height;
    71.         GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
    72.         if (InvToggle == true)
    73.         {
    74.             InventoryWindow = GUI.Window(1, InventoryWindow , InventoruWindowMethod,"Loot");
    75.         }
    76.    
    77.     }
    78.            
    79.     void InventoruWindowMethod (int Windowid = 1)
    80.     {
    81.         float rx = Screen.width / native_width;
    82.         float ry = Screen.height / native_height;
    83.         GUI.matrix = Matrix4x4.TRS (new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
    84.         GUI.DragWindow(new Rect(0, 0, 10000, 20));
    85.         GUILayout.BeginArea (new Rect (40, 30, 300, 375));
    86.  
    87.         GUILayout.BeginHorizontal();
    88.  
    89.         if (GUILayout.Button (LootDictionary[0],GUILayout.Height(75), GUILayout.Width(75)))
    90.         {
    91.             if (LootDictionary[0] != ItemClass.EmptyIcon && LootAmounts[0] !=0)
    92.             {
    93.                 InvGUI.InventoryNameDictionary[0] = LootDictionary[0];
    94.                 if(LootAmounts[0] !=0)
    95.                 {
    96.                     LootAmounts[0] -=1;
    97.                     InvGUI.dictionaryAmounts[0] +=1;
    98.                 }
    99.             }
    100.             if (LootAmounts[0] == 0)
    101.             {
    102.                 LootDictionary[0] = ItemClass.EmptyIcon;
    103.             }
    104.         }
    105.    
    106.  
    107.  
    108.  
    109.         if (GUILayout.Button (LootDictionary[1],GUILayout.Height(75), GUILayout.Width(75)))
    110.         {
    111.             if (LootDictionary[1] !=  ItemClass.EmptyIcon && LootAmounts[1] !=0)
    112.             {
    113.                 InvGUI.InventoryNameDictionary[1] = LootDictionary[1];
    114.                 if(LootAmounts[1] !=0)
    115.                 {
    116.                     LootAmounts[1] -=1;
    117.                     InvGUI.dictionaryAmounts[1] +=1;
    118.                 }
    119.             }
    120.             if (LootAmounts[1] == 0 )
    121.             {
    122.                 LootDictionary[1] = ItemClass.EmptyIcon;
    123.             }
    124.         }
    125.  
    126.  
    127.  
    128.  
    129.         if (GUILayout.Button (LootDictionary[2],GUILayout.Height(75), GUILayout.Width(75)))
    130.         {
    131.             if (LootDictionary[2] != ItemClass.EmptyIcon && LootAmounts[2] !=0)
    132.             {
    133.                 InvGUI.InventoryNameDictionary[2] = LootDictionary[2];
    134.                 if(LootAmounts[2] !=0)
    135.                 {
    136.                     LootAmounts[2] -=1;
    137.                     InvGUI.dictionaryAmounts[2] +=1;
    138.                 }
    139.             }
    140.             if (LootAmounts[2] == 0 )
    141.             {
    142.                 LootDictionary[2] = ItemClass.EmptyIcon;
    143.             }
    144.         }
    145.         GUILayout.EndHorizontal ();
    146.         GUILayout.BeginHorizontal();
    147.         GUILayout.Box (LootAmounts [0].ToString (), GUILayout.Height (30), GUILayout.Width(75));
    148.         GUILayout.Box (LootAmounts [1].ToString (), GUILayout.Height (30), GUILayout.Width(75));
    149.         GUILayout.Box (LootAmounts [2].ToString (), GUILayout.Height (30), GUILayout.Width(75));
    150.         GUILayout.EndHorizontal ();
    151.  
    152.  
    153.         GUILayout.EndArea ();
    154.                
    155.     }
    156.  
    157. }
    158.  
     
  2. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    also the case statement sometimes picks cube twice and displays it as 2 separate items how can i ensure it does not do this?
     
  3. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    ok i have solved that last comment

    Code (CSharp):
    1. void Start()
    2.     {
    3.         // Display Disctionary
    4.         if (fixedLoot == false)
    5.         {
    6.             //random
    7.             LootDictionary[0] = LootRandomizer();
    8.             LootAmounts[0] = amountRandomizer();
    9.            
    10.             LootDictionary[1] = LootRandomizer();
    11.             LootAmounts[1] = amountRandomizer();
    12.  
    13.             if (LootDictionary[0] == LootDictionary[1])
    14.             {
    15.                 LootDictionary[0] = LootRandomizer();
    16.                 LootAmounts[0] = amountRandomizer();
    17.                
    18.                 LootDictionary[1] = LootRandomizer();
    19.                 LootAmounts[1] = amountRandomizer();
    20.             }
    21.         }
    22.         else
    23.         {
    24.             //fixed
    25.             LootDictionary[0] = firstLoot;
    26.             LootAmounts[0] = firstLootAmount;
    27.            
    28.             LootDictionary[1] = secondLoot;
    29.             LootAmounts[1] = secondLootAmount;
    30.         }
    31.        
    32.     }
     
  4. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    can anyone help me?
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Please ask a specific coding problem. Your question is too general and too "do this work for me for no compensation please". Even if that's not what you intended, it's what it looks like.
     
  6. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    i dont mean to come across as that way all i need help with is the items stack wrong and sometimes dont have icons and that is what i am asking help for