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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Buttons etc. on a system without a mouse or touch

Discussion in 'UGUI & TextMesh Pro' started by GameMechanics, Nov 5, 2015.

  1. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
    On systems with a mouse or touch screen, UI Buttons work well. On a system with a GamePad or Joystick, I am at a loss. I move a fake cursor around with a DPad or Joystick, but I cannot find a way to control the highlighting. I had my own version, but opted to use the Unity version. I think I need to go back to my version unless there is a way to control the highlighting and selection. Is there something I am missing?
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    If you want to stick to the new Unity system you'll likely want to make your own Input Module. Depending on your needs you may also want to create a custom Event System, though for things like highlighting I don't think it's needed.
     
  3. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
    That stuff is like using a nuclear bomb to hammer in a nail. If there isn't a simple answer (which there should be), I can remake the button object way faster than learn something new in Unity.

    It is an option though. Thanks.
     
  4. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Sure, but would it be as robust and versatile? Would learning it now cost you more time than it would save in the long run?

    Those aren't rhetorical questions; perhaps in your case it makes sense to roll your own solution. It is worth keeping in mind though that there certaintly are benefits to it, and that Unity will have covered all the corner cases. You could reinvent the wheel, but you probably don't want to reinvent the entire car.

    As for it being a nuclear option, it can be very lightweight. Just call EventSystem.SetSelected() or some such whenever your cursor hovers over a Button. At the very least I would recommend you give it a look before deciding it's not for you. =) Good luck!
     
  5. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
    What you pick to do has a lot to do with where you are in a project. I am trying to get a project that is shipping to port to more platforms. In that stage, brute force is always better than learning something new. The nuances of new things just takes too long to master.

    I will get back to your suggestion when I have more time.
     
  6. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
    I duplicated all the utility I am using in about an hour. I am sure there is a lot more that I am missing but don't need right now.

    The only major thing I am missing is the OnClick() piece which is great. I wish I had that.
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Hmm, interesting @Senshi
    Possibly another request for the #UIExtensions project, hmm :D
     
  8. NeilW

    NeilW

    Joined:
    May 29, 2014
    Posts:
    40
    I'm not sure if I am missing what you are after here but it seems like pretty much everything you require to put together a button based ui using a joystick should be there already (personally I would go for an approach that didnt use a virtual mouse pointer as I dont think these work well, instead up, down, left, right should just select the appropriate ui item):
    • Buttons already have various states that you can assign colours to - one of these is 'highlighted' i.e. the currectly selected one - if i make a scene with two buttons and hightlight set to yellow i can select one by using the cursor keys on my keyboard and press them using space
    • I think by default the Input manager is set up to support joystick input as an alternative to keyboard - see here http://docs.unity3d.com/Manual/ConventionalGameInput.html
    So I think there might be a chance this 'just works' out of the box if the set up is right (some way of visualising the highlighted button)
     
  9. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    @GameMechanics I think it depends entirely on the situation. I have often found that when I did something like this (recreating existing functionality) it often comes back to bite me later-on with unexpected side-effects or corner cases. Taking a good half hour to really learn the existing system then often yields a better RIO. That said, I hope things go well for you and your project!

    Was that a not-so-subtle hint? =P Perhaps I'll give it a spin over the weekend. ;)
     
    SimonDarksideJ likes this.
  10. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
    I was expecting their to be a Button method that changed the highlight state.
     
  11. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
    I actually did it your way because I have too many damn buttons to replace them all. It worked like a charm. The one thing I could not figure out was having a key press linked to a button press. I am adding that separately.

    Thanks!!
     
    Senshi likes this.
  12. GameMechanics

    GameMechanics

    Joined:
    Jul 15, 2013
    Posts:
    57
  13. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    That "fix" does two things which seem bad:
    • It's overriding the base colour transition effect on the button, which could lead to unexpected results
    • EVENTTRIGGER!, avoid using this at all costs. better to write a script which uses the event handers such as IPointerDownHandler. (note you can't have two events on the same object (such as click on a button))
    But as ever, if the solution works for you, then it's your implementation :D
     
  14. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    I'm guessing this "your way" was referring to using a custom Input Module? If so, why not just add your logic in there? Of course as Simon says (heh) if it ain't broke... ;) But if it does start showing weird behaviour I would just migrate all UI control code into the Input Module.

    If it's a [1-10] kind of thing you could even use a single "HotKeyParent" GO to automatically bind each subsequent child to a number. If it's something less coherent, like [A, W, G, X, L] or something, you could just expose fields for each called hotkeyButtonA, etc.

    Just some thoughts, cheers!