Is it better to create different canvases or to create different panels (game over, start…) in order for the game to adapt to every kind of cell phones ?
I prefer to have a single canvas for the entire game. This has the advantage of keeping the same size settings for everything that you attach to it. The disadvantage is that it’s slower to have many elements in a single canvas, as anytime you add/remove/move an element it will rebuild the whole thing.
Thanks for your quick reply, much appreciated !
One more question though, does it affect the game’s speed ?
I use several, if I feel the need to. In my current game, I have one on one scene and two on another; it feels cleaner to have separate UIs, I can find what I’m looking for more easily. I don’t think having several can be a problem either.
I have not had any issues with speed so far, and I build for iOS/Android.
But I can think of a few cases where I would rather split the canvas up. For example in an RTS game in you’re showing the health bars of every unit on the canvas it would be good to put that in its own canvas as it has a lot of change and movement.
We use several, but it’s based on what makes sense. The basic ui is on it’s own canvas which includes background image, navigation buttons, that sort of thing. Then if you go into a dialog, we have a canvas for that section. So, let’s say you want to go to the shop, that might be on it’s on canvas with any gui dealing with the shop there. Then another one might be the help canvas, or the login canvas.
We just do logical splits. Every app is different and one or many may depend on a lot of factors (some already listed).
One of the advantages of the UI system performance-wise is that it combines all of the sprites in the UI which are on the same sprite sheet into a single mesh with a single draw call. This is done at the Canvas level, which means that unifying vs dividing per canvas approaches both have advantages and disadvantages in terms of performance. For 99% of games, the difference in performance is virtually nothing, and you shouldn’t use these performance differences to decide which to use - do what makes more logical and organizational sense to you instead.
If you do have a big complex UI where performance will be critical, then you need to be aware of the fact that each frame, a Canvas looks through all of its components, sees if any have changed, and if so, rebuilds its mesh - the entire mesh. So if you have 500 UI objects, and one of them changes each frame, then that one that changes may be better off on its own Canvas, so that the other 499 can just have the same mesh from frame to frame.
On the other hand, every Canvas will have at least one draw call, which adds a smidgeon of overhead. So having 500 different Canvas objects (in addition to being annoying to organize) means that you have 500x as much overhead as needed. Of course, if a given set of UI elements is on its own sprite sheet, it’s going to have its own draw call either way, so there’s no performance cost to separating each sprite sheet onto its own Canvas if you’ve got more than one sheet.