Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

[SOLVED] Simple UI - First Selected not highlighting, controller not being detected

Discussion in 'UGUI & TextMesh Pro' started by Comafly, Jan 25, 2016.

  1. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    So I've just got a simple game up and running at the moment; it controls with the keyboard or controller.
    I decided to give it a little menu system, but for the life of me cannot get it to work with the controller - the mouse works fine.



    As you can see in the preview here, I have the Event System set up to pick StartBtn as First Selected.
    I have the Horizontal and Vertical axes set to the inputs I created - which work fine with my game.
    And I have selected different colours for the Normal/Highlighted colors in the StartBtn and SettingsBtn component editor.

    Using a mouse, the menu works fine. They highlight and perform their functions when clicked.
    But it is just not detecting the controllers.

    Am I missing something super obvious here? Any help is greatly appreciated!
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
    What version are you using? I thought the first selected got removed a while back and in 5.3 they merged the input modules so that touch / keyboard and mouse use the same input module (touch alone was removed)

    Personally, for first selected I created my own version to set it on the event system on startup which works well.
     
  3. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Oh, it seems I'm still using 5.1. Guess I had automatic updates turned off for some reason.
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
    Not exactly a bad think, a fair few devs I've talked to have stopped at 5.2 due to issues in later versions
     
  5. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    I've updated my Unity to 5.3.1f1 and yes the touch and kb/m components have been merged, but First Selected is still an option, and still does not seem to work : /

    Also, if I remove the functions from the buttons so they don't do anything, and I click on either of them, THEN I can use the controller to navigate them. So the focus is obviously not being set properly. I'm going to go have a look around for setting button focus.
     
    Last edited: Jan 26, 2016
  6. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Alrighty, so I got it sorted. For whatever reason, First Selected was not doing its job. So I just coded it myself. It was rather straight forward:

    - You set the class to use the UI and EventSystems
    - You find the button you want to set the focus on.
    - You find your event system
    - You set the SetSelectedGameObject of your event system to the gameObject of your button.


    Code (csharp):
    1. using UnityEngine.UI;
    2. using UnityEngine.EventSystems;
    Code (csharp):
    1. Button btn = GameObject.Find("StartBtn").GetComponent<Button>();
    2. EventSystem es = GameObject.Find("EventSystem").GetComponent<EventSystem>();
    3. es.SetSelectedGameObject (btn.gameObject);
    You can just set public variables for your event system and button, and drag them in to the appropriate slots in your inspector, but I prefer to do everything by code so it's all contained in one place.

    Hope that helps anyone with the same issue.

    Cheers
     
    Last edited: Jan 26, 2016
    slander36 and SimonDarksideJ like this.
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
    Yup, that's pretty much what I usually do. Although I normally just create a property in the script and attach the script to the event system to avoid all the lookups :D