Search Unity

2D games For All Resolutions

Discussion in '2D' started by mathewsbabu, Apr 21, 2015.

  1. mathewsbabu

    mathewsbabu

    Joined:
    Sep 30, 2014
    Posts:
    33
    How can we create sprite renders that will fit all resolutions..That is in different mobile devices.I know that Canvas Helps with anchoring property.But Sprite Renders do not have this.So what to do??
     
  2. codeedward

    codeedward

    Joined:
    Dec 19, 2014
    Posts:
    93
    Just create Your game object. Unity do this automaticly for You. You could change resolutions in Your editor to check behaviour. So canvases need additional component named CanvasScaler, but normal GameObjects not.
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The canvas is designed for UI elements. Sprites that are not used as part of a UI element are simply other GameObjects. Can you tell us more snout what it us that you are trying to do?
     
  4. mathewsbabu

    mathewsbabu

    Joined:
    Sep 30, 2014
    Posts:
    33
    if i placed a gameobject at left end in iphone ,it should be on the left end if i select ipad screen..but sadly it is not
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  6. codeedward

    codeedward

    Joined:
    Dec 19, 2014
    Posts:
    93
    First of all I need to tell You @mathewsbabu that I was wrong when I answered you. I thought about scaling within the same ration. About Your question - yesterday I had the same problem but I almost sure that I solved it.

    Fast look -> I have game with many tiles. I want to have 10 tiles from left to right. The same number of tiles in different resolutions and no extra empty spaces on left and right. I had a good look for one resolution but for other it was bad. Then I found in google some tricky way:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraGameScript : MonoBehaviour {
    5.  
    6.     public float orthographicSize = 454;
    7.     public float aspect = 0.648f;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     public void InitializeCameraProjectionMatrix()
    15.     {
    16.         Camera.main.projectionMatrix = Matrix4x4.Ortho(
    17.             -orthographicSize * aspect, orthographicSize * aspect,
    18.             -orthographicSize, orthographicSize,
    19.             camera.nearClipPlane, camera.farClipPlane);
    20.     }
    21. }
    Of course initialization is somewhere in my other script.

    Now in all different resolutions I have 10 tiles exactly from left to right without any extra space. I can even run it landscape! (Everything is then very widely, but it is still exactly from left to right). Of course I adjusted these magic numbers. However, at the end of the day I do not know how it is working! @Adam Buckner do You know why I need to put these magic numbers? How it its working and why it is so easy to do this? Probably this way have some defects whose I do not know.
     
  7. mathewsbabu

    mathewsbabu

    Joined:
    Sep 30, 2014
    Posts:
    33
    Thanks for the reply @Adam Buckner and @codeedward ..Now am into some other stuffs..will let u know after checking this