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!
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.
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.
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.
using UnityEngine.UI;
using UnityEngine.EventSystems;
Button btn = GameObject.Find("StartBtn").GetComponent<Button>();
EventSystem es = GameObject.Find("EventSystem").GetComponent<EventSystem>();
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.
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