automatic screen resolution for Android?
Hey all, I'm pretty new to Android applications and coding in general and was wondering if there is a way to make my main camera automatically adjust to different screen resolutions for mobiles that run Android?
I'm not asking for an in depth tutorial but I've looked over and have found no real easy answers, I'd just like a bit of help on getting started. So do I need to use code or does Unity have a way of doing this already in the engine?
Thanks.
Answer by Darsak · Feb 09, 2016 at 10:26 AM
@ShazBang, I have created following Script that automatically scales game object and reposition its position according to Android / iOS or any device.
Place this script on each game objects that you need to scale or reposition according to device.
[/*////////////////////////////////////////
|| //
|| @author : Darshan Mehta //
|| @email : mehta.darsan@gmail.com //
|| //
///////////////////////////////////*/
using UnityEngine;
using System.Collections;
public class ScaleRepositionScript : MonoBehaviour
{
public bool isScalableX;
public bool isScalableY;
public bool isRepositionableX;
public bool isRepositionableY;
public float ratio = 1.76f;
void Start ()
{
float scaleFactor = Camera.main.aspect / ratio;
if (isScalableX) {
this.gameObject.transform.localScale = new Vector3 (this.gameObject.transform.localScale.x * scaleFactor, this.gameObject.transform.localScale.y, this.gameObject.transform.localScale.z);
}
if (isScalableY) {
this.gameObject.transform.localScale = new Vector3 (this.gameObject.transform.localScale.x, this.gameObject.transform.localScale.y * scaleFactor, this.gameObject.transform.localScale.z);
}
if (isRepositionableX) {
if (isRepositionableY)
this.gameObject.transform.position = new Vector3 (this.gameObject.transform.position.x * scaleFactor, this.gameObject.transform.position.y * scaleFactor, this.gameObject.transform.position.z);
else
this.gameObject.transform.position = new Vector3 (this.gameObject.transform.position.x * scaleFactor, this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
}
}]
Hope this works for you.
Your answer
Follow this Question
Related Questions
Is there a way to access mobile (android) wide angle camera in unity? 0 Answers
When i change the resolution in the game tab does the camera covers that much area only 0 Answers
Grey/Dark line between two cubes 1 Answer
Screen Space - Camera for Mobile (Android) not working 0 Answers
Android Screen Resolution 0 Answers