Multiple Buttons next to eachother

Hey Community,

i want to order 5 Buttons in a row next to each other. They should always fill the hole screen (so everyone ist 20% of screen width) and the buttons should have the same heighr as width.

Is it possible to realize this without coding just by anchros and pivot-points?

Hope for some creative ideas.

Otherwise how would you do it by code?

Best regards.

Yep - make a canvas and put a panel on there that’s as big as you want. Put a vertical layout group and tell it to expand children on width and height (should be by default).

Create a prefab Button and drag the panel and button onto the public slots in the script below.

I use a lambda to identify the scripts, coincidentally it creates 5 so didn’t even need to edit it:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class LambdaButtons : MonoBehaviour {

    public GameObject prefabButton;
    public RectTransform ParentPanel;

    // Use this for initialization
    void Start () {

        for(int i = 0; i < 5; i++)
        {
            GameObject goButton = (GameObject)Instantiate(prefabButton);
            goButton.transform.SetParent(ParentPanel, false);
            goButton.transform.localScale = new Vector3(1, 1, 1);

            goButton.GetComponentInChildren<Text>().text = "Button " + i.ToString ();

            Button tempButton = goButton.GetComponent<Button>();
            int tempInt = i;

            tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
        }
    }

    void ButtonClicked(int buttonNo)
    {
        Debug.Log ("Button clicked = " + buttonNo);
    }

}

There’s an issue with the way looped values get processed so to write the lambda you have to create a temporary button and int to pass it back, bit of a pain but it works and supposedly doesn’t affect 5 but not used 5 yet so can’t say for sure.

Thanks very much. Just replaced the vertical-layoutgroup by a horizontal. And make a few custom changes.

Thanks again.

Best regards.