Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Reizka · May 02, 2016 at 10:34 AM · guibuttonsdynamicguilayout

Dynamically creating buttons with GUILayout.Button and sprites to textures

Okay, I'm still relatively new to Unity3d and trying to figure out the best way to do certain things with it.

For instance for buttons there seems to be multiple different approaches. I'm currently trying to generate a set of buttons dynamically that would be used to control the player's character. Dynamic approach is necessary as the set of available commands is rather fluid.

I would essentially like to have them in three columns on the left side of the screen.

I managed to create the buttons, but am struggling with adding images to them. I have a spritesheet with several different button types and states inside it, but GUIlayout.Button doesn't seem to want to have anything to do with them. I tried converting sprites to textures, but Unity seems to add image of the whole spritesheet into the button instead of a single sliced image from within the sheet.

Anyways. To sum up my questions.

  1. How would I go about making several vertical columns instead of single long one?

  2. for the texture issue, do I have to cut the buttons I want to use from the spritesheet myself, outside unity?

Here's also my current code, I'm sure there are smarter ways to do these things. Please feel free to tell me how :)

 GUILayout.BeginArea(new Rect(15, 15, 200, 600));
         GUILayout.BeginVertical();
         btnHeight = 40;
         btnWidth = 40;
 
         for (int i = 0; i < movesAvailable.Length; i++)
         {
             GameObject gobj = Resources.Load("MainGame/GUI/buttons/" + movesAvailable[i]) as GameObject;
             SpriteRenderer rend = gobj.GetComponent<SpriteRenderer>();
             Sprite sprite = rend.sprite;
             GUILayout.Button(sprite.texture,"button", new GUILayoutOption[] { GUILayout.Width(btnHeight), GUILayout.Height(btnWidth) }); 
         }
 
         GUILayout.EndVertical();

Cheers!

Comment
Add comment · Show 2
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 Cherno · May 02, 2016 at 11:29 AM 0
Share

The User $$anonymous$$anual and Scripting API have information on how to use the various GUILayout functions, including Horizontal and Vertical areas as well as Grids.

avatar image Reizka Cherno · May 03, 2016 at 01:42 AM 0
Share

On the documentation I've only noticed the functions that you can see me using in the script that I posted: "BeginVertical/Horizontal" I was mainly wondering whether there is a smarter way to go about doing this, since with these functions I have to make my own internal counter and endVertical/horizontal checkers, to get the rows and columns of buttons to manifest correctly.

2 Replies

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

Answer by Outliver · May 02, 2016 at 02:10 PM

  1. Create a prefab for your button. Regarding the sprites, read the docs. I'm pretty sure, there's some how-to guide on that out there.

  2. Create a Horizontal Layout Group and put your buttons into that group. You may then adjust the settings in inspector. At least that's the way I use to manage dynamically created buttons. You won't have to use GUILayout.BeginVertical(); until you're doing editor scripting.

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
0

Answer by Reizka · May 12, 2016 at 02:49 PM

Thanks for the answers!

My solution in the end was bit different. Not sure whether its the smartest way to do this, but so far it seems to work.

I made a gameobject that holds an grid layout group and a script that manages the buttons to be created for each game view.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUILayout three buttons alligned horizontally with no spaces between them 1 Answer

Determining GUI button pressed 2 Answers

GUI Texture Changes Position Depending On Screen Resolution? 1 Answer

GUI.enabled ? 0 Answers

How will i develop the type of gui? 0 Answers


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