Setting X of camera's viewport causes the black strip to be rendered - unity3d

I’ve run out of ideas, please help me.
That's the situation:
Imagine a simple object in the center of screen (it's always so, because camera has an orbit script)
Then we add a canvas image, 300px width, for example, on the right side of the screen (blue one in the picture). Our cube is not centered anymore.
For example, we can change X property of camera viewport rect, but we'll get a black strip and if object appears in this zone, it would not be rendered.
So, what can I do to offset camera position?
Thank you in advance!

The best option I have for now is:
create basic UI for FullHD resolution and make right panel as wide as black strip is
if user's screen bigger than 1920px, than change Canvas Scaler mode from Constant pixel size to Scale with screen size

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.

How to keep the same resolution of canvas on different screen resolution?

First of all, sry for my English mistakes, I'm not a native English speaker.
I'm trying to make an UI which is composed of a canvas within different gameObject, and I would like that my canvas scales to the dimension of the screen but keeps its original resolution (16x9 portrait). If it is displayed on a tablet resolution (4x3) then an image is displayed in the space that is not covered by the canvas.
But actually all I've got is a canvas which scales to every resolution, and it changes the aspect of its child (for example a square becomes a rectangle).
Thank you for showing interest in my query!
UI's are heavy beasts. Canvas in Unity have a component attached to themm called Canvas Scaler which is set by default on Constant Pixel Size. You may try to set this property on Scale With Screen Size and then specify the base resolution you want to work with (usually 1920x1080 is a console standart). This is your first step
Then, to avoid strange Image scaling, you may check the property Preserve Aspect, this way the ration of the Sprite into your Image will remain the same indepently of the ratio of the Image
Last, you may play a bit with anchors but this is another story, you should let those at plain center at the beginning and come back to it when you will feel ready
Hope that helped ;)
Your canvas should have a component called Canvas Scaler. Here it should say Constant Pixel Size, change this to Scale With Screen Size and it should lock the Canvas to be the same width / height as the screen. If you want to lock an image to a specific width/height ratio, go to the Image component on the image and check the Preserve Aspect checkbox. This way if you have a 100x100 image, the images width will always be the same as the height. If you have a 200x100 image, the images width will always be twice the height, etc etc etc.

Unity - 2D - Draw a sprite with it's "real" size

Is there a way to draw a 10x20px sprite always with 10x20px regardless of the resolution?
I don't mean to add it as a UI/canvas object, I want to be able to place it in the world and move it or move the camera.
thx for any help!
If using an Image component you can hit the Set Native Size button in the Inspector
Set the dimensions of the image box to the original pixel
size of the Texture.
In order to move it and place it in 3D it has to be a child of a Canvas with RenderMode = WorldSpace
Then also note the options of the Canvas especially the CanvasScaler -> Reference Pixels Per Unit value. It should be 1 in your case before hitting Set Native Size in the Image.
You can do that by setting the Camera ortographic size to the correct size. Try the 2D Pixel Perfect Camera package by Unity https://blogs.unity3d.com/2019/03/13/2d-pixel-perfect-how-to-set-up-your-unity-project-for-retro-8-bits-games/
The script in that package will automatically set your Camera ortographic size with provided parameters. If you want to manually set it, the formula is:
Camera ortographic size = vertical resolution / PPU / 2
Vertical resolution means the target vertical resolution that your sprite was designed for. For example, if your sprite is designed to look pixel perfect on 1600x900 screen, then the vertical resolution will be 900.
PPU means the pixel per unit, by default all imported sprites are set to 100.

Canvas too big for the camera in Unity

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?

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)