I'm working on Unity3D 2D mobile game and I've got a problem connected with aspect ratio. When changing aspect ratio the camera always fits vertically. For landscape mode games it is a good practice. For portrait mode games it is better to fit camera horizontally. So My game needs to fit camera horizontally. Please help me with it.
Let's say your aspect ratio is 4:3
If you want to make your camera fit landscape mode of 16 units wide, you would make your camera a size of 16 / 4 * 4
If you want to make your camera fit portrait mode of 16 units tall, you would make your camera a size of 16 / 3 * 4
Related
I'm developing a 3d game on unity and using the 1080x1920 portrait resolution.
When I build and run the project on my phone, which has a 1080x2340 resolution in portrait, it cuts off part of the game (the sides).
red lines indicating where it cuts off
The camera doesn't follow, so the player can "go out of the screen".
How can I fix this, making sure my game looks the same on different resolutions?
(edited to correct resolution order)
Does your play area have a fixed size? If the in-game play area's aspect ratio isn't the same as the device running the game, you'll have to either cut off part of it or include blank space at the top/bottom or left/right. Unity's camera orthographic size dictates how tall the camera view is, but not how wide. The width is just screenAspectRatio * camera orthographic size.
To fix this:
float aspect = Screen.Width / Screen.Height;
myCamera.orthographicSize = Mathf.Max(playAreaHeight, playAreaWidth / aspect);
This will cause the camera's view to be either too wide or too tall rather than too small.
I am trying to set-up my game's HUD so that it scales properly at any resolution, and I ran into a problem I haven't been able to solve yet.
My game uses a dynamic crosshair which consists of 4 images (no sprites, just an Image element with a color) of 2x8/8x2 size, depending on the orientation.
Depending on if you're moving or not, the crosshair gap increases by increasing the X/Y positions of the 4 pieces of the crosshair to show that the accuracy of the weapon is being reduced.
This works and scales perfectly at almost any resolution, but in very low resolutions such as 640x480, the crosshair disappears completely. My guess is that the crosshair images are being shrinked to scale for this resolution, and this causes the width or height of the images to become 0, making the crosshair invisible. If I increase the width or height to 3 pixels, then the crosshair doesn't dissapear at this resolution, but that's not a viable solution. I want the width/height to be 2 pixels.
This is my canvas scaler setup:
https://imgur.com/a/bzPSHRu
And this is the crosshair setup:
https://imgur.com/a/7KDMsgc
https://imgur.com/a/qml6UWB
https://imgur.com/a/970Q4im
https://imgur.com/a/Stvhy6J
This is what happens at different resolutions:
1920x1080 (looks fine): https://imgur.com/a/vV1fuJj
1024x768 (looks fine): https://imgur.com/a/iMUi7oM
640x480 (crosshair becomes invisible): https://imgur.com/a/3v66tkr
Any ideas on how to solve this issue? Thanks a lot in advance!
How can i make a sprite size appear 5cm in every screen size and in every resolution in unity 2D? I can use canvas image also depending upon your suggestions i tried everything not working..
Write a script to resize the camera so that the object with world size (Width, Height) always exactly matches the camera horizontally or vertically, and see if you can fix the sprite size,This is my own understanding, and I don't know if it works or not.
Which view mode are you using for your game? Is it landscape or Portrait?
You can use 2960 X 1440 Landscape if you are using landscape mode, and 2960 X 1440 Portrait if you are using portrait mode from the resolution aspect in game mode. These are the highest resolutions for both landscape mode and portrait mode. When you open your game on any other resolution phone the resolution of your sprite will be set accordingly. Always use the highest resolution in this regard.
Have you tried using Screen.dpi?
Screen.dpi
like mentioned in this post?
I am completely confused on how to correctly size my lans background image which is landscape based. As of right now I'm doing GameScene(size: self.frame.size) and I print out the size of the screen so I know what size to make my background and it turns out my background should be 1024x768 but that doesn't seem like landscape dimensions? So I made my background 1024x768 but the entire image doesn't fit into my iphone when its landscape because the iphone lanscape dimensions arent 1024x768. How do I make a background that will look 1:1 with the dimensions of my iphone? The only way I can think is if I set the GameScene(size: CGPoint(x: 1334, y: 750)) but then wont it be screwed up for any other device? What's the best way to approach this? I have an artist who is going to make a background for me but I have no idea what dimensions to give him.
When dealing with scenes for Sprite Kit, try not to focus on the screen size, because screen size is no longer a factor (now this is not 100% absolute fact, this is a general rule to go by)
Instead, treat your SKScene as if it was a virtual screen. The size of your SKScene is the "resolution" of your SKScene, and the OS will work in the background to figure out how to convert 1 virtual pixel (From here on out we will call point) to screen pixels( referred to here on out as pixels)
Now there is only 1 special case where the OS will change the resolution (scene size) to match the screen, and that is .resizeFill The other 3 will never change resolution on you.
.aspectFill and .aspectFit will ensure that your point to pixel conversion keeps and equal width and height (e.g. 1 point could equal 4x4 pixels) The only difference is .aspectFill will expand to fill the entire screen, meaning that excess points will be rendered outside the native screen bounds [ so (0,0) may lie 20 pixels left of the left most pixel, thus not being visible] and .aspectFit will fill till it hits a screen border, leaving black bars to fill the unused pixels.
Now .fill does not keep and equal width and height point to pixel ratio, and in the case of a 4:3 going to a 16:9 screen, you will notice that your point to pixel will be 5:4 because a 16:9 screen is 25% wider than a 4:3. This gets you the fatty effect.
So when dealing with your game you need to figure out the desired effect. If you set your scene size to 1024x768, then all non retina iPads will have a 1:1 pixel to point ratio, where retina has 2:1 pixels to point ratio. For an iphone 5, you would get roughly 1.14 pixels to every point (iphone is 1168 and your scene is 1024, so you do 1168/1024) then of the 768, you would be loosing 25%, because the ipad is 25% taller than an iphone in landscape. This means only 576 points will be showing, and the rest are in invisible screen space.
Basically, you can never get a 1:1 with both an iPad and an iPhone doing a universal app because you are working with 2 different aspect ratios. You are going to have to make 2 different sets of assets, or take some creative liberties that doesn't alter the gaming experience. This depends entirely on the game and unfortunately nobody will be able to answer it till they have an understanding of your game.
at the moment I start with Unity in 2D development for android. Before I develop in Unity i develop with LibGDX where I can the Viewport to a static screen resolution at 1080 * 1920, if the game starts on a smaller device for example 480 * 800 the game still looks pretty good. In Unity when I use a Orthographics Camera and set the width to 1080 and the height to 1920 it looks like a equal long quadrate and not portrait.
How can I use a static Camera which the Viewport is 1080 * 1920 and for other devices unity self charge the resolution for the game?
Sorry for my bad english :(
greetings coco07!
You can't. You're thinking about 2D engines. Unity is a 3D engine with a 2D extension (sort of). You set your camera up (doesn't matter what orthographic size you use), and it gets scaled to the viewport of the device it's running on. the size you set is the size of the viewport, in world units, along the y axis of the device's display. The width is set automatically based on the device's aspect ratio. You can see this by resizing the game window in unity. Doing that causes the visualization of the camera's bounds to change. To create a static camera, you'd have to manually add black borders around the edges (or at least I think you do), OR, better yet, create your game in a manner that runs on all aspects. You should consider aspect ratios between 4:3 and 16:9 for landscape and 3:4 to 9:16 for portrait games. This is specially important since many recent devices use on-screen system buttons (such as the google nexus and Xperia Z series) which means you get an aspect ratio of a little bit less that 16:9 (or a bit more, on tablets). This doesn't sound like much, but if you down-scale a 16:9 image to fit on those devices' screens, it looks ugly as hell.