How to make game fit screen on Unity - android

I'm developing a game, and in Unity I've set the resolution to 1280x800.
When I export to Android, I expect it to adjust to the screen (adding black bars if necessary) but it isn't the case.
This is how it looks like on a Nexus 4:
And on a Nexus 5:
How can I make it so that it looks the same everywhere? (having black bars if necessary as I said before).

You need to normalize the camera's orthogonal size to a wanted resolution:
camera.orthographicSize = 640/screenwidth * screenheight/2
More info here

Related

Can't Center the scene to the screen in Godot?

I have few scenes in my project. I was able to center all of the other scenes. But i cannot center the main scene in my project. Its fine when it opened on a mobile device(probably because resolution matches i guess). but when i opened it in tablet(its an old tab "xiaomi tab 3") its always align to the right on the screen. I was able to center other scenes but i cant center the main scene to screen for some reason. project setting set as 2D and Expand. I also tried to use Control nod, canves layers, margine containers.. but nothing works. If someone can give any suggestions, Thank you in advance.
img1
https://i.stack.imgur.com/FJOHl.jpg
project
1920 x 1080
I have seen this problem, or similar, in windowed mode. I don't know if this is the exact case, but if it doesn't mess with your project, try full-screen mode.
If it's that, you can also take care of the window bar size (which is a problem because it can change from different devices), since godot will count its pixels too.
Notice that you're only having this problem in one axis.
Also, I think you could take a look into the Viewport Stretch Mode. It could mess your graphics depending of the intended result—but other projects won't even notice, so judge for yourself.
The viewport setting sets the root scene’s Viewport to the base resolution. The rendered output of the root Viewport is then scaled to the display resolution. [...]
The viewport setting is a better choice than the 2d setting when pixel-perfect precision is required, since primary rendering still occurs at the base resolution.
Support multiple devices
Scaling correctly for all devices could be an odyssey, so bon voyage.

Unity to Android scaling

Hello in my unity project i have made 2 levels but when i build and run them on android they always scale upwards or downwards either the buttons were way too big or the main menu icon was really small.. i was wondering if there is any way change the scale or configure the look from the editor. Any help is appreciated.Here are some screenshots. http://kharief.imgur.com/all/ ( i want the paper to be the background and scale down the buttons)
You can change the way the Canvas scales in the inspector. You can either have the pixels set to a constant size, scale with the screen size, or maintain a constant physical size.

Screen Rendering in Unity 2D Android Build Glitch?

