Search Unity

Tutorial - Inventory System with the new UI System 4.6

Discussion in 'UGUI & TextMesh Pro' started by Sander1991, Aug 25, 2014.

  1. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Hey,

    I have started a tutorial series where I create a Inventory System with the new UI System.
    Craft System(works with inventory):
    • Part 1(Creating the whole craft system):

    • Part 2:


    PS.: Im sorry for my bad english I hope it is good enough for you guys ;). Ask if you have some questions.
     
    Last edited: Sep 10, 2014
    DylanYasen, melkior, heemun and 3 others like this.
  2. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    I recommend you add a Layout - Grid Layout component to the inventory panel instead of coding the slots' positions. This way it's very easy to adjust the slot size and offset from the Inspector.
     
  3. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    OK thank you very much.
     
  4. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    Thanks for this alot, we really need some practical tutorials on uGUI.
     
  5. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    I doing another one with dragging item from slot to slot..I hope I gonna finish it this evening.
     
  6. DoubleLee

    DoubleLee

    Joined:
    Apr 11, 2014
    Posts:
    34
    Are you going to make a video about using grid layouts, that seems to be the preferred way to handle placement of slots. I'd like to see it in action.
     
  7. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    I gonna do it later I guess. I will finish the inventory, which is going pretty fast I guess and than I gonna do another video where I optimize everything.
     
  8. raistlinthe1

    raistlinthe1

    Joined:
    May 29, 2014
    Posts:
    1
    Zee Germans always apologizing about their "bad" English
    Gotta love them ;)
     
  9. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    :D I understand english perfectly but I do not speak it often. It is a different to hear a word and knowing what it means and speaking with someone where you have to know the words. But I notice that am getting better from video to video.
     
  10. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Dragging Item is in as a tutorial aswell...
     
  11. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    These are fantastic, thank you for sharing :)
     
  12. royal_m

    royal_m

    Joined:
    Aug 13, 2014
    Posts:
    3
    Thank you for this wonderful tutorial implementation of the new ui tool
     
  13. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Thank you. I gonna create another video, after the inventory tutorial, where I gonna optimize everything so that the inventory is good perfomance wise. And later I gonna create a environment panel - like in DayZ - which will show dropped item near you.
     
  14. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Added another video about stacking items and drinking consumable.
     
  15. royal_m

    royal_m

    Joined:
    Aug 13, 2014
    Posts:
    3
    Always as well. Deeply the continuation...
     
  16. bwheatley

    bwheatley

    Joined:
    Jun 23, 2012
    Posts:
    45
    A download link coming so we can check out the code in a little bit more detail?
    Either way thanks for the work it's been very helpful.
     
  17. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    To be honest. I do not like the guys who only come for downloading the sourcecode ;)(no offense). I know what you mean but this is not the intention of this tutorial. People have to watch the video. Maybe I gonna attach it at the end of the video, that you can click onto it like the "part 2 ---->" things.
     
    ManAmazin likes this.
  18. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Added Part6 with Dropping/Picking Items, environment-box like in DayZ.
     
  19. royal_m

    royal_m

    Joined:
    Aug 13, 2014
    Posts:
    3
    Thank you for the new video
     
  20. heemun

    heemun

    Joined:
    Apr 15, 2014
    Posts:
    18
    Great tutorial, thanks man.

    Btw, i've got a question: i want to drag item in the inventory like drag and drop, i mean i press the button, move the item to another slot (button is still pressed), release the button and item move from inital slot to the one which over i released the button.

    I can't do that. I tried to use OnEndDrag method, but it has slotNumber of initial slot, not the one i finish dragging. do u have any ideas how to do that?
     
  21. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Mh...I gonna look for it. To be honest I do not know it now. But I gonna post a solution when I got one.
     
  22. heemun

    heemun

    Joined:
    Apr 15, 2014
    Posts:
    18
    i think i've found it:

    public void OnEndDrag(PointerEventData data)
    {
    int targetSlotNumber = slotNumber;

    if (data.pointerEnter.GetComponent<SlotScript>() != null){
    targetSlotNumber = data.pointerEnter.GetComponent<SlotScript>().slotNumber;
    }

    if (inventory.items[targetSlotNumber].itemName == null && inventory.draggingItem)
    {
    inventory.items[targetSlotNumber] = inventory.draggedItem;
    inventory.stopDraggingItem();
    data.pointerEnter.GetComponent<Image>().color = defaultSlotColor;
    }

    }

    this works fine, except
    data.pointerEnter.GetComponent<Image>().color = defaultSlotColor;
    this string.

    I paint an empty slot with green color and a filled slot with red color. I do it in OnPointerEnter method. Then i set the default slot color in OnPointerExit method (like when dragging an item you can see if you can put your item in this slot).

    It works fine with an empty slot, cuz data.pointerEnter return Slot GameObject. But a filled slot return ImageIcon, so the filled slot remained red,after Drag ends. I'm confused...
     
  23. LuxUnity

    LuxUnity

    Joined:
    Sep 29, 2010
    Posts:
    717
    Hi, and thank you for the tutorials, but I found a bug-problem with your solution.

    If the canvas have reference resolution component the tooltip and the mini drag-drop icon position are wrong.
     
  24. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Actually I do not know what you mean. I do not get this bug. Do you have a picture or something?
     
  25. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    I gonna do something like that soon. The problem is that I have to learn for my university ;). But anyways thanks for posting this script. Gonna test it soon.
     
  26. heemun

    heemun

    Joined:
    Apr 15, 2014
    Posts:
    18
    well, it seems i found kind of solution.

    i use static variable GameObject wrongSlot;

    In OnPointerEnter method i check if slot isn't empty, paint it to red and remember this slot GameObject:
    if (inventory.items[slotNumber].itemName != null && inventory.draggingItem)
    {
    gameObject.GetComponent<Image>().color = Color.red;
    wrongSlot = gameObject;
    }

    In OnEndDrag method i check if the object has SlotScript component (it means it's a Slot object and pointed slot is empty, otherwise it is an ItemIcon object and pointed slot is filled):

    if (data.pointerEnter.GetComponent<SlotScript>() != null)

    If it is true i get the slotNumber of empty slot and paint it to default color:

    targetSlotNumber = data.pointerEnter.GetComponent<SlotScript>().slotNumber;
    data.pointerEnter.GetComponent<Image>().color = defaultSlotColor;

    otherwise i paint the object i stored before:

    wrongSlot.GetComponent<Image>().color = defaultSlotColor;

    and in any case i put item in the proper slot (the one we point or to the initial slot if there is an item in the pointed slot) and stop dragging:

    inventory.items[targetSlotNumber] = inventory.draggedItem;
    inventory.stopDraggingItem();


    It works perfectly, but i'm not sure if it's a "clean" solution, guess there's something better and simplier. Anyway, i hope it will help someone.

    PS Sorry for flooding so much in ur topic^^

    PPS am i the only one who don't know how to post the C# code well here?
     
  27. LuxUnity

    LuxUnity

    Joined:
    Sep 29, 2010
    Posts:
    717
    When drag:

    reference resolution off (work fine)


    reference resolution on (bad position)


    In both images mouse is at the center of the slot.
     
  28. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Added another part about gearing system for your character. So that you have only one slot for gearing a chest, head, shoes and so on and we fixed some small bugs.
     
    eses likes this.
  29. heemun

    heemun

    Joined:
    Apr 15, 2014
    Posts:
    18
    thanks for ur new vid.
    Btw, i think it's better to use foreach loop to look for smth in database:
    foreach (Item item in database.items)
    {
    smth...
    }

    instead of

    for (i=0; i < database.items.count; i++)
    {
    smth...
    }
     
  30. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    You think or you know?^^
    I actually do not know what perfomance wise is better. Someone knows it?
     
  31. heemun

    heemun

    Joined:
    Apr 15, 2014
    Posts:
    18
    you need a little less code with foreach. You don't need i counter, you don't need to check List.Count in the loop head and you don't need to create a new Object in the loop, cuz u already created it in foreach().

    I'm not sure there's much difference in performance though...
     
  32. Druss

    Druss

    Joined:
    Jan 13, 2014
    Posts:
    3
    Hi Sander,

    first of let me say thank you very much for this great tutorial. It is pretty much appreciated!
    I noticed when you click on the left top of an inventory Slot and do some sort of back and forth while clicking, the consumables get duplicated. I tested this quiet a few times so it happens all the time. Did this occur to you too ?

    Thanks for your reply!

    Keep up the good work ! If you are bored and want to expand this an implementation of a vendor in this tutorial would be absolutely awesome !
     
  33. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    The problem is that there is no right click at the moment. This will be fixed soon. So atm dragging and consuming consumables with left click. So dragging = consuming...
    When it will be fixed by Unity I gonna do this with right clicking(consuming) and will do another video for it ;)
     
    Last edited: Sep 8, 2014
  34. jebi

    jebi

    Joined:
    Jul 7, 2013
    Posts:
    6
    Thank you man! i'll watch.
    Also.. can you make tutorial about hud text?
     
  35. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    What do you wanna see exactly with texts?
     
  36. jebi

    jebi

    Joined:
    Jul 7, 2013
    Posts:
    6
  37. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    jebi likes this.
  38. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    CraftSystem is up aswell. Working with the inventory System.
     
  39. Druss

    Druss

    Joined:
    Jan 13, 2014
    Posts:
    3
    Cool ! Thumbs up.
     
  40. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    Thank you very much. Further videos will come after my exam time.
     
  41. Sander1991

    Sander1991

    Joined:
    Aug 25, 2014
    Posts:
    162
    I know that you guys thought that I would do some more tutorials. At the moment I have to do some stuff for my university and I hope that I can finish it pretty fast. Will keep you on update ;).
     
  42. Paffer

    Paffer

    Joined:
    Mar 7, 2013
    Posts:
    6
    For someone reason on Part 5 it doesn't behave like yours, it doesn't add the items to the empty slot.

    Code (csharp):
    1.  
    2.     private void CheckIfItemExists(int id, Item item)
    3.     {
    4.         for(int i = 0; i < items.Count; i++)
    5.         {
    6.             if(items[i].itemID == id)
    7.             {
    8.                 items[i].itemAmount = items[i].itemAmount + 1;
    9.                 break;
    10.             }
    11.             else if(i == items.Count - 1)
    12.             {
    13.                 AddItemToEmptySlot(item);
    14.             }
    15.         }
    16.     }
    17.  
    18.     private void AddItem(int id)
    19.     {
    20.         for(int i = 0; i < _database.items.Count; i++)
    21.         {
    22.             if(_database.items[i].itemID == id)
    23.             {
    24.                 Item item = _database.items[i];
    25.  
    26.                 if(_database.items[i].itemType == Item.ItemType.ItemResources
    27.                   && _database.items[i].itemType == Item.ItemType.Miscellaneous)
    28.                 {
    29.                     CheckIfItemExists(id, item);
    30.                     break;
    31.                 }
    32.                 else
    33.                 {
    34.                     AddItemToEmptySlot(item);
    35.                 }
    36.             }
    37.         }
    38.     }
    39.  
    Basically it doesn't even do this
    Code (csharp):
    1.  
    2.             else if(i == items.Count - 1)
    3.             {
    4.                 AddItemToEmptySlot(item);
    5.             }
    6.  
    7.  
    Any solution ?
     
    Last edited: Oct 14, 2014
  43. Alkanine9

    Alkanine9

    Joined:
    Mar 5, 2014
    Posts:
    11
    Could you please add a chest or backpack system?
    Like how chests in Minecraft work, you can put items in it and take items out of it.
    Backpacks would be similar, just that they open up an extra inventory.
     
  44. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I dont know if anyone who did this tutorials hv this problem. All my inventory is working the only problem i hv is when i drag the item to the character window slot, the item goes to proper place but the icon doesnt show up in the character menu. I check all code and i dont find anything.. i dont get any console error or message too.. Can anyone help me plz? Everything works perfectly in the inventory one

    i try ask help here in forum but seams no one know wts going on. If u can take a look i will appreciate Sander1991

    http://forum.unity3d.com/threads/character-window-issue.312697/#post-2031654
     
    Last edited: Mar 22, 2015
  45. RastaRambo

    RastaRambo

    Joined:
    May 10, 2015
    Posts:
    1
    I love you bro <3333333
     
  46. tani41

    tani41

    Joined:
    Feb 23, 2016
    Posts:
    10
    how to make unity UI inventory like hitman blood Money in a sphere shape weapon selection menu?