- Home /
Different camera views in different devices
Hello.
In my editor I made game with resolution 1280 x 800. I made camera to fill this stage.
That how it looks in my editor:
But when I build *.apk file and installed it into phone with small screen it will look like that:
So it does not show all the stage.
But then I have installed it into device with big screen (Galaxy Note 2). It shows me not only stage with white background but also takes parts that are redundant to player:
http://cs14108.vk.me/c620325/v620325770/170c/ThPlvD-dUQs.jpg
Question: What kind of script code I must to write(in C#) to make it look like in editor, when I run it on every device?
The way Unity handles different sized devices is that it maintains the same view height but sizes the width accordingly. So an iPhone 5S will be extremely narrow whereas an iPad will be wide, but the height you see will be consistent. You can change this behaviour with scripts so that it expands the height ins$$anonymous$$d of the width but at the end of the day you will never get it to be consistent across all devices.
There are a lot of tricks that developers use to get the games to look as consistent as possible across devices with varying aspect ratios, but they are just tricks.
Also for your specific game you want to make sure that in your Android build settings that it is forcing the orientation to be Landscape.
Answer by jogidipen · Mar 28, 2014 at 06:29 AM
I given answer for your solution in this link.
just create and apply script on main camera.
I had the same problem and that script solved it :) thank you very much :)
does this work when used on an iPad, I've used canvas scaler and my ui is great but the rendering my intractable elements do not move with canvas scaler
Answer by aqib · Mar 28, 2014 at 06:46 AM
I hope this will help you out. Add this script to main camera. You can recalculate the ratio if you want.
using UnityEngine;
using System.Collections;
public class FixedRatio : MonoBehaviour {
// Use this for initialization
void Start ()
{
// set the desired aspect ratio (the values in this example are
// hard-coded for 16:9, but you could make them into public
// variables instead so you can set them at design time)
float targetaspect = 1280.0f / 800.0f;
// determine the game window's current aspect ratio
float windowaspect = (float)Screen.width / (float)Screen.height;
// current viewport height should be scaled by this amount
float scaleheight = windowaspect / targetaspect;
// obtain camera component so we can modify its viewport
Camera camera = GetComponent<Camera>();
// if scaled height is less than current height, add letterbox
if (scaleheight < 1.0f)
{
Rect rect = camera.rect;
rect.width = 1.0f;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1.0f - scaleheight) / 2.0f;
camera.rect = rect;
}
else // add pillarbox
{
float scalewidth = 1.0f / scaleheight;
Rect rect = camera.rect;
rect.width = scalewidth;
rect.height = 1.0f;
rect.x = (1.0f - scalewidth) / 2.0f;
rect.y = 0;
camera.rect = rect;
}
}
}
This is pretty fine solution too. However, when you try to launch game in device with higher resolution, it shows black lines.
Answer by ivomarel · Mar 28, 2014 at 06:46 AM
I would recommend using a plugin such as NGUI for your UI. NGUI uses something called anchoring, which can (for example) anchor your object to the top right corner, so they always stay there, no matter the resolution.
For other tricks, I'd recommend creating a background around your game that repeats itself (in case of orientations that are too wide/too high). I use this for a platform runner, where the width is always the same, but based on the aspect ratio, the player sees less/more sky.
It is not wise to use UI solution for actual gameplay since it adds lots of overhead.
Answer by gabigarciagar · Oct 21, 2018 at 05:56 PM
I have added such script implemetation to my game but I still have 1 problem. Because of the images resize, the canvas resize doesnt fit the real game screen. See pictures as example:
Before playing I force free aspect and the canvas resize to fill the full size of the real screen Then I press play and the game resize with the camera script but the canvas remains the same
¿Who can alfo fix this? Thank you