- Home /
Trying to adjust camera for different resolutions cuts out part of the image
I'm trying to write a script to adjust the camera for any screen size. I intend for the game to be played on mobile. When I add the script to my camera and adjust the resolution in Unity, everything works fine, however when I try to play it on my phone it cuts off part of the picture. Is it something wrong with my code or the way I set up my scene?
public class PortraitCam : MonoBehaviour {
// Use this for initialization
void Start () {
float width = Screen.width;
float height = Screen.height;
float ar = width / height;
gameObject.GetComponent<Camera> ().aspect = ar;
}
}
Answer by UnitedCoders · Feb 02, 2018 at 05:28 AM
You can achieve your desired result using this way. If you want adjust your All GameObjects sprites. Just simply create an Empty GameObject( at Vector3.Zero, name it ParentOfAll) add sprite renderer on it, add the below script . And make child all your GameObjects of ParentOfAll and adjust position and anchor of all your child GO. According to current Screen Resolution.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class BgScaleToScreenSize : MonoBehaviour {
SpriteRenderer sr;
// Use this for initialization
void Start () {
Resize ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.R)) {
Resize();
}
}
void Resize()
{
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;
//transform.localScale.x = worldScreenWidth / width;
Vector3 yHeight = transform.localScale;
yHeight.y=worldScreenHeight / height;
transform.localScale=yHeight;
//transform.localScale.y = worldScreenHeight / height;
}
}
This script adjust your Parent GameObject Scale to your device resolution, and fit on the screen. But it will lose the aspect ratio of the sprite. if you want to maintain the aspect then add this script
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class ScaleWidthToScreenWidth : MonoBehaviour {
SpriteRenderer sr;
void OnEnable()
{
Resize ();
}
// Use this for initialization
void Start () {
Resize ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.R)) {
Resize();
}
}
void Resize()
{
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;
transform.localScale = new Vector3 (xWidth.x, xWidth.x, 1);
//transform.localScale.x = worldScreenWidth / width;
// Vector3 yHeight = transform.localScale;
// yHeight.y=worldScreenHeight / height;
// transform.localScale=yHeight;
//transform.localScale.y = worldScreenHeight / height;
}
}
I hope this will works for you :) @CodyCantEatThis
I got it to work, but I didn't use the BgScaleToScreenSize since it would mess up everything. Thanks!
Answer by UnitedCoders · Feb 02, 2018 at 05:34 AM
If you want to maintain the anchor like bottomLeft,Right or whatever then add this script to your GameObject in which you want to maintain the anchor.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class MySetAnchor : MonoBehaviour {
public enum MyAnchorPoint {Center, LeftBottom,RightBottom,TopLeft,TopRight,CenterBottom,CenterTop,CenterLeft,CenterRight}
public MyAnchorPoint _MyAnchorPoint;
void OnEnable()
{
SetAnchorMethod ();
}
// Use this for initialization
void Start () {
SetAnchorMethod ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.R)) {
SetAnchorMethod();
}
}
void SetAnchorMethod()
{
if ( _MyAnchorPoint==MyAnchorPoint.LeftBottom) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.RightBottom) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.TopLeft) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.TopRight) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.CenterBottom) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (.5f, 0, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.CenterTop) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (.5f, 1, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.CenterLeft) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (0f, .5f, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.CenterRight) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (1f, .5f, 10));
}
else if (_MyAnchorPoint==MyAnchorPoint.Center) {
this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (.5f, .5f, 10));
}
}
}
I hope this will help you :)
Your answer

Follow this Question
Related Questions
Camera viewport should be square and pixel-perfect, regardless of player aspect ratio. 0 Answers
Cinemachine confiner on different aspect ratios 1 Answer
Outside game clear screen 0 Answers
Should texture assets be created for 300dpi? 0 Answers
Why is lightmap resolution different across certain objects? 0 Answers