Search Unity

Set Selection State w/ Code? amidoingitright?

Discussion in 'UGUI & TextMesh Pro' started by p87, Aug 23, 2014.

  1. p87

    p87

    Joined:
    Jun 6, 2013
    Posts:
    318
    I'm trying to figure out how to programmatically set a button to "selected", it has proven to be harder than expected. Or at least, not very obvious.

    I am currently calling eventSystem.SetSelectedGameObject.
    For the arguments, I am giving it
    1. my GameObject with the Button component,
    2. new BaseEventData(eventSystem)

    It seems to work, but feels unusual that i have to pass this second argument though. Is this right?

    Are there any videos or tutorials anywhere that explain the "Selectable" stuff, and UI navigation? I saw this video, which explained a bit about the event system. Otherwise the docs that shipped with 4.6 are kind of light on this new UI navigation stuff. Or, I missed it.
     
    Last edited: Aug 23, 2014
    username132323232 likes this.
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Well we haven't finished the docs yet :)

    So this is correct currently, we are considering removing the second argument to the function as it doesn't make much sense.
     
    p87 likes this.
  3. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    I am just solving same thing. Why I have to change selected UI by code? Some type of drag and drop solution could be better. I pass as second argument null :)
     
    Last edited: Aug 23, 2014
  4. p87

    p87

    Joined:
    Jun 6, 2013
    Posts:
    318
    Thanks Tim. I agree on the second argument being confusing. It's purpose is unclear to me. (Curious, what is it's purpose?)

    You can set the object that should be selected by default on the EventSystem component. It has a StartSelected property or something like that.
     
  5. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    Of course, but I think change focus when I go to another menu. From Main menu to Options for example. I know that it is super simple script, but integrating this function could be better :)
     
  6. p87

    p87

    Joined:
    Jun 6, 2013
    Posts:
    318
    Yeah, the docs are light on the user interface navigation info.

    I think it makes sense to NOT automatically select user interface objects, though. Otherwise you would have to write lots of annoying code to deselect things (if the behavior was not what you wanted.)
     
  7. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    66
    NOT automatically, but by user setting. For example as properties in Button in OnClick(). nGUI has something similar.
     
  8. FirstWaveGames

    FirstWaveGames

    Joined:
    Oct 15, 2013
    Posts:
    25
    I've been playing around with this, I have my main menu and then I select options and I make the options canvas active.
    (I'm using a gamepad controller btw)

    I use this script as a component in my button:

    Code (CSharp):
    1. public void Select(GameObject selection)
    2. {
    3.     eventSystem.SetSelectedGameObject(selection,  baseEventData);
    4. }
    then I call this from the button On Click () in the inspector.

    What happens is that it select the new object but the old object stays selected forever and ever...

    is this a bug?
     
  9. FirstWaveGames

    FirstWaveGames

    Joined:
    Oct 15, 2013
    Posts:
    25
    ok, nevermind
    I've found the error

    I was activating/deactivating the object of the canvas that contains each submenu,
    instead now I enable/disable the canvas component and it works fine