Search Unity

Handling user input. (Question)

Discussion in 'Scripting' started by tegreon, Oct 21, 2020.

  1. tegreon

    tegreon

    Joined:
    Aug 13, 2020
    Posts:
    4
    Hello everyone,

    I was wondering what the best way to handle input. In my game there are a bunch of hex tiles, which have their own instanced classes with properties, such as coordinates, resources, buildings, etc... Whenever a player clicks on a tile I activate a UI element which grabs the data from the tile based on a ray cast and displays it.

    First question - is activating and deactivating UI GameObjects a legit way of handling UI? I'm pretty new at this so I don't know how its generally done.

    Second question - currently whenever the player left clicks at all the UI will activate, even if the player is for example placing a building. What is the best way to ensure the UI will turn on only in certain scenarios? I was thinking of creating a static class with bools, and only activating the UI if all of them are false, but that seems like a terrible solution, as every time I have a condition that prevents the UI from turning on id have to add a bool, then make sure it is set properly each time. For multiple UI elements that just seems like a nightmare.

    Thanks.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Yep this is pretty standard practice.

    It really depends on how your project is structured, but a fairly straightforward way is to deactivate or disable whichever component is responsible for the ui raycasting when appropriate.
     
    tegreon likes this.
  3. tegreon

    tegreon

    Joined:
    Aug 13, 2020
    Posts:
    4
    That gives me an idea. I think I will create a bunch of MonoBehaviours on a UI controller GameObject and enable/disable them as needed.

    Thanks!
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    your questions are valid, but there are no strict rules or guidelines on how to organize your inner logic, as long as it works.

    if you'd hate seeing a bunch of bools (and I do recommend gathering everything related into one "UI controller" place), I get you -- but you can do it however you want, with bitwise flags, dynamic conditions, system events, even dictionaries.

    just don't overdo it, it's just a UI. it's always a bit messy.

    for multiple UI components that act like radio buttons, you just want an int
    Code (csharp):
    1. // obviously you call this only when there is a demand for update
    2. for(int i = 0; i < uiPanelCount; i++) getUIPanel(i).SetActive(i == currentPanel);
    (of course this is just a boilerplate, you need to supply the missing pieces yourself, and refer to the actual UI panels from some array or list)
     
    Last edited: Oct 22, 2020
    tegreon likes this.