Jump to content
  • Advertisement
  • 09/03/19 12:58 PM

    Managing UI between scenes in Unity

    General and Gameplay Programming

    Whistling Alpaca Games

    Coming up with a solution for managing UI elements across different scenes wasn't as trivial as I first expected. It's very easy to attach a canvas to a scene and call it a day. Initially, I had all of my canvases attached to the first scene I expected the player to enter and then simply call DontDestroy(gameObject) on all of the canvas scripts.

    So what's the issue? If you allow the user to save and load from other areas, you have to copy/paste a prefab into each of your other scene. This is a trivially easy solution, but it's too dirty for my liking, instead, we should dynamically create and manage the UI elements via a UI manager.

    The first step would be to create an empty object in our loading screen (the loading screen will always be accessed before gameplay starts).

    1.png

     

    In this case, since we're also going to be appending a bunch of canvases to this empty object, it needs a canvas or stuff just doesn't render properly. Also, make sure to add a reference to your UI element prefabs:

    2.png

     

    And the start script for the UIManager script:

        void Start()
        {
            if (allCanvases == null)
            {
                DontDestroyOnLoad(gameObject);
                foreach (GameObject prefab in prefabsToInst)
                {
                    GameObject toAdd = Instantiate(prefab);
                    toAdd.name = prefab.name;
                    toAdd.transform.SetParent(transform);
                    uiCanvases.Add(toAdd);
                }
            }
            else
            {
                Destroy(gameObject);
            }
        }

     

    Each canvas appends itself to the UIManager and therefore does not get destroyed on scene change. All specific behavior for each individual canvas can be handled in the individual UI component's scripts. Blamo! That's it! Crisis averted, we can now navigate between scenes and know that our UI manager is now empowered to show canvases, hide them, etc.

     

    Note: This article was originally published as a blog entry here on GameDev.net by Whistling Alpaca Games.



      Report Article


    User Feedback


    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement
  • Game Developer Survey

    completed-task.png

    We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a $15 incentive for your time and insights. Click here to start!

    Take me to the survey!

  • Advertisement
  • Latest Featured Articles

  • Featured Blogs

  • Advertisement
  • Popular Now

  • Similar Content

    • By RoKabium Games
      For #screenshotsaturday, the song of Fire and Ice can be experienced in SAMA if you bring your Pyroblaster into the cold depths of Aura.
    • By thomedy
      Okay i am building a super market game...
       
      and im developing the ai for my npc...
       
      i am thinking about the way that customers walk around a super market and it kinda has a fluid motion feel to it.... like they way they way in between aisles..
      im very seriously considering writing a vector field for my  my path where the customer (npc) can  sense pivot points (end of aisle) and transition
       
       
      another thought i had was just an intuitive.. if you went right last time... go left this time walking the entirety of the supermarket and starting over
       
      i amg oing back and forth on this idea... what is the most realistic path and most efficient use of my memory
       
    • By HDR Games
      Yesterday I've published my new game to google play!
      I'm 16 years old and I will be very happy if you check my game and tell me what you think about it.
      It takes a minute to download and you won't regret it. Please let me know what you think in the comments.
      Best android game of the year! https://play.google.com/store/apps/details?id=com.HDRGames.FishTheFun
      Swim into the depths of the sea! Jump the highest and reach the sky. Beware of enemy fish, birds and obstacles. 
      1. Discover fish as you play!
      2. Buy fish to be able to play with them!
      3. Improve your best score to beat your friends'!
      4. Complete 100 quests to win rewards!
      5. Enter every day to receive daily rewards!
      6. Enjoy the sea, and Fish The Fun.
      Link to HDRGames website: http://hdrgames.sytes.net
      Link to HDRGames Instagram: https://www.instagram.com/hdr.games
      Watch the trailer: 
       



    • By COUNTING DEMON STUDIOS
      Devlog #4 showing the most recent updates to the game I've been working on in my spare time for about 2 years now.

      Any comments or suggestions are much appreciated!
       
    • By asmit10
      Read this first to not waste time
      Hey everyone! I'm basically brand new to game dev, specifically programming which is what I care to specialize in. Currently i'm working on a 3D Tile-based tower defense game in Unity. The plan is to plan for primary monetization on mobile, but it will come to PC too. I probably lack the most skills in the art department, primarily graphics work, but it's not like my modeling is great either. That being said, my main focus is programming so if you read further below and find yourself interested in the project and have a different interest, please let me know!
       
      Game Info
      TD Game ala Bloons TD
      Differences
      Towers are based on tiles pre-placed on a grid (plan to add ability to buy tiles, so that tile scarcity can be more of a part of level design)
      No Russian dolling of enemies, different enemies following a difficulty gradient
       flying enemies that normally follow a different path
      Misc
      Branching, occasionally choice making upgrade branches for each distinctly unique tower. (thinking something like 15 unique upgrades per turret, though that would be tough to come up with)
      Initially planning for 10 distinct level layouts not including an endless survival 'leaderboard' mode
      Seems like it would be silly to not include easy / medium / hard difficulties, just need to think of a way to reward the player for doing so
      Planning to have some long-term progression system. Maybe related to # of kills with a tower. Every 500 kills = 1% fire rate or something like that.
      All current art and models are place holders, screenshots of the project can be provided if you show interest
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!