Search Unity

Scale sprite problem

Discussion in '2D' started by Talon911, Oct 11, 2015.

  1. Talon911

    Talon911

    Joined:
    Jul 25, 2015
    Posts:
    7
    I tried to scale my background sprite to the screen resolution, but it doesn't work.
    What am I doing wrong?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScaleBackground : MonoBehaviour {
    5.  
    6.     GameObject Background;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         Background = GameObject.Find("Background");
    12.  
    13.         float windowWidth = Screen.width;
    14.         float windowHeight = Screen.height;
    15.  
    16.         float spriteWidth = Background.transform.GetComponent<BoxCollider2D>().size.x * 100;
    17.         float spriteHeight = Background.transform.GetComponent<BoxCollider2D>().size.y * 100;
    18.  
    19.         float backgroundWidth = windowWidth / spriteWidth;
    20.         float backgroundHeight = windowHeight / spriteHeight;
    21.  
    22.         // Scale Sprite
    23.         Background.transform.localScale = new Vector3(backgroundWidth, backgroundHeight, 0);
    24.     }
    25.     // Update is called once per frame
    26.     void Update () {
    27.    
    28.     }
    29. }
    30.  
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Talon911 likes this.