I have searched both Stack Overflow, and Unity Answers, but I can't find any information on this problem. I have asked a question on unity answers, still awaiting response.
The problem I have is when I build to either Android, or Web, (haven't tried others), there is a black bar on the right of the screen. On android, it is a duplication, 4 times, of a strip of the screen. about 20px wide, and 1/4 of the screen height, quadrupled up the right side of the screen. When I move objects through the area of the screen being copied, it is animated in the side bar.
on the Web build it is just black, but should be blue background.
My game is 2D, using orthographic camera.
Please help! I have no idea how to even properly search this kind of problem.
I just did a Web player build in a new test project, and it was fine, so it must be some setting in my game project,
Thanks in advance
Edit.
I also did a build in android with a new test project no problems. Then I created a new project, and copied my game assets and other folders into the project directory. Then the silly glitch happened again. I am at a loss here. Any ideas?
Edit * [Solved] Thank you to Josh, the problem was solved!!!! What was going on was on my mainCamera, the Viewport Rect setting were of. I had the settings as
X=0, Y=0, W=0.975, H=1.
So a bit of the width of the screen was being clipped.
I set the settings to the default,
X=0, Y=0, W=1, H=1.
And it worked!!!!
this is a picture of the glitch on my HTC One V Android
This is the glitch on Chrome Web player, no screen copying, just black, where the background should be blue
Have a look at you camera. The first property is 'Clear Flags'. If that is set to 'Don't Clear' whatever was last drawn in any area of the screen will remain in the next frame.
Try setting it to 'Solid Color', or 'Skybox' and set the Skybox Material parameter in Edit -> Render Settings.
Also, check your camera's 'Viewport Rect' and ensure that it's parameters cover your screen. The default values do this; X=0, Y=0, W=1, H=1.
The aspect ratio of your the window your game is being run within will affect the ratio of your camera's bounds. It's not always the same as the editor window within Unity. You can lock the editor window to a specific resolution or aspect ratio by selecting to dropdown box in the top left of you 'Game' tab within Unity.
Beyond this it's hard to tell what's causing the issue without more information. Are you doing something funky with shaders, multiple cameras or rendering paths?

How to make GUI Buttons on GUI Texture resolution independent in Unity3d?

I am newbie to Unity3d and Android developent, and I am in need of your help to make my GUI controls resolution independent.
I have a GUI Texture in my scene to be used as a graphical background for a controller (see image 1):
Above this, a set of GUI buttons is rendered in absolute x,y screen positions (see image 2):
Those two elements rendered together makes the buttons controller for my android application, in a 480x320 HVGA Landscape screen resolution (see image 3):
The problem is that if the resolution is changed to 800x480 WVGA Landscape, everything is meshed up as you can see in the following screenshot:
I have tryed to make use of the Matrix4x4 to keep the GUI buttons scaled to a new resolution, but I still get them scattered all around screen.
Could anyone be kind enough to give some advise or maybe a code snippet on how to make both GUI buttons on absolute possitions and GUI Texture, screen resolution independent?
Thank you all in advance for your answers.

How to Work With Different Screen Resolutions

I'm using Unity 4.3.3 for my game. I'm making a game for Android and iOS devices. Since I'm pretty new to unity, I can't seem to find a way to work with different screen resolution.
I'm using c# for my scripts.
I want my game to run full screen. I've tested it on iPad 2 and it runs perfect but for iPhone 4 the sides are clipped and for iPhone 5 they are clipped even more. What is the solution? Which option should I choose under Player Settings > iOS > OtherSettings > Target Resolution?
unity changes its ingame camera to match the dimensions of the aspect ratio of the screen it's using. Obviously this isn't always desired, so there's a couple of ways around this:
1) First method is very easy to implement, though it rarely looks good. You can simply change the camera's aspect ratio to match the one you've designed your game around. So if for example you've designed your game at 4:3, you'd do something like this:
Camera.aspect = 4f/3f;
Just keep in mind that this will distort the game on different aspect ratios.
2) The other method is a little more complex, but the result looks much better. If you're using an orthographic camera, one important thing to keep in mind is that regardless of what screen resolution is being used, the orthographic camera keeps the height at a set value and only changes the width. For example, with an orthographic camera at a size of 10, the height will be set to 2. With this in mind what you'd need to do is compensate for the widest possible camera within each level (for example, have a wider background) or dynamically change the Orthographic Size of the camera until its width matches what you've created.
GUI components are easier to implement simply by setting their position to depend on the screen resolution wherever needed. For example if you want a button to appear in the top right corner, you simply set its position to something like position = new Vector2(Screen.width - 50, 50);
EDIT: Since you mentioned devices you used, I did a bit of looking up to find what aspect ratio each one uses: Ipad2 = 4:3 , Iphone4 = 3:2, and Iphone5 = 16:9
Since you said it looks perfect on the Ipad2, I'm guessing you've designed your game to use a 4:3 aspect ratio, so the "clipping" you mention is just that your orthographic camera has widened. The solution is to use one of the two methods I described above.
You can see how it will look on each device within the unity editor fairly easily. If you go to the game tab (what pops up when you press play in the unity editor), you'll see it has a device name, along with a resolution and aspect ratio. Changing this will let you see what it will look like at different aspect ratios.
You may want to try capturing the device resolution by using Camera.ScreenWidth and Camera.ScreenHeight.
Then do some math to calculate the difference from the resolution you have and apply it to the parent object.
This will scale everything bigger/smaller to fit the device resolution without having multiple image resolutions etc for different resolution devices.
If your goal is to get the previous width:
[RequireComponent(typeof(Camera))]
public class ResizeCamera : MonoBehaviour
{
private float DesignOrthographicSize;
private float DesignAspect;
private float DesignWidth;
public float DesignAspectHeight;
public float DesignAspectWidth;
public void Awake()
{
this.DesignOrthographicSize = this.camera.orthographicSize;
this.DesignAspect = this.DesignAspectHeight / this.DesignAspectWidth;
this.DesignWidth = this.DesignOrthographicSize * this.DesignAspect;
this.Resize();
}
public void Resize()
{
float wantedSize = this.DesignWidth / this.camera.aspect;
this.camera.orthographicSize = Mathf.Max(wantedSize,
this.DesignOrthographicSize);
}
}
You need to change camera viewport according to device's aspect ratio.
Suppose you made the game for 720x1080
Then in the script you should do
float xFactor = Screen.width / 720f;
float yFactor = Screen.height / 1080f;
Camera.main.rect = new Rect(0,0,1,xFactor/yFactor);
Or
The another way is, if you are making a game for aspect ratio 9:16 then
Do this in your script,
public float targetRatio = 9f/16f; //The aspect ratio of your game
void Start()
{
Camera cam = GetComponent<Camera>();
cam.aspect = targetRatio;
}
I know it's an old thread, but people might still end up looking for an answer.
To adjust a perspective camera to different screen aspect ratios with different behaviour than the Unity default, you need some trigonometry. All the necessary info about that is here: https://en.wikipedia.org/wiki/Angle_of_view
If you want an easy way out, though, here's a solution that works with all camera types: http://u3d.as/oUX
Ok, this is a big question. Many people have referred to forcing an aspect ratio, which is a good solution to consider. However for many games this is not appropriate. For many game designs, you want the game to feel like it's native to the device/screen it's running on, and you want to use every square inch of screen real-estate you can. To do this, you must build and test every system in your game for different sized and shaped screens. It's a very similar problem to building apps and websites for multiple devices.
Screen Size
The screen size can be retrieved programmatically using the DPI and the resolution with a statement such as:
Resolution res = Screen.currentResolution;
float screenSquareInches = res.width * res.height / Screen.dpi;
For the content on the screen that is viewed through the camera, there's only one variable that needs to be scaled, and that's the size of the camera (or how much stuff in the game world that will be squeezed onto the screen). You can control the size of the camera with the field of view for 3D games or the orthographic size for 2D games. Only testing will say what equation supports the widest range of screen sizes, but something along these lines could serve well:
// this should be a tweakable number, so you can fine tune it to your needs. It represents the screen size at which the camera size should not be scaled at all
float expectedScreenArea = 20;
// this number should also be tweakable, which declares how much smaller/larger text can get depending on the screen size. Generally larger screens are viewed from farther away, so scaling should be slight
float cameraScalingRate = 0.25f;
// multiply this number by the default/base camera size you have picked, whether that's field of view or orthographic size
float cameraSizeMultiplier = 1 + (screenSquareInches / expectedScreenArea - 1) * cameraScalingRate;
Ok, that should get you most of the way on scaling the game world, but what about UI elements, things like text, HUD, and on-screen controls? Well, for elements like text, the above equation should work decently well, though you might want a different value for the scaling rate. For on screen controls, you might want a more full transformation, such as joysticks moving to the bottom corners of the screen on a tablet rather than centered on the sides, or if it's not a touch screen device, remove on-screen controls altogether. That's outside the scope of this question.
Screen Aspect Ratio
The aspect ratio of the screen (how wide vs. tall is it) is also important and needs it's own handling. Generally the best way to do this is to freeze the vertical height of the screen, so that as the screen gets wider, elements on the screen are not scaled, but as the screen gets taller, elements on the screen scale proportionally to how much taller the screen grows. This is already the default behavior for cameras, but for UI elements you have to set it as such. This is relatively easy if you are using the new UI system with canvas'. On a CanvasScalar component, there's a property that controls how the elements are scaled depending on the aspect ratio. Change this to match width or height, and have it match height.
For Further Consideration
With supporting different screen sizes and shapes there's more to consider than just making everything fit. You also need to make sure you game plays well on the different screens. This ranges from things as subtle as not making the player feel claustrophobic on a small screen to making sure enemies can't shoot at the player if they're off screen to making sure you update boundaries that keep elements on screen.
Another thing to consider if this is a PC game is that someone might plug their computer into a different screen or change the resolution on the fly. With this in mind, it can be good to rerun the above equations as needed so a player doesn't have to exit the game to get things lining up right again.
The easiest way I've solved this was as pointed out by Steven, force my perspective camera aspect ratio to the one I develop for. Of course this deforms some objects if the aspect ration I developed for is to different from the one of the device. For UI objects, if you are using the build-in, I create the rectangles based on a percent of whatever the screen size is
Rect(0.5 * Screen.width, 0.5 * Screen.height, 0.25 * Screen.width, 0.25 * Screen.height)
That will place the top left corner at the center of the screen and create a rectangle of 1/4 the height and 1/4 the width of the screen. I'm using NGUI 2.7 for the UI and I haven't seen any issues with different aspect ratios so far, and that one uses an orthographic camera to render the UI
If you are developing 2d game,that is your camera is in orthographic view then Just do the following code and attach it with your gameobject. It will scale the gameobject(in this case the sprite) with the screen resolution.
void Resize()
{
SpriteRenderer sr=GetComponent<SpriteRenderer>();
if(sr==null) return;
transform.localScale=new Vector3(1,1,1);
float width=sr.sprite.bounds.size.x;
float height=sr.sprite.bounds.size.y;
float worldScreenHeight=Camera.main.orthographicSize*2f;
float worldScreenWidth=worldScreenHeight/Screen.height*Screen.width;
Vector3 xWidth = transform.localScale;
xWidth.x=worldScreenWidth / width;
transform.localScale=xWidth;
Vector3 yHeight = transform.localScale;
yHeight.y=worldScreenHeight / height;
transform.localScale=yHeight;
}
There is a free asset in Asset-store regarding this issue...Just check out...
http://u3d.as/content/palash-bhowmick/fit-all-screen-size/9q9
if shape of your object is changed, then delete the lines which starts with:-
transform.localScale.x=
if You have no choice You also can Do some more work and add a special Script to your camera that:
checks Screen Resolution for device
sets the target resolution for that
and also sets the target aspect ratio for that (IMPORTANT)
all you need to do is just writing a base function for checking the screen resolution (both width and height for devices that you want) and then add some more functionality to that for setting the proper screen resolution and aspect ratio!
Its very easy for Unity Now as Unity3D has its own UI system. For 3D Unity Adjusts the camera Automatically with screen size.
Problem arises for UI actually and its a little bit tricky. First of all try to figure out which elements needs to change position and size with screen size.
For example if you have a button exactly in the center of screen. Then it doesnt need to change the position according to screen. It will remain in center.
Now lets take a scenario in which you have a button in the corner of screen like we see pause button in top right or top left. Now you will have to anchor your button in the corner accordingly. It doesnt matter on which screen size the game is running, the pause button should stick to the corner.
Now lets take another case in which you have a background in your menu. Now you will have to apply stretch for this BG sprite. This BG will be stretching and shrinking accordingly but all items on it will have a fix size.
For this it is recommended to have BGs flat so that a same BG can have a good quality for 16:10 or 4:3
(Its all about Unity Native UI)

Categories

Resources