Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Show/hide certain elements based on conditions

Discussion in 'Scripting' started by vee41, Apr 26, 2015.

  1. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Hi!

    I've been wondering what are the good practice would be in this case; I am sure there are many but even one would be very helpful:

    1) When I press an object in my game I want bunch of UI elements to appear around it.
    2) Depending on various variables some of the UI elements will be shown/hidden.

    What I have at the moment:

    World space canvas that is placed around the object when an object is clicked, the canvas has all the icons nicely arranged. This is controlled by UI Manager singleton.

    How I'd like to expand this functionality is to make buttons hide themselves under certain conditions. This would mean they need to:

    a) Trigger show/hide check on proper moment (when canvas has been moved).
    b) Way to tie each button to conditions that show and hide them. I'd love to wire these conditions through inspector.

    Solutions I thought and did not like:

    - Doing switchcase or if structure for each button that calls their check and shows them based on results. For each new button I'd need to do this wiring and it just seems a bit too messy. I am looking for the elegant way here where each button knows it's own hide condition! :)

    - Creating a inherited class for each button that would hold their display/hide conditions which would be called i.e. by UIManager singleton. Seems a bit overkill to create class for every single button.

    What I'd love to have:

    I was looking for a way for each button to have sort of public delegate function that would point to the function that returns bool status if they should be hidden/shown (got such functions ready). Couldn't get this to work, but basically it would be neat solution to hook each buttons "DisplayCheck" function to function which is the actual show/hide condition. If this could be done in inspector, even better! Looked into UnityEvents but couldn't manage this with those.

    Probably trying to over think this too much and have just missed some really simple and elegant approach on one of the aforementioned attempts :)
     
    rboerdijk likes this.
  2. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    An idea (perhaps naive), in Update() just enclose everything in an if statement that checks your conditions, else disable itself? I'm assuming your UI elements are predefined and in the scene, just disabled, and you are just looking to selectively enable them?

    If your UI manager is creating prefabs on the fly then you have to actually instantiate them anyway so perform checks there?

    I might not be understanding where you are coming from though =]

    Halbera.
     
  3. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Thanks for the reply! I thought something similar, the issue I have with that approach is I'd need to have a reference to every single button and their hide conditions someplace. It would be much cleaner solution to just have every button know their personal hide condition, and I'd like this to be generic link to function i.e. not hardcode every buttons conditions so every button requires some custom code.
     
    rboerdijk likes this.
  4. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Well if your buttons are pre-made prefabs then you'll only need to do it once for each prefab =]. If they are created on the fly then your UI manager could just not create them if they arent needed? I dunno what these ui elements are supposed to represent. Need more context!

    Halbera.
     
  5. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    That is true, but I am still looking for a bit more generic way to do this.

    Here is a bit more concrete example:

    Player is standing in crossroads and I'd like to display an UI icon for each possible route player can go. By default there might be 4 open routes, but sometimes one or more of them might be blocked and I'd like to hide icons for those. Like I said the answer is trivial, but I am looking for the generic, elegant solution that does not require any additional hardwiring after I've setup the system once and later decide that I want to have 16 possible routes instead of those 4.

    So this is what happens:

    1) Player arrives at crossroads and event to display UI icons is triggered.
    2) All UI icons are placed to proper locations.
    3) All UI icons determine whether they should be shown/hidden by calling an function in another class

    I'd like to avoid having individual code for each button which seems like overkill, instead I'd like each button to have few variables that could be used to define the conditions where button should be shown hidden (i.e. "Take the left turn here" button would have vector pointing to left, which would be sent as parameter to the function that determines whether this way can be traveled.

    I guess I could do something like this in UI Manager:

    // show buttons
    // foreach button
    // button.display()


    And the button:

    // public parameter1;
    // public parameter2;


    // void display()
    // call the function to determine visibility (parameters)


    Now this works to some extent, but I am still wondering if there is more 'elegant' way to do this by using delegates or UnityEvents which I am not really proficient with yet?
     
  6. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Im not sure. I feel like if your ui knows what roads are availiable then the ui should probably handle what gets displayed which takes care of your desire to not have code for each button.

    I dunno, complete novice here!

    Halbera.