Adjusting the 3D game to the screen size - unity3d

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.

Related

How to resize camera to work with multiple resolutions on Unity?

I'm working on a fun project on Unity and I want to support all mobile resolutions in landscape mode. I designed everything to work in 1920:1080 resolution.
Everything works in world space, including UI elements.
What's correct way of supporting all resolutions (including weird ones like square 1:1)?
I don't want the scene to be cropped or filled with blue, all I want is my camera's viewport to be scaled to fit the device screen. I don't care if objects in the scene will get thin or fat.
To support different aspect ratios for your UI Elements, I recommend making use of the anchors that come with every RectTransform. These will ensure that the position of an element is consistent across various aspect ratios. For example, setting the anchor of an element to be left on the x axis will make the origin and pivot on the far left. This means an x position of 10 will position the element 10 units away from the far left of the element's parent. This is made easier with Anchor Presets. Although you can set every value yourself, Anchor Presets provide an easier way to anchor your UI Elements. Provided you have a RectTransform on you UI Elements, every element should have the Anchor Presets available. No need to change the width and height of your Canvas.
Aside from that the Camera should automatically resize depending on the resolution of the game view. You can test how your elements' anchoring works with different aspect resolutions by setting your game view's Aspect to free aspect, which will change the aspect ratio of the game depending on game view's width and height.

UI is shown too small in final (built) game

I'm a newbie to Unity and am learning. I've made my first game which is a simple platformer, along with a main menu. The game also has some text GUI elements. When I run it in Unity's built-in player, the GUI looks fine, but when I build the project and run the game using the .exe, the UI is scaled down.
I'm attaching some screenshots below to clarify this.
(As seen in Unity player)
(As seen in game)
Also, the game UI also looks scaled down:
(As seen in player)
(As seen in game)
I want the in-game UI to be exactly like the one seen in the Unity player.
How do I fix this?
The Canvas can be set to scale with different settings if you want it to be accurate you should choose:
-Scale With Screen Size
Using the Scale With Screen Size mode, positions and sizes can be specified according to the pixels of a specified reference resolution. If the current screen resolution is larger than the reference resolution, the Canvas will keep having only the resolution of the reference resolution, but will scale up in order to fit the screen. If the current screen resolution is smaller than the reference resolution, the Canvas will similarly be scaled down to fit.
1- Go to your Canvas and select the Scale With Screen Size option
2- After that, make sure that the Reference Resolution is the same as the Game Window resolution in your Unity layout, I leave here an example:
As you can see, the resolution set on my Game window is (1024x768) and the reference resolution of the Canvas too.

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

Unity: Positioning an element on canvas

I need to move an image down through canvas so that its central point would be where is now its top edge. It makes some 50 points, but if I decrease y by 50, it moves to different part of the screen on devices with different screen size. I guess, it's because my main canvas is set to scale with the screen size. So I suppose I need to manually divide the number 50 by my screen height and then code to multiply by Screen.height? Isn't there a more convenient way to move UI objects?
Allow me a second question: Do you think it is even wise to make a game purely on canvas? My game is simple 2D, only slightly animated and contains many layout elements, so I decided to go for it, but I have hard time to grasp the UI position rules.
you may have the problem of the anchoring.
Unity UI totally depends on the Anchoring, if you have got right anchoring there is no issue.
For example. if you anchored something at the Center than changing left and right value moves them according to the center anchor.
for clear visualization, you can paste a screenshot of the behavior.

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?