Simple Inventory UI in Unity With Grid Layouts
Вставка
- Опубліковано 3 лип 2024
- ➤Previous Inventory System Tutorial (Watch First): • Flexible INVENTORY SYS...
Creating an inventory isn't so hard, but displaying it in a nicely presented way can be... but its also a lot of fun.
There are so many UI options for an inventory that can all function differently that its really hard to make a tutorial for it, but I figure most people atleast just need to be able to display inventory slots inside some larger container.
Using Layout groups (grid, vertical, horizontal) can give you the grid your looking for in Unity.
By the end of this unity tutorial you'll be in a good place to expand your Inventory UI in any direction you want.
✨Want to support the channel?
Buy me a coffee ☕: ko-fi.com/bmoli
➤SUBSCRIBE to inflate my ego with a bigger number 💖
➤LIKE the video if you enjoyed, it really helps the channel!
➤Join our DISCORD SERVER and become the worlds first trillionaire: / discord
Thanks for watching!
#unity #tutorial #inventory #bmo
Wow, I can't believe this doesn't have more views! This is the shiny visual reward for all the coding in the other parts of the series! Great tutorial, my brain is struggling a little will the abstractions and excessive use of the word "inventory", but I think I get it. I needed to invoke the OnInventoryChange change event in the Start method of my Inventory class to get the empty slots to draw before picking up any items. In addition to just making something functional, I feel like this series helped me understand events and scriptable objects too. Thank you!
Love the layout suggestions, the easy way you explain ideas makes all the difference! Always look forward to your posts 👍
This was exactly what I needed to put the finals pieces of this together. Some key parts I was totally not understanding
Absolutely amazingly clear, concise, and precise presentation on how to establish an inventory system. I wish I had seen this a year ago for my own project. Thank you!
Feels like your channel needs more visibility brother. Nice explanation, straight to the point and with high quality.
Some core fundamentals here man. Enjoying the recent content brother, always well done
Excellent pull together of these elements.
Good job
Great video, thank you!
Thank you man!
Really like the way to make your videos. Keep up the good work!
Thanks!
Hello! Awesome video, I have been watching in order the last few for a project. Thank you for the great content.
If you share your scripts i can take a look
Amazing work and on such a beginner friendly level compared to other inventory tutorials and guides out there! But could you go over Item drag and swap?
Do you have your code anyway avaliable for download on a github or g drive so we can compare (when we go wrong) without having to pause the videos?
Thanks
Thanks for this, I found you go quite quickly coding into the editor but suppose I need to get better! :)
Well done! Can u explain how the canvas work with different screen ratio and how to set it up to make it scales right with all the ratio?
How big would an inventory need to be before you looked at different ways of drawing the slots? And do you have any suggestions on where to look into a more efficient way? I was planning on having an inventory open/close and only updating when the inventory has changed.
Hi! I've been following your tutorials and they've been really helpful! I appreciate that you explain why you make the choices you do, and what the drawbacks might be. I do have one question though: do you have a recommended resource/video to learn a different way to update the inventory instead of destroying and rebuilding? Thanks!
How would I go about making an "equip item" system with this inventory?
I didn't know Grids, and grid components were literally default things Unity offered. I've been dreading making a Grid script that could be flexible for my needs... I might still need to do that but at least I know I have options!
You can't use this system to make max stack right. like in Minecraft for example. having 2 stacks of cobblestones. as the dictionary key is the item you cant do like if I have 64 off 1 item it's going to spill over to the next inventory item in the list. or am I missing something? I really appreciate any help you can provide.
Great tutorial, everything was easy to follow and modify but one thing I don't get is: How can I implement the Remove function? been trying with input and buttons but I just can get the "how".
How would I make more than 1 item show in the inventory because when I did it the gem worked fine but i dont know how to make any other item show. Would I have to copy the code for the gem and change some names for it to works on any other item?
How can i add more items?
I made it so the inventory is opened when player presses down the "I" key but whenever I go to pick up an item with the inventory closed, the inventory doesn't update. When I walk over the item with the inventory open, the inventory does update tho.
Could you do a video showing the using of items? Like a helmet, that shows up on the player that can also reappear in ui when unequipped. And the consumption of items like a healing potion? Thanks!
Yes totally. I've managed to get my inventory working (in that it starts generating the icons in the inventory), but I am struggling now to remove items (icons) from the inventory when it needs to. I've set my code to "destroy(the icon prevab);" but for some reason the icon doesn't go away from the inventory :(
@@RudolfJvVuuren did you figure it out?
That was very helpful but how do you actually remove an item from the inventory?
Yes, that's what I also want to know now lol
when I pick up an item the ifrst time, the icon name and stacksize wont pop up in my UI. I copied everything what he did. What should I do?
Will this save from scene to scene ?
Nothing stays from scene to scene in Unity... unless it is in a scene or gameobject that doesn't get destroyed when transitioning to a new scene. Another way is to use non-MonoBehaviour vanilla C# classes that live only in memory, and keep your inventory data there, and rebuild/populate the UI after loading a new scene. Look into topic of serialization/deserialization (to files) if you want to save your inventory items permanently to disk and then load them back at later time.
@@322ss thank you :)
And how to add more different
Items?
Im not catching on? When did we make the inventory item script? Edit: It was another lol
I hope that i will be as good as u :o maybe one day i will :D
How can I make an image of a 3D object? I have an inventory GUI with 4 slots.. If the player picks up a knife in the game, how do I get an image of that knife to display in my inventory?? I assume I have to get an image of it, but how do I do that? I can seriously only think of getting a screenshot and then editing it in something. That sounds TEDIOUS for multiple items and images..
Hello, thanks for this nice tutorial,first. I’m learning a lot. When I play the scene my inventory redraw another new inventory just down the old one after getting the first item, and when I go tho pickup the next item Unity breaks:( Somebody know what could be happening? Thanks a lot.
Finally fixed! Thaaaaaanks
@@davidbzrz it might be helpful for others, who could be in the same situation, to explain how you fixed it ;-)
@@davidbzrz I have the same issue, how did you fix it?
Alright, this aint good I think. On the other video I was able to expand on the idea - this one feels like I got knee capped. I think I'm getting PTSD for multipart tutorials cause I keep getting the rug pulled out, thankfully this was two parts.
I posted on the previous one a method to have mutliple inventories using the system BMo made. The system from the previous video is very simple and a great structure for evolving new ideas from. This one is riddled with just too many pitfalls:
Destroying the whole inventory and instantiating it when a new item is added, the problem isn't the efficiency for me it's how do you even setup a modular way to adjust the max inventory? Its baseline really bad to just put in an integer for something like that. I tried the obvious of making an int but Unity doesn't like that, so I tried a getter setter, but his system doesn't like that down the road when you are instantiating the entire thing again. So I tried using a lamda at cache no go. Then I said screw it let's make a Start for it and set things up there.
It worked fine but because it is happening at Start the first item is the only item it keeps populating to the inventory rather than what is actually in the inventory. This has me totally at a loss XD. Anyway, broke the whole damn thing in the end even the part that was working from the last video, feels bad but moving on - thanks for the attempt though man.
did you find a better solution?
@@danielrosenberg7174 Mhm,
Inventory is complex, I'll be short and clear about this:
Pre: Assuming you have the item data setup already.
1. Create a class for slots, give it all the data you want the slot to have. (Image, Stack Count, etc) This slot will also have methods to handle interactions with the slot. (Clear Slot, Populate Slot, Check Slot, Swap Slots, etc). I also had an item type which I could refer to if I wanted a specific slot to only allow a specific type of item. (Equipment).
2. Make a monobehavior for the Inventory and put a list of the type of your class for slots. I'd give this list a max size so as to allocate memory for better performance. Put methods in that to populate the list. Inventory is very generalized; it's however you need it to be but generally I found the methods I used were: AddSlot, RemoveSlot, TransferSlot.
All in all, it's not impossible but it certainly is a complex thing to work out.
After this I made 2 visualization monobehavior classes, one for the inventory the other for the slot.
@@dibaterman this is great. thanks for the long answer. to add to what you're saying. when I add an item in to this. how do I check if a slot already contains that item and is not full?
@@dibaterman Thank you for this! Would you be willing to share a code example? Or was there a video you watched to get this working? I am trying to follow your instructions, but I feel like I might be too beginner just to go off of the description.
@@danielrosenberg7174 Sorry didn't see this, you probably have an answer already, but if not: First understand that your inventory is a List/Dictionary/Array (whatever you chose). Second understand that you don't need to search the list since you've visualized it as a UI.
So what you want is something that you can attach to a hidden slot. The slot doesn't have to be hidden though, it could be a slot that follows the mouse when you have the inventory open. The idea is you click the visualized slot, and the slot just needs a button on it that triggers when clicked. (You can also create another UI element that pops up when clicked and add context there). Then just copy the data from the slot to the mouse slot/hidden slot.
Then click the slot you want to put things on, and have a onclick check/condition that if your hidden slot is populated then it will either add if the current (clicked) slot is empty or transfer.
Transfer is simple, just make a data type to hold transfer data for that slot. So within the transfer method initialize a new slot for data, store the old data in the new slot, then put the slots current data into the hidden/mouse slot data and finally put the new slots data(your old hidden/mouse data) into the current slot.
My problem was that slots item didn't get .enabled = true.
So nothing appeared in my inventory.
I solved by looking 11:41. My for loop was problematic.
I wrote (int i = 0; i > inventorySlots.Capacity; i++). The > was on the wrong side.
Solution : for (int i = 0; i < inventory.Count; i++)
with the < on the good side.
Best video on the topic btw ;)
You probably know by now, but to avoid little mistakes like that, in VS you can just type 'for' then double 'TAB' to auto create the loop, then just add your variables.
Great video - Thank you!