- Home /
Making an Inventory (Basics)
Whoa, the examples I see for inventories around here are very confusing! I was reading through the Unity reference site about Gui basics, but got lost on grids. I understand making a Gui box, and making grids in general, but how do I make PAGES of different grids for one gui (inventory)?
Basically, here are my goals-
have a small box to display the current inventory Gui when clicked (a bag icon button)
if you click this, a new page pops up showing a box with a grid in it(or set up in a grid like fashion) for the inventory
if a player finds/nears an item (assigned with a specific texture) and presses a button, the grid squares are assigned the items texture
the item in the scene is destroyed
if you click the texture(item) in the inventory, you instantiate that item, and the texture in the inventory dissapears.
Also, in general, how do you go about putting a number on a button that starts at zero and is added/subtracted to when you do collect/withdraw the same thing multiple times?
And how do you toggle a gui's visibility? I know you can use enable=false; but wouldn't that restart the whole gui each time you close it?
When I did try out the grid example in the reference site, the buttons got stressed and smaller the bigger I made the string.. Why? Here it is again
var selectionGridInt : int = 0; var selectionStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];
function OnGUI () { selectionGridInt = GUI.SelectionGrid (Rect (25, 25, 100, 30), selectionGridInt, selectionStrings, 2);
}
Also, how come the first grid button is automatic? I tried this for pressing grid buttons
if(selectionGridInt == 0) {
//do something;
}
but 0 doesn't wait until I push it!
I know I'm asking a lot of questions, but the examples I find are giving me headaches. Any help is very much appreciated!
Answer by superventure · Jan 17, 2011 at 05:43 AM
I was thinking about deleting this whole question, but I will leave it with the answers I found for anyone else who might need this info.
For pages in an inventory:
enum Page { None,Open,Page1,Page2,Page3,Page4//etc however many pages you need }
private var currentPage:Page;
function Awake(){
currentPage=Page.Open;
}
function OnGUI(){
switch (currentPage) {
case Page.Open: Open(); break;
case Page.Page1: Page1(); break;
//case Page.Page2: Page2(); break;
//case Page.Page3: Page3(); break;
//case Page.Page4: Page4(); break;
}
}
function Open() {
if (GUI.Button(Rect (20,40,80,20), "Iventory")) {
currentPage = Page.Page1;
}
}
function Page1() {
if (GUI.Button(Rect (20,40,80,20), "Close")) {
currentPage = Page.Open;
}
}
This is basically a summary for the guide I found here link text
As for a grid with icon buttons, I found this very useful guide and example here
http://forum.unity3d.com/threads/11167-Laying-out-Icons-In-an-inventory-that-will-rearrange-to-fit
Answer by Berenger · Dec 29, 2010 at 08:39 AM
Hi
I've done something very similar few weeks ago, and I think I can give you some advices.
First, I read somewhere that buttons are way too expensive for that situation, where you can end up with 100 squares or so. So I just used labels and a simple code that tell me which square I am over (and maybe hilight it) :
int x = mousePos.x / squareWidth;
int y = mousePos.y / squareHeight;
// Might be a good idea to clamp those, btw
Then I have an array of size nbLine * nbColumn, so each square know what item it contain
Finally, when there is a clic, you can access the item with something like that
item = itemArray[ x + y*nbColumn ];
item.drop(); // or whatever
So, you can easily modify the number of squares in your inventory, the size etc. When you pick up something, you need to scan the itemArray to check which square is empty to add a new item. I think you've got anything you need with that.
About Gui visibility, enabled = false will disable the component, not the GameObject. That means each time you use enabled, the functions called are OnEnable and OnDisable, which you can override btw (is Start() called when you do gameObject.active = true ? I'm not sure ...). So it won't probably restart anything.
Hope that helps.
I don't understand most of this- How would I end up with more squares/slots than what I manually entered for the string? I thought you couldn't click labels??
What does this mean - int x = mousePos.x / squareWidth;
how would you set up the nbline/column?
You can't click labels, but you can know when the user clic. If you know which square is under the mouse (that's what i am doing with x and y, mousePos being Input.mousePosition) you can know which square is clicked. The nbline/column are variables you have to create (integers), and you can give any value you want, at least > 0.
I don't understand how to set this up, and from what I do understand I would ultimately just have a page with preset words/textures that I click to generate an item anyway. $$anonymous$$y goal is to 'generate' a texture (label/not) for the gui page when the player nears or finds an item, not have the gui page prefixed with items. I do still want to understand your suggestion, but again, you're going way over my head...