Canvas too big for the camera in Unity - unity3d

I have created a 2D game with an orthogonal camera and using 16:9 display size.
I dragged my background image onto the hierarchy (it's about 2048x1152) and then set the camera size to be 22.5, which made it fit the background perfectly and displays just right.
However, when I add a Canvas for a UI it is absolutely giant, about 100 times bigger. It only becomes 'normal' size with respect everything else added when I set the camera to its default size of 5. So when I add a small graphic, it too becomes giant.
I'm simply following a book I read and I'm not doing anything to deviate.
Am I doing something wrong? Below is what I mean. The background image is the little image in the bottom right and the outlined rectangle is the canvas with a small graphic added.
Thanks.

To force your Hierarchy Canvas UI to the same resolution as the Camera View in your Unity Editor Scene window resolution (i.e. not ridiculously massive), or in other words get the Canvas to fit into the Camera size in the Scene, do the following:
Set the Canvas component's Render Mode to Screen Space - Camera.
Make sure you select or drag the relevant Camera from the Hierarchy to the Render Camera field in the Inspector.

You should use the Unity canvas for this along with the canvas scaler component. If I'm not mistaken it will scale all elements relative to the screen they are viewed on.
The canvas scaler allows you to match the scaling based on a preferred viewport size which is a life saver.
However this may not fit you needs perfectly as it would mean that the background element would become fixed. So if you wanted to pan the element you would need to move it's x and y elements within the canvas.
Hope that helps?

Related

Unity gameobjects and text ui change size and position while changing resolution

sorry I have to put all of this as an image but I couldn't post this with text as when creating the original post something happened to where it was flagged as spam. I figured this would be the only way?
Unity's UI screen is different from the in-game world space.
For the UI part you can easily fix the issue of the size and position by considering the following:
First thing you need to check is the Canvas Scaler component which is automaticaly generated on your Canvas gameobject.
Canvas Scaler Example
First under The UI Scale Mode you should choose the "Scale with Screen Size" which will make UI elements bigger the bigger the screen is.
Second is the Reference Resolution which is the resolution the UI layout is designed for. If the screen resolution is larger, the UI will be scaled up, and if it's smaller, the UI will be scaled down. A good example is to set the dimensions int something like this (1920x1080) which is the most common landscape resolution.
Third is the Screen Match Mode which is a mode used to scale the canvas area if the aspect ratio of the current resolution doesn't fit the reference resolution. With other words if you change your in-game resolution this will handle your UI elements.
Fourth is Match which will determine if the scaling is using the width or height as reference, or a mix in between. So if your game is in landscape mode you should set it to 0, if it's in portrait mode you should set it to 1.
Last thing to keep in mind that will affect your UI elements position is the Anchor presets which is part of the Rect Transform component on each of your UI elements.

Physical Camera Viewport should adjust with the UI for different resolution

I have a physical camera in the scene which renders a 3D Scene. The 3D Scene is rendered between the Top UI Bar and the Bottom UI Bar and has been set properly for reference resolution 720 x 1280.
So, the problem is when the resolution changes, the UI is set properly for that particular resolution. But the 3D scene that is rendered between the 2 UI parts, doesn't sit properly between them. I am attaching 2 reference images for easier understanding.
The below-given image is based on the reference resolution and the 3D Scene is properly fit between the 2 UI parts.
The below-given image is for another resolution where the UI adjusts itself accordingly. But the 3D scene doesn't, i.e the camera should move to fit the 3D Scene between the UI.
So, is there any way I can move the camera according to the resolution so that it fits properly between the UI for different aspect ratios.
Thank you.
First of all, try looking at the ViewportRect property of the camera - it enables you to crop the viewport to an area which should enable the effect you seek.
This however has the limitation of keeping the 'center scren' axis of perspective, which in some cases is uncalled for.
An 'should always work' solution is rendering to a RednerTexture - camera adapts its screen aspect to the aspect of the RenderTexture, and if you display your texture on RawImage, you should get dececnt results. This however has some performance cost (memory readout is not the fastest on mobile).
In case the cost woul be unnaceptable, its possible to construct custom camera projection matrix, that would respect your desired viewport rect, and also allow arbitrary perspective, but its not trivial unfrotunately. Here is some more information
https://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html

Adjusting the 3D game to the screen size

How to make the 3D game adapt to the screen resolution?
I tried to change the fieldOfView of the camera, but this adjustment does not work correctly!
If you mean UI elements, there are little triangles usually in the middle of the canvas the element is under. These are anchors that will tell the element to try and stay in the same place on the canvas regardless of the screen resolution. You can read more about it here: https://docs.unity3d.com/Manual/UIBasicLayout.html https://docs.unity3d.com/Manual/HOWTO-UIMultiResolution.html
If you mean your actual game view, you'd probably need to write a script that adjusts the camera's FOV at the start of the game based on the resolution, but I have no idea where to even begin on the formula you'd use.

I can not scale my Canvas in unity

I can't scale my Canvas in unity. Where i normally do it, it like this:
I can't scale it or any thing.
As indicated at the top of the RectTransform, values are driven by the Canvas component (or the CanvasScaler component)
According to the Render Mode you have selected, you are able (or not) to change the values. You may want to scale the children of the canvas instead of the canvas itself. If you have problems with designing your UI for multiple resolutions, I advise you to read the documentation page about this problem.
Screen Space - Overlay
The canvas is drawn after all the other cameras. Its dimensions change according to your screen. The scale does not need to be changed.
ScreenSpace - Camera
The canvas is rendered by the given camera, at a given distance from the latter. Its dimensions change according to the camera's rendering settings.
World space
The canvas is a "real" object inside your scene, treated like any other objects (culling apply, ...). In this mode, you can scale the canvas.
Source : https://docs.unity3d.com/Manual/UICanvas.html

Unity Canvas does not fill screen

I have a 2D game set to 1080p pixel size (so each unit equals 1 pixel) and I have added a canvas to my scene. However the canvas fills only about 1/8th of the screen and is also not central either. All the canvas size and position settings are locked so I cant move it or resize it. The canvas itself does reflect the screen (ie if I put text in the top right corner it appears correctly) but the size makes it hard to judge where best to position the elements).
How do I get the canvas to fill my scene?
On your canvas, set the Canvas Scaler component's Ui Scale Mode to Scale with Screen Size. Then you can define a Reference Resolution of 1080p, i.e. 1920 x 1080.
EDIT: To see the canvas fit into the camera's size in the scene, change the Canvas component's Render Mode to Screen Space - Camera, and drag the camera from the hierarchy to it.
(check this video)