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. Dismiss Notice

UIs beyond my title won't show up.

Discussion in 'UGUI & TextMesh Pro' started by AndrewGrayGames, Feb 20, 2015.

  1. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,822
    I've recently started using Unity UI in the title screen of my current project, Sara the Shieldmage, in order to learn how it works. I'm starting small, just linking up three screens - a title, a 'load game' presenter that consists of a back button, and a 'settings' screen that also consists of a back button.

    I did notice the in-editor GUI for wiring up events, but I found myself having to do a lot to disable and hide stuff, so instead I wrote this base script that I put on the gameobject for each screen*:

    Code (csharp):
    1. public class UGUIPresenterBase : DebuggableBehavior
    2. {
    3.     #region Variables / Properties
    4.  
    5.     public float FadeRate = 0.5f;
    6.  
    7.     protected Maestro _maestro;
    8.  
    9.     protected List<GameObject> _children = new List<GameObject>();
    10.     protected List<Button> _buttons = new List<Button>();
    11.     protected List<Image> _images = new List<Image>();
    12.     protected List<Text> _labels = new List<Text>();
    13.  
    14.     #endregion Variables / Properties
    15.  
    16.     #region Hooks
    17.  
    18.     public virtual void Start()
    19.     {
    20.         _maestro = Maestro.Instance;
    21.  
    22.         int childCount = transform.childCount;
    23.         for (int i = 0; i < childCount; i++)
    24.         {
    25.             Transform current = transform.GetChild(i);
    26.             Button button = current.GetComponent<Button>();
    27.             Image image = current.GetComponent<Image>();
    28.  
    29.             _children.Add(current.gameObject);
    30.  
    31.             if (button != null)
    32.                 _buttons.Add(button);
    33.  
    34.             if (image != null)
    35.                 _images.Add(image);
    36.         }
    37.  
    38.         DebugMessage("Presenter " + gameObject.name
    39.                      + " has " + _buttons.Count + " buttons, " + _images.Count + " images,"
    40.                      + " and a total of " + _children.Count + " child objects.");
    41.     }
    42.  
    43.     public virtual void PresentGUI(bool isActive)
    44.     {
    45.         DebugMessage((isActive ? "Presenting " : "Hiding ") + gameObject.name);
    46.  
    47.         ActivateButtons(isActive);
    48.         ActivateImages(isActive);
    49.     }
    50.  
    51.     #endregion Hooks
    52.  
    53.     #region Methods
    54.  
    55.     protected void ActivateImages(bool isActive)
    56.     {
    57.         for (int i = 0; i < _images.Count; i++)
    58.         {
    59.             Image current = _images[i];
    60.             DebugMessage("Fading " + current.gameObject.name + " " + (isActive ? "in" : "out"));
    61.  
    62.             current.CrossFadeAlpha(DetermineOpacity(isActive), FadeRate, false);
    63.         }
    64.     }
    65.  
    66.     protected void ActivateButtons(bool isActive)
    67.     {
    68.         for (int i = 0; i < _buttons.Count; i++)
    69.         {
    70.             Button current = _buttons[i];
    71.             DebugMessage("Turning button " + current.gameObject.name + " " + (isActive ? "on" : "off"));
    72.          
    73.             current.interactable = isActive;
    74.             current.enabled = isActive;
    75.  
    76.             // Hide child text, too!
    77.             Text childText = current.GetComponentInChildren<Text>();
    78.             childText.CrossFadeAlpha(DetermineOpacity(isActive), FadeRate, false);
    79.         }
    80.     }
    81.  
    82.     protected float DetermineOpacity(bool isActive)
    83.     {
    84.         return isActive ? 1.0f : 0.0f;
    85.     }
    86.  
    87.     #endregion Methods
    88. }
    Each screen has a script that inherits from this, for instance:

    Code (csharp):
    1. public class ContinuePresenter : UGUIPresenterBase
    2. {
    3.     // Special, presenter-specific logic goes here when it becomes pertinent.
    4. }
    And in turn, I have a TitleManager** set up to coordinate these:

    Code (csharp):
    1. public class TitleController : ManagerBase<TitleController>
    2. {
    3.     #region Variables / Properties
    4.  
    5.     private TitlePresenter _title;
    6.     private ContinuePresenter _continue;
    7.     private SettingsPresenter _settings;
    8.  
    9.     #endregion Variables / Properties
    10.  
    11.     #region Hooks
    12.  
    13.     public void Start()
    14.     {
    15.         _title = GetComponentInChildren<TitlePresenter>();
    16.         _continue = GetComponentInChildren<ContinuePresenter>();
    17.         _settings = GetComponentInChildren<SettingsPresenter>();
    18.  
    19.         PresentTitleScreen();
    20.     }
    21.  
    22.     public void PresentTitleScreen()
    23.     {
    24.         _title.PresentGUI(true);
    25.  
    26.         _continue.PresentGUI(false);
    27.         _settings.PresentGUI(false);
    28.     }
    29.  
    30.     public void PresentContinueScreen()
    31.     {
    32.         _continue.PresentGUI(true);
    33.  
    34.         _title.PresentGUI(false);
    35.         _settings.PresentGUI(false);
    36.     }
    37.  
    38.     public void PresentSettingsScreen()
    39.     {
    40.         _settings.PresentGUI(true);
    41.  
    42.         _title.PresentGUI(false);
    43.         _continue.PresentGUI(false);
    44.     }
    45.  
    46.     #endregion Hooks
    47.  
    48.     #region Methods
    49.  
    50.     #endregion Methods
    51. }
    One small problem, though - when I run my title scene, everything on the title presents correctly. When I click my continue or settings buttons - which, are wired to call the correct TitleController method on the canvas, the title GUI elements all hide as they should, but the continue or settings elements do not appear. I've vetted my code a few times, and I'm not seeing the problem (especially given I haven't yet added any special code.) Where am I going wrong?

    Note: you can download my current codebase here, so you can see for yourself my editor bindings and stuff.

    *: DebuggableBehavior adds the DebugMessage() method you see, and some switches.
    **: ManagerBase<T> implements a static Instance property, similar to a Singleton.
     
  2. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    Could you try adding a canvasgroup to the gameobjects that hold each screen, then fade the alpha on that instead?
     
    AndrewGrayGames likes this.
  3. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,822
    Newbie question - how do I do that, exactly? I right-clicked one of my screen game objects, and looked under the UI category, but didn't see a Canvas Group component.
     
  4. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    Look under the Add Component button in the Inspector. You will find it under Layout or you can search in the search bar there. I assumed those menus were the same but i guess not.
     
    AndrewGrayGames likes this.
  5. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,822
    It was under Layout. That's weird that it wasn't with the UI stuff. Eh well, it got my Title Screen stuff working, and allowed me to cut out a little bit of code, so that helps! Thanks bro.
     
    Feaver1968 likes this.
  6. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    I've done some editor scripting and adding to the right click menu is kind of a pain, so I can understand that they wouldn't bother implementing all of the potential scripts in it. Though it kind of sucks. Anyway, glad it worked. :)
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Don't forget, with the add component menu you can also search! so typing the name fo the control finds it.
    UI maintains two branches in the menu, UI and Layout. UI for controls, Layout for (you guessed it) Layout controls.