Search Unity

Scale camera to screen height and width

Discussion in '2D' started by VirusXProgramming, Mar 16, 2015.

  1. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
    Hi!
    I'm developing a game for the ios and android platforms, It's 2d and the thing is I'm running into a problem.
    The app's screen is perfect let's say on the Iphone 4S but it's cut off on Iphone 6(+) and android and etc..
    I don't care if you have a larger screen you can see more to the side but how do you make the camera display to fit the whole screen?

    I work in c#, Thanks in advance!
     
  2. VirusXProgramming

    VirusXProgramming

    Joined:
    Nov 15, 2014
    Posts:
    67
  3. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. [ExecuteInEditMode]
    4. [RequireComponent(typeof(Image))]
    5. public class Background : MonoBehaviour
    6. {
    7.     public delegate void OrientationChanged();
    8.     public static event OrientationChanged OnOrientationChanged;
    9.     private Image _image;
    10.     private bool _wide;
    11.     private void Start()
    12.     {
    13.         _image = GetComponent<Image>();
    14.         _wide = Screen.width > Screen.height;
    15.         if (_wide) Wide();
    16.         else High();
    17.     }
    18.     private void Update()
    19.     {
    20.         var oldWide = _wide;
    21.         _wide = Screen.width > Screen.height;
    22.         if (_wide && !oldWide)
    23.             Wide();
    24.         else if (!_wide && oldWide)
    25.             High();
    26.         if (_wide)
    27.             _image.transform.localScale = new Vector3(Screen.height / _image.preferredWidth, Screen.width / _image.preferredHeight, 1);
    28.         else
    29.             _image.transform.localScale = new Vector3(Screen.width / _image.preferredWidth, Screen.height / _image.preferredHeight, 1);
    30.     }
    31.     private void Wide()
    32.     {
    33.         _image.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, 90.0f);
    34.         if (OnOrientationChanged != null)
    35.             OnOrientationChanged();
    36.     }
    37.     private void High()
    38.     {
    39.         _image.transform.localRotation = Quaternion.identity;
    40.         if (OnOrientationChanged != null)
    41.             OnOrientationChanged();
    42.     }
    43. }
    44.  
     
    Last edited: Mar 26, 2015