Jump to content
  • Advertisement
  • entries
    3
  • comments
    0
  • views
    562

Managing UI between scenes in Unity

Whistling Alpaca Games

1066 views

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).

image.png.8294510e0cbecde8fd6cf1ac28dfe366.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:

image.png.d442990e024c65029449488ce90ce0ab.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.




0 Comments


Recommended Comments

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
  • Advertisement
  • Blog Entries

  • Similar Content

    • By ethancodes
      I'm fairly new to creating full 2d levels, so please feel free to correct me if I use incorrect terminology or if I'm doing something wrong here. Currently, I am using the Tile Palette with several sprites placed on it to create my levels. With that I can just run my mouse across the screen and quickly place tiles. Then on the TIlemap gameobject I place a Tilemap Collider and a Composite Collider. This works great, it's quick, simple, and efficient. However, for the next part of my character controller I'm looking at adding the ability to do a ledge grab. After doing some research, it seems like a common way to do that is to make the ledge he can grab "grabable" by putting a different collider on them. So my question is, is there an efficient way to where I can let the Tile Palette know if I use a specific corner sprite, place the ledge grab collider on it? Is there any way other than going through all the ledges in the game and manually placing them there? I'm also open to any tips on level building and using the Tilemap, etc that may help me out as, like I said, I'm pretty new to it all. Thanks!
    • By syedMohib44
      Hello guys I am a bit confused on rotation and position in unity. The thing is when I am at 0 rotation on X-Axis and I restricted my player to not let it go more than 10 on x-axis and less than -10 x-axis but when I rotated it to 90 degree It stated to move forward on left key press and backward to right key press. I just want to clamp position between 10 and -10 on every rotation It must not move differently when rotated... e.g for some understanding the scenario If a player is on a road and it is restricted to go on foot path then it should also be restricted when rotated to left or right... Please help me with this guys any help would be appreciated. 
      Thank You.
    • By Doctrina Kharkov
      We released an arcade story-driven game created by 12-13 years old children.
      All coding, art and animation was madden by them.
      Game have 5 unique mechanics levels.
      https://play.google.com/store/apps/details?id=com.Doctrina.Skonny
       
    • By VoroNoise
      Main designer here with a massive scale RTS/grand strategy/city builder/god game. If this sounds interesting and you're an artist or programmer with ideas then please contact me! We can make a bundle! I already have musicians with solid tracks. About three quarters of the way through total development, just need minor details filled out and a lot of art assets worked in.

  • Advertisement
×

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!