Search Unity

Question Two different UI at the same time

Discussion in 'Getting Started' started by monsterrungame, Aug 9, 2021.

  1. monsterrungame

    monsterrungame

    Joined:
    Sep 27, 2019
    Posts:
    15
    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
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    Joe-Censored likes this.
  3. monsterrungame

    monsterrungame

    Joined:
    Sep 27, 2019
    Posts:
    15
    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?
     
    Last edited: Aug 10, 2021
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    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.
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,131
    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.
     
    Schneider21 and JoeStrout like this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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).
     
    Ryiah and Schneider21 like this.
  7. monsterrungame

    monsterrungame

    Joined:
    Sep 27, 2019
    Posts:
    15
    Thanks you guys. I have made in my prefab additional Canvas as a parent and set render to World space and everything is okay.
     
    JoeStrout likes this.