Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UI Button Show selected State

Discussion in 'UGUI & TextMesh Pro' started by gn.gametorque, Feb 13, 2015.

  1. gn.gametorque

    gn.gametorque

    Joined:
    Feb 13, 2015
    Posts:
    26
    Hi,

    I am working with Unity UI and wish to show the selected state of a button.

    Like

    Button.selected = true; // this will start showing the selected state.

    thanks
    gn
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Are you sure you don't mean a toggle control?
    Buttons only have a click state.

    Or do you mean when the button control is in focus?
     
  3. gn.gametorque

    gn.gametorque

    Joined:
    Feb 13, 2015
    Posts:
    26
    I have a list of Buttons in ScrolBar and when ever I click a button I display its contents in the content area.
    Now I wish to display the selected state of button as its contents have been displayed.

    I dont mean to say about a Toggle Button.... I am using Simple Button.
     
  4. gn.gametorque

    gn.gametorque

    Joined:
    Feb 13, 2015
    Posts:
    26
    Button should have an option or property so that we can display its selected state.
     
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Then you should have a State manager script and each button's onclick can additionally update the state manager with it's name.
    Remember with the UnityEvent property draw, you can add multiple actions for a single event:

    Action 1: Update Content Area
    Action 2: Update state manage with selected state.

    So rather than trying to morph the button, use a manager.
     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    It does: mybutton.Select();
    It will display what you set in its highlighted field in the inspector. This should work for all UI components that inherit from UI.Selectable. Check out the doc and look at the button info.

    They have a pressed state, not really a click state. It has more states as well, and it varies depending on the transition you pick, it can also have no states for a simple button.
     
  7. gn.gametorque

    gn.gametorque

    Joined:
    Feb 13, 2015
    Posts:
    26
    it works fine. I have sub menus after clicking a main menu button, and when the selected state of child menu button displays the parent menu button looses its selected state.
    I am thinking to use a toggle button and set manually isOn property for each click of all its Buttons?

    Is there any other approach to do this?
     
    Last edited: Feb 19, 2015
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Toggle button would be the best approach. Remember that the new UI is new, and they don't have very many controls setup. You may have to create your own controls that you need.
     
  9. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    I'd agree with @Chris Trueman on that one, the toggle would seem to suit your requirement better,
    Be sure to use a Toggle Group as well, if you only want one toggle to be active at a time.
     
  10. gn.gametorque

    gn.gametorque

    Joined:
    Feb 13, 2015
    Posts:
    26
    Using Toggle Group is the best way to handle my problem.

    Thanks everyone. :)
     
  11. Anjamma

    Anjamma

    Joined:
    Mar 24, 2017
    Posts:
    1
    I have to use normal buttons only but how? I didn't get anything..
     
    AvalinAtWork and mervinroy like this.