Hi. So I’m trying to have a nice parallax effect in a 2D game I’m trying to make but I’m having trouble getting my sprites to scale to all resolutions.
Here’s the script that I have attached to my background sprites:
using UnityEngine;
using System.Collections;
public class BackgroundScaler : MonoBehaviour {
public int textureSize = 194;
void Start () {
var width = Mathf.Ceil (Screen.width / (textureSize / CameraScaler.scale));
var height = Mathf.Ceil (Screen.height / (textureSize / CameraScaler.scale));
if (gameObject.name == "Bg1") {
transform.localScale = new Vector3 (width * textureSize, 40f, 1);
} else if (gameObject.name == "Bg2") {
transform.localScale = new Vector3 (width * textureSize, 40f, 1);
} else if (gameObject.name == "Bg3") {
transform.localScale = new Vector3 (width * textureSize, 100f, 1);
} else if (gameObject.name == "Grass") {
transform.localScale = new Vector3 (width * textureSize, 20f, 1);
} else if (gameObject.name == "Ground") {
transform.localScale = new Vector3 (width * textureSize, 70f, 1);
} else {
transform.localScale = new Vector3 (width * textureSize, height * textureSize, 1);
}
GetComponent<Renderer> ().material.mainTextureScale = new Vector3 (width, height, 1);
}
}
And here’s the script I have attached to the main camera:
using UnityEngine;
using System.Collections;
public class CameraScaler : MonoBehaviour {
public static float pixelToUnits = 1f;
public static float scale = 1f;
public Vector2 nativeResolution = new Vector2(800,480);
void Awake() {
scale = Screen.height / nativeResolution.y;
pixelToUnits *= scale;
Camera.main.orthographicSize = (Screen.height / 2.0f) / pixelToUnits;
}
}
Can someone please help me out? Thanks!