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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I make the camera the same size and position as the Canvas?

Discussion in '2D' started by KazFoxsen, Apr 16, 2020.

  1. KazFoxsen

    KazFoxsen

    Joined:
    Apr 6, 2020
    Posts:
    6
    I'm making a 2D character creation game and I read that it's best to set the Canvas to Screen Space - Overlay, which made the canvas huge (1600 x 900) compared to my orthographic camera. I resized my character sprites to fit the Canvas better (the sprites are children of the Canvas), but the sprites show up in the wrong place in Game View. I tried changing the size and position of the Camera, trying to match it to the Canvas so I when I edit the Scene and check out Game View, everything will be proportioned the same, but I can't get the Camera to be the same size and position as the Canvas.

    How do I fix this?
     
  2. TitoOliveira

    TitoOliveira

    Joined:
    Aug 4, 2014
    Posts:
    77
    Need more info on how you're setting up this game.

    Why are you sprites children of the Canvas?
    Regular sprites inhabit the world of your game, while the Screen Space Canvas is an overlay that exists on top of the image rendered by the camera. You will never be able to comfortably align these two things together.

    I believe that part of your confusion is because the Canvas show up in the scene view as a giant object in the middle of everything else. That sure is annoying, but trying to align the Canvas object with your other game's objects won't work. Even though the Canvas look like somethign in the middle of the scene, it will be rendered as an overlay on your camera, and its size will scale according to your camera's resolution and whatnot.

    Are you making the whole game as a UI screen? If that's the case you should be using the UI > Image object as children of your canvas for your sprites. These objects will have a RectTransform that scale along with your Canvas.
     
  3. KazFoxsen

    KazFoxsen

    Joined:
    Apr 6, 2020
    Posts:
    6
    Yeah, the whole game is UI since it's a doll maker. In order to swap out and color character parts, I followed a tutorial that utilized sprites, but if I can recreate the same scripts and use Images, then that's fine.
     
  4. Trevdevs

    Trevdevs

    Joined:
    Jun 3, 2015
    Posts:
    15
    Agreeing with Tito you should use UnityEngine.UI Components if you are working inside a canvas such as Image since rather than their transform being of type Transform they are type RectTransform since they are built for GUI's with anchoring. Anchoring allows you to say "hey no matter the size of my screen keep this object 150 pixels to left of the edge of the screen" with a normal Sprite object you can do that but you would need a script and do some fancy positioning math.
     
  5. KazFoxsen

    KazFoxsen

    Joined:
    Apr 6, 2020
    Posts:
    6
    How do I swap Images via script? All the instructions I've seen require a SpriteRenderer. Can an Image have a SpriteRenderer on it and still work as UI?

    Is an SVG Image instead of a regular image ok for this purpose? I want to import SVGs.
     
    Last edited: Apr 18, 2020
  6. TitoOliveira

    TitoOliveira

    Joined:
    Aug 4, 2014
    Posts:
    77
    Instead of a Sprite Renderer, you will be working with an Image Component. Similar to the Sprite Renderer, the Image also has a Sprite field, so you can swap it via script in the same way.

    Regarding SVG support, i know Unity added that a little while ago, but i never worked with it. So i can't help in that regard.