Search Unity

Inventory Dragging slots

Discussion in 'Immediate Mode GUI (IMGUI)' started by PvTGreg, Oct 22, 2014.

  1. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Hi ive recently created an inventory by following someones tutorial and im trying to figure out how to drag the item around i have a rough idea of how to do it
    1 when you click on the item its contents is stored ina variable
    2 it then removes itself from the slot
    3 a gui follows the mouse
    4 when you click on a new slot it adds the info to the slot

    could anyone help me set this up?


    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.     public static Texture2D CylinderIcon ;
    11.  
    12.     public Texture2D iEmptyIcon;
    13.     public Texture2D iCubeIcon ;
    14.     public Texture2D iSphereIcon ;
    15.     public Texture2D iCylinderIcon ;
    16.  
    17.    
    18.  
    19.     //Items
    20.     public ItemCreatorClass CubeItem = new ItemCreatorClass(0,"Cube",CubeIcon,"Description");
    21.     public ItemCreatorClass SphereItem = new ItemCreatorClass(1,"Sphere",SphereIcon,"Description");
    22.     public ItemCreatorClass CylinderItem = new ItemCreatorClass(2,"Cylinder",CylinderIcon,"Description");
    23.  
    24.  
    25.     void Start()
    26.     {
    27.         EmptyIcon = iEmptyIcon;
    28.         CubeIcon = iCubeIcon;
    29.         SphereIcon = iSphereIcon;
    30.         CylinderIcon = iCylinderIcon;
    31.     }
    32.  
    33.  
    34.     public class ItemCreatorClass
    35.     {
    36.         public int ItemID;
    37.         public string ItemName;
    38.         public Texture2D ItemIcon;
    39.         public string ItemDescription;
    40.        
    41.         public ItemCreatorClass(int ID,string Name,Texture2D Icon,string Desc)
    42.         {
    43.             ItemID = ID;
    44.             ItemName = Name;
    45.             ItemIcon = Icon;
    46.             ItemDescription = Desc;
    47.         }
    48.     }
    49. }
    50.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class InvGUI : MonoBehaviour {
    6.  
    7.  
    8.     private bool InvToggle = false;
    9.     private Rect InventoryWindow = new Rect(300,100,400,420);
    10.    
    11.  
    12.  
    13.     static public Dictionary<int,Texture2D> InventoryNameDictionary = new Dictionary<int,Texture2D> ()
    14.     {
    15.         {0, ItemClass.EmptyIcon},
    16.         {1, ItemClass.EmptyIcon},
    17.         {2, ItemClass.EmptyIcon},
    18.         {3, ItemClass.EmptyIcon},
    19.         {4, ItemClass.EmptyIcon},
    20.         {5, ItemClass.EmptyIcon},
    21.         {6, ItemClass.EmptyIcon},
    22.         {7, ItemClass.EmptyIcon},
    23.         {8, ItemClass.EmptyIcon},
    24.     };
    25.  
    26.     static public List<int> dictionaryAmounts = new List<int>()
    27.     {
    28.         0,
    29.         0,
    30.         0,
    31.         0,
    32.         0,
    33.         0,
    34.         0,
    35.         0,
    36.         0
    37.  
    38.     };
    39.  
    40.  
    41.     ItemClass itemObject = new ItemClass();
    42.     // Update is called once per frame
    43.     void Update ()
    44.     {
    45.         if (Input.GetKeyDown(KeyCode.I))
    46.         {
    47.             InvToggle = !InvToggle;
    48.         }
    49.     }
    50.  
    51.  
    52.     void OnGUI()
    53.     {
    54.         if (InvToggle == true)
    55.         {
    56.             InventoryWindow = GUI.Window(0, InventoryWindow , InventoruWindowMethod,"Inventory");
    57.  
    58.         }
    59.  
    60.     }
    61.                                        
    62.     void InventoruWindowMethod (int Windowid = 0)
    63.     {
    64.         GUI.DragWindow(new Rect(0, 0, 10000, 20));
    65.         GUILayout.BeginArea (new Rect (50, 30, 400, 400));
    66.  
    67.         //First Row
    68.         GUILayout.BeginHorizontal ();
    69.         GUILayout.Button (InventoryNameDictionary[0], GUILayout.Height (100), GUILayout.Width(100));
    70.         GUILayout.Button (InventoryNameDictionary[1], GUILayout.Height (100), GUILayout.Width(100));
    71.         GUILayout.Button (InventoryNameDictionary[2], GUILayout.Height (100), GUILayout.Width(100));
    72.         GUILayout.EndHorizontal ();
    73.         //First Row Amounts
    74.         GUILayout.BeginHorizontal ();
    75.         GUILayout.Box (dictionaryAmounts [0].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    76.         GUILayout.Box (dictionaryAmounts [1].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    77.         GUILayout.Box (dictionaryAmounts [2].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    78.         GUILayout.EndHorizontal ();
    79.  
    80.         //Second Row
    81.         GUILayout.BeginHorizontal ();
    82.         GUILayout.Button (InventoryNameDictionary[3], GUILayout.Height (100), GUILayout.Width(100));
    83.         GUILayout.Button (InventoryNameDictionary[4], GUILayout.Height (100), GUILayout.Width(100));
    84.         GUILayout.Button (InventoryNameDictionary[5], GUILayout.Height (100), GUILayout.Width(100));
    85.         GUILayout.EndHorizontal ();
    86.         //Second Row Amounts
    87.         GUILayout.BeginHorizontal ();
    88.         GUILayout.Box (dictionaryAmounts [3].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    89.         GUILayout.Box (dictionaryAmounts [4].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    90.         GUILayout.Box (dictionaryAmounts [5].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    91.         GUILayout.EndHorizontal ();
    92.  
    93.         //Third Row
    94.         GUILayout.BeginHorizontal ();
    95.         GUILayout.Button (InventoryNameDictionary[6], GUILayout.Height (100), GUILayout.Width(100));
    96.         GUILayout.Button (InventoryNameDictionary[7], GUILayout.Height (100), GUILayout.Width(100));
    97.         GUILayout.Button (InventoryNameDictionary[8], GUILayout.Height (100), GUILayout.Width(100));
    98.         GUILayout.EndHorizontal ();
    99.         //Third Row Amounts
    100.         GUILayout.BeginHorizontal ();
    101.         GUILayout.Box (dictionaryAmounts [6].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    102.         GUILayout.Box (dictionaryAmounts [7].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    103.         GUILayout.Box (dictionaryAmounts [8].ToString (), GUILayout.Height (20), GUILayout.Width(100));
    104.         GUILayout.EndHorizontal ();
    105.        
    106.  
    107.         GUILayout.EndArea ();
    108.  
    109.     }
    110.  
    111.  
    112. }
     
  2. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Also how would i add tooltips?
     
  3. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    anyone?
     
  4. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    anything?
     
  5. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    anyone?
     
  6. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    First, Tooltips. Simple Gui.label should do, pretty basic but effective way to display text and values. Add a bit of textures to it, it'd look nice.

    Second, don't expect anyone to give you a completely working system from just your scripts. Making other people do all the work will never turn up right.

    Third, the easy way to set up the system you're saying is by simply finding the slot the mouse is over, and when it clicks, do exactly as you said. Make a new "slot" for the mouse, and when you click the slot, copy the contents to the "mouse" slot and then clear it. In theory, this would all be pretty simple. something like...


    Code (CSharp):
    1.  
    2.  
    3. bool mouseHolding;
    4. bool mouseovering;
    5.  
    6. void Update(){
    7.     Vector2 mousepos = Input.mousePosition();
    8. if(Input.GetMouseDown){
    9.     clickedslot = getslotfrommousepos();
    10.     if(clickedslot.info!=empty){  
    11.         mouseslot.infot=clickedslot.info;
    12.         clickedslot.info=empty;
    13.         moseHolding=true;
    14.     }
    15. } else if (Input.GetMouseUp){
    16.     mousedoverslot = getslotfrommousepos();
    17.     mouseslot.info=empty;
    18. } else if (mousedoverslot!= empty)
    19.     mouseovering=true;
    20. }
    21.  
    22. void OnGui(){
    23.     if(mouseHolding)
    24.         Gui.drawTexture(or whatever you wish to use for the label);
    25.     else if (!mouseholding && mousovering)
    26.         Gui.label(Tooltip!);
    27. }
    28.  
    29.  
    Notice, this would be a Drag 'n Drop inventory, not a click to pick up then click to put down system.

    Something like that, if you fill in all the values with your own, should work. Sorry for it being so messy, I'm not on my PC right now.
     
  7. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
  8. PvTGreg

    PvTGreg

    Joined:
    Jan 29, 2014
    Posts:
    365
    Thanks for this i have since restarted my inventory system and have implemented all of the things i needed it was due to the way i used the dictionary it kinda screwed me over
     
  9. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    No problem.