Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Problem with canvas & UI prefabs

Discussion in 'Scripting' started by LambdaTheDev, Jan 14, 2021.

  1. LambdaTheDev

    LambdaTheDev

    Joined:
    Jan 15, 2020
    Posts:
    53
    Hello guys,
    I started making some UI prefabs to make my work easier. But something wrong happened when using the prefab. It's how it looks like on the canvas I made the prefab (it fits the canvas):

    But when I applied it on the new scene, well...

    ... prefab extends the canvas.

    Does anyone know what can I do?

    EDIT 1
    Both canvases are the same size
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,643
    There are underlying reasons this happened and you can track it down by examining everything about each scene's canvas setup and scaling that are different from the other.

    But keep in mind it might not be super-easily to address it in the general case, especially if you start partitioning your UI into chunks.

    An alternate workflow is to put chunks of your UI into separate scenes and then load those scenes additively.

    For instance you might have the following scenes, all loaded additively:

    - content scene (the level, including lighting)
    - player (including camera)
    - the generic UI (score, etc.) (including EventSystem)
    - the weapon UI (ammo, readout)
    - the pause UI (turned off until you press ESC)
    - the Inventory UI
    - etc.

    By keeping those all as separate scenes, you can get away from lots of code to load prefab chunks here and there.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,824
    If you want your prefab to always match the parent's size and shape, it's probably better to set its anchors to stretch.

    My best guess at what happened is that the resolution being used for your Game window in the editor changed, and the Canvas automatically changed to match it. (The exact manner of change will vary depending on the settings of your Canvas Scaler, if any.)