Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by superventure · Dec 29, 2010 at 07:17 AM · menugridinventoryitempage

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-

  1. have a small box to display the current inventory Gui when clicked (a bag icon button)

  2. 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

  3. if a player finds/nears an item (assigned with a specific texture) and presses a button, the grid squares are assigned the items texture

  4. the item in the scene is destroyed

  5. 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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superventure · Dec 29, 2010 at 09:11 AM 0
Share

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?

avatar image Berenger · Dec 29, 2010 at 09:28 AM 0
Share

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.

avatar image superventure · Jan 08, 2011 at 04:57 AM 0
Share

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...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

OnTriggerEnter / Inventory question?! 1 Answer

How to combine items in inventory with words 0 Answers

how to detect wich item i have in my inevntory 0 Answers

UI: Dynamically sized inventory window with scroll 0 Answers

Creating a dynamic list in OnGUI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges