Hello guys
I’ve standard UI in my game that lays into Canvas with Screen Space render mode. In addition I’d like to make some diegetic interface and I need for them other Canvas with World Space render mode. How to resolve this? Any attempts to make a prefab for diegetic interface ends with inheritance from the main Canvas with Screen Space render mode… Is there any solution for that?
thanks in advance
There is nothing to resolve. You can have as many canvases (in as many different render modes) as you like.
I’m afraid I have no idea what you’re talking about. I use multiple canvases in my projects all the time.
I’ve two canvases on the root in the scene. One for Screen Space and the second for World Space. When I create a UI prefab it’s automatically have Screen Space canvas as a parent in his root, I can’t change that (it’s grey in the hierarchy). I assume that the prefab will display incorrectly cos have wrong Canvas parent. Maybe I should change his parent after Instatiate to World Space Canvas?
Could you show us the code that instantiates the UI element where it’s defaulting to the screen space canvas? I’m thinking you just need to use one of the overloads that explicitly sets the parent transform, and pass in your world space canvas there.
In fact it’s recommended that you have multiple canvases because if you make a change in any way to a UI element in a canvas it will trigger a redraw for every UI element belonging to that canvas.
It sounds to me like you’re trying to create a prefab of some UI elements within the canvas. That can work, but it’s going to be a little confusing, as you discovered. This is because UI elements have to live within a canvas; if you edit a prefab of isolated UI elements, then Unity is going to just make up a canvas for them to be in while editing. That doesn’t cause any real problems, but it is confusing. And when you instantiate that prefab, it’s up to you to instantiate them within the Canvas that you intend it to be in.
So usually, especially when dealing with world-space canvases, it makes more sense to create a prefab of the entire canvas, rather than some UI stuff within it. The only exception would be if you have a complex dynamic UI where you need to add a bunch of different UI sub-hierarchies at runtime (and can’t just show and hide parts of a predefined hierarchy).
Thanks you guys. I have made in my prefab additional Canvas as a parent and set render to World space and everything is okay.