Search Unity

{Urgent}How to create the Dropdown button with the random option

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by schetty, Aug 22, 2016.

  1. schetty

    schetty

    Joined:
    Jul 23, 2012
    Posts:
    402
    Hi,
    I need to create the dropdown button button like this:
    RefernceButton.png
    I have button, along with the button i have a 3 sub buttons which have a option and next to the i have a one more button called expand, if user click that more button should be popup this button will create dynamic. when i click the any button corresponding game object will spawn.

    I have figured out how to integrate the functionality, but i dont know how to create this button.
    Can anyone please help me how to create the buttons like this?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,666
    Not sure I completely understand, but if you have 3 buttons and clicking on one expands out a menu of buttons, just use the base button uGUI. Then create a panel with buttons on it as children. You can turn on this panel if you want based on the button that is pressed or you can do an expanding button panel by anchoring it at the top and scaling it down, then scale it up when a button is pushed so it "expands" from the top.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Create a Panel in the Editor. Add a Grid Layout Element to it. If the "More Buttons" are well defined before the user starts the program Just add them as button Children to the panel and set up all their options.

    if not create a Template Button ( call it whatever you want... e.g MyButtonPrefab) and make it a Prefab. This prefab should have a Layout Element added with min width/height set, for the Panel's grid Layout to work with. Then Create a Script and Attach it to the panel. One of the public functions of that Script should be to AddButton. Here is an C# example of doing that.
    1. public Button AddButton() { // Optionally add a lot of arguments to configure the button
    2. GameObject newButton = Instantiate(MyButtonPrefab, this.transform) as Button;
    3. //Here is where you would use all the arguments you pass in to configure the button
    4. newButton.rectTransform.sizeDelta = Vector2 variable;

    5. return newButton // so whatever function calls this can get an index into the children for *THIS* button
    6. and then you could setup all the features of the button in your other script that called this one to create a new button
    Start off with your Panel unchecked in the Unity Editor (Script Equivalent is Panel.gameObject.SetActive(false);
    Then Have your more button's OnClick Event just Set the Panel to Active. Make sure that all the buttons OnClick's are not only set to to do whatever custom thing they do, but also to set the Panel(their parent) back to SetActive(false);