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

Question regarding multiple UI menus.

Discussion in 'General Discussion' started by Keldd92, Dec 1, 2020.

  1. Keldd92

    Keldd92

    Joined:
    Aug 26, 2020
    Posts:
    2
    Hello all,

    I am relatively new to the Unity Canvas system. In the current game we are creating, we have multiple menus for player upgrades, inventory, and quest hub. I was wondering if there is an easier way to construct the menus with the canvas system to keep them prefabbed and connected rather than building a giant script for functionality and having to reconnect it every time it is moved to another scene or manipulated. Can I add the functionality to the canvas itself if its SetActive state keeps being manipulated? Ill post the top of my script to show all of the connection that the town currently has. Should this be broken down further into smaller scripts or should be fine as is?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4.  
    5. /// <summary>
    6. /// This script is for the functionality of the port. It handles the switching between screens and the functionality of buying
    7. /// an upgrade.
    8. /// </summary>
    9. public class PortFunctionality : MonoBehaviour
    10.  
    11. [Header("Script Links")]
    12.     public PauseMenu pause;
    13.     public StatsHandler stats;
    14.     public ShipMovement shipMovement;
    15.     public PlayerHealth health;
    16.  
    17.     [Header("Header Text Object")]                      //Header text of the upgrade screen. Can be manipulated to change per screen.
    18.     public Text headerText;
    19.  
    20.     [Header("Window Objects")]                          //Port Windows. Main Port/Upgrade Window/Quest Window. All screens are seperate.
    21.     public GameObject portWindow;
    22.     public GameObject upgradeWindow;
    23.     public GameObject questWindow;
    24.  
    25.     [Header("Upgrade Windows")]                         //Upgrade Window with different tabs per skill.
    26.     public GameObject introWindow;
    27.     public GameObject dmgUpgradeWindow;
    28.     public GameObject armorUpgradeWindow;
    29.     public GameObject speedUpgradeWindow;
    30.  
    31.     [Header("Damage Upgrades")]                         //Upgrade slot for each upgrade.
    32.     public GameObject damageOneBlock;
    33.     public GameObject damageTwoBlock;
    34.     public GameObject damageThreeBlock;
    35.  
    36.     [Header("Armor Upgrades")]                          //Upgrade slot for each upgrade.
    37.     public GameObject armorOneBlock;
    38.     public GameObject armorTwoBlock;
    39.     public GameObject armorThreeBlock;
    40.  
    41.     [Header("Speed Upgrades")]                          //Upgrade slot for each upgrade.
    42.     public GameObject speedOneBlock;
    43.     public GameObject speedTwoBlock;
    44.     public GameObject speedThreeBlock;
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,323
    For example.

    Let's say you have an UI on-screen health gauge for the player and its state is managed by component attached to the gauge itself.

    You can update its state without directly linking it to the player.
    To do that, introduce a global function which returns currently active player. "Player.getActivePlayer()", and have the gauge call THAT instead of drag and dropping connection.

    Let's say you have a more complex UI screen that can be attahed to the player, or can be attached to another creature, there's ton of components in it, and you don't want to reconnect each of them by hand.

    To do that. Add some sort of "DataSource" component at the root of the canvas. Within DataSource add a method called "getTarget()", which will return what we're pulling the data from.
    For every component that is a child or a grand child (or a grandgrandgrand.... child), that has to display some information, grab DataSource instance within OnEnable(), using GetComponentInParent, and within Update use the DataSource you got to get the data you need.

    That reduces number of manual connections you need to specify to zero or one, regardless of how many buttons you have.

    Basically, it is better to program components in such way that they do not need manual linking and find linked components by themselves.

    In your example, you can remove ALL those variables or majority of them, and have them update themselves instead of trying to update them from within one component.
     
  3. Keldd92

    Keldd92

    Joined:
    Aug 26, 2020
    Posts:
    2
    Thanks for the response! I will try to give that a go today. That's the direction I thought I had to go, but was unsure if that was the right process or not to pursue. Thanks again!