- Home /
Android: ScreenResolution/Scaling Script
Preface : I am new to Unity.
Here we are....
I have read over countless posts, but none of them give a simple solution to the question:
How do I make my app scale properly to any android screen resolution/size?
I stumbled upon a members script (CarterG81's Code), the only problem is that if I use his default PixelsPerUnit scaling, I get insanely large sprites. To fix this, I figured the best way to do it would be to scale the PPU variable depending on the devices screen dimensions (Screen.currentResolution.width or height).
Here is a bit of the code:
using UnityEngine;
using System.Collections;
public class ResolutionCorrection : MonoBehaviour {
public float adjusted;
void Awake () {
// ORIGINAL 16:10 Aspect Ratio
// Base resolution width = 1280
adjusted = 40.0f * (Screen.currentResolution.width/1280.0f);
}
void Update () {
this.camera.orthographicSize = Screen.height * gameObject.camera.rect.height / adjusted / 2.0f;//- 0.1f;
}
}
The original code did not scale the PixelPerUnit variable based on the screen size (i.e., if your screen size is half the size of the default resolution, you half the PPU variable (in this case, from 40 to 20)).
Now, this script does work, but it is janky. I almost need to set a dedicated c# to each camera, on each scene, in order to make each scene look properly. (e.g., the intro screen will be scaled perfected, but the next scene is huge). Whenever I fix the resolution error using this method, my parallax screen speed changes, which no longer matches game objects that are moving on my main camera (parallax uses its own camera).
Is there anyway to fix this issue? It is almost as if I could make a universal script that anyone could just drag and drop on their cameras to fix the issue. If I find an answer, I will post it, so that you guys have access to it. Sorry for the long post (it is shorter than I thought it would be, but I managed to get the eye tracking to control the gameObjects by using a c# wrapper for some of my C++ code, but that is neither here nor there :D)
I love you all.
adjusted = 40.0f * (Screen.width/1280.0f);
Corrects some issues. I used a variable default size (the 1280.0f was labeled as a public float in my testing phase. It works better than anticipated. I can get my whole app to resize (save one page that only contains TWO sprites (one I used as a background, yeah, I know that was bad.)
Your answer
Follow this Question
Related Questions
Android Screen Resolution. 3 Answers
GameObject or Screen Size Shrink? 1 Answer
Screen.width and Screen.currentResolution returning unwanted value 0 Answers
How can I fix the screen disturbance of android 4.1/4.2 ? 0 Answers
Android Resolution Problem 1 Answer