- Home /
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.
How would I go about making several vertical columns instead of single long one?
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!
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.
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.
Answer by Outliver · May 02, 2016 at 02:10 PM
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080005im_/https://answers.unity.com/themes/thub/images/avi.jpg)