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
0
Question by MythStrott · Aug 07, 2010 at 03:40 AM · buttoninventoryguilayout

GUI Layout. Can I end and begin a horizontal group automatically?

Hey! I'm writing the script for my inventory.

What I'm trying to do is something like this:

function OnGUI()
 {
   GUILayout.BeginArea (Rect (0,0,400,));
     GUILayout.BeginHorizontal();
        if (hatchet > 0){
          GUILayout.Button ("Hatchet");
        }
        if (coins > 0){
          GUILayout.Button ("Coins");
        }
        if (logs > 0){
          GUILayout.Button ("Logs");
        }
        if (kindling > 0){
          GUILayout.Button ("Kindling");
        }
        if (fishingPole > 0){
          GUILayout.Button ("Fishing Pole");
        }
        if (rawSalmon > 0){
          GUILayout.Button ("Raw Salmon");
        }
        if (rawTrout > 0){
          GUILayout.Button ("Raw Trout");
        }
        if (fishingBait > 0){
          GUILayout.Button ("Fishing Bait");
        }
        if (cookedTrout > 0){
          GUILayout.Button ("Cooked Trout");
        }
     GUILayout.EndHorizontal();
   GUILayout.EndArea();
}

This isn't my actual code. The buttons are more complicated and have functionality. But this is the basics of my inventory script. What I want is to have a 5 x 8 inventory where the buttons all align to the top and to the left. I don't want them overlapping. I think this could be achieved by writing script that automatically places:

 GUILayout.EndHorizontal();
 GUILayout.StartHorizontal();

after every 5 items that exist in the inventory (greater than 0). How could I do this?

Thanks very much.

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

1 Reply

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

Answer by Eric5h5 · Aug 07, 2010 at 05:23 AM

I think it would be a lot easier with arrays:

import UnityEngine.GUILayout;

var inventoryItems : int[]; var inventoryNames : String[];

function OnGUI() { var count = 0; BeginArea (Rect (0,0,400,700)); BeginVertical(); for (v = 0; v < 8; v++) { BeginHorizontal(); for (h = 0; h < 5; h++) { while (count < inventoryItems.Length && inventoryItems[count] == 0) count++; if (count < inventoryItems.Length && Button (inventoryNames[count])) { // do whatever } count++; } EndHorizontal(); } EndVertical(); EndArea(); }

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 MythStrott · Aug 07, 2010 at 03:33 PM 0
Share

I haven't used arrays yet. I suppose now would be a good time to learn.

But before I start, will this make it possible for the buttons to align as I want? I looked at the script you wrote and it looks like your for/while is what takes care of that. Thanks

avatar image Eric5h5 · Aug 07, 2010 at 06:30 PM 0
Share

@$$anonymous$$ythStrott, as far as I know, yes...you could always test it and see. :)

avatar image MythStrott · Aug 07, 2010 at 07:34 PM 0
Share

Javascript array tutorial, here I come =D

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

Button in GUI BeginScrollView problem 0 Answers

proper inventory system issue.. 1 Answer

Unity android- GUILayout button not showing text? 1 Answer

Method is called, but GUI doesn't show up 1 Answer

Errors when using GUILayout in ModalWindow 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