Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Properly scaling a 9x9 grid on iPhone 5 (9:16) res

Discussion in 'iOS and tvOS' started by sdviosx, Jan 10, 2015.

  1. sdviosx

    sdviosx

    Joined:
    Jan 4, 2015
    Posts:
    22
    Hello,
    I need help in positioning and evenly scaling my 9x9 grid made up of 64x72 tiles in the iPhone5(640x1136) screen.

    The main camera size settings is set to 5.68, the background is 640x1136 and the screen res is set to iPhone 5 tall (9:16).

    Here is the code I am using to create and position the grid, bear in mind I am a beginner in C #:
    Code (CSharp):
    1. public class SDASynergyFactory : MonoBehaviour {
    2.    
    3.     const float tileWidth  = 64.0f; // 32 for non retina
    4.     const float tileHeight = 72.0f; // 36
    5.    
    6.     public GameObject synergyPrefab;
    7.    
    8.     // Helper method that positions the 9x9 grid in lower left side of screen
    9.     private Vector3 PointForColumnAndRow(int column, int row){
    10.        
    11.         Vector3 screenToPoint = new Vector3((float)column * tileWidth + tileWidth/2 ,(float)row * tileHeight + tileHeight/2 ,0);
    12.         Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenToPoint);
    13.        
    14.         return worldPos;
    15.     }
    16.    
    17.     // Instantiate prefab, and make it a child of SDASynergyFactory then position SDASynergyFactory to the middle of screen.
    18.     public void AddSpriteForSynergys(HashSet<SDASynergy> synergys){
    19.        
    20.         foreach(SDASynergy synergy in synergys){
    21.            
    22.             Sprite sprite = Resources.Load<Sprite>("Sprites/" + synergy.SpriteName());
    23.             synergy.sprite = sprite;
    24.             synergyPrefab.GetComponent<SpriteRenderer>().sprite = synergy.sprite;
    25.             synergyPrefab.name = synergy.SpriteName();
    26.             synergyPrefab = Instantiate(synergyPrefab,PointForColumnAndRow(synergy.column,synergy.row),Quaternion.identity) as GameObject;
    27.             synergyPrefab.transform.parent = this.transform;
    28.         }
    29.        
    30.         Vector3 screenMidPos = new Vector3(Screen.width/2,Screen.height/2,10);
    31.         Vector3 midpoint = Camera.main.ScreenToWorldPoint(screenMidPos);
    32.         transform.position = midpoint;
    33.     }
    34.    
    35.     // Position SDASynergyFactory to the middle of the 9x9 grid.
    36.     void Start () {
    37.        
    38.         Vector3 synergyLayerMidPos =  new Vector3((float)SDALevel.numColumns * tileWidth/2,(float)SDALevel.numRows * tileHeight/2,0);
    39.         Vector3 worldPos = Camera.main.ScreenToWorldPoint(synergyLayerMidPos);
    40.         transform.position = worldPos;
    41.     }
    42.    
    43. }
    Code outputs this image: Screen Shot 2015-01-08 at 7.10.52 PM.png

    The desired result: Screen Shot 2015-01-08 at 7.14.16 PM.png

    Thanks in advance.
     
  2. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,572
    Rather than scaling the tiles, it would be much easier to change the camera size.

    Use ScreenToWorldPoint to determine the where the left hand edge of the screen sits. Divide that value by the point you want to be visible on the left (both relative to the camera centre point), then multiply the camera size by that amount.
     
  3. sdviosx

    sdviosx

    Joined:
    Jan 4, 2015
    Posts:
    22
    Thanks, I figured it out by changing tile width and hight variables from (64.0,72.0) to 0.64 and 0.72.