Dave
Dave

Reputation: 171

Same GUI multiple scenes in Unity game - prefab of Canvas?

My problem is how to share same GUI in multiple scenes.

For example:

I have Inventory GUI on Farm scene and I need this GUI on other scenes like Castle, Dungeon etc.

It's good idea to make prefab of Canvas? Or design again the GUI? What about mixing "shared GUI" and individual GUI like Dialogs etc.?

Thanks for any answer.

Upvotes: 4

Views: 3182

Answers (2)

Adam Roszyk
Adam Roszyk

Reputation: 409

Besides GUI itself you will need some scripts to handle each element and property separately.

Take a look at singleton codes; it shows how to create persistent object, which stays across different scenes.

http://wiki.unity3d.com/index.php/Singleton

It's good practice to script GUI which needs to be visible on different levels with this design pattern.

Upvotes: 1

cjmarsh
cjmarsh

Reputation: 292

You can certainly make a prefab of a canvas for each screen you need although if you have to destroy it and respawn it a lot it could cause a performance hit. An alternative is to create a single canvas with all the screens as children, all disabled, and enable each as necessary. This has the advantage of not doing lots of computation at once but on the downside there's some overhead for keeping it all in memory. For a small game I would recommend the latter and if you need to scale it up you can mix the two: have canvases with more than one related screen, ideally screens grouped in such a way that they are only accessible in certain situations, letting you keep as few canvases in the scene as possible.

Upvotes: 1

Related Questions