Question by
InterventX · Sep 02, 2015 at 04:24 AM ·
scalequadparallax
Unity 5 2D - How to scale a quad to fit all screen resolutions?
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!
Comment
Your answer
Follow this Question
Related Questions
Can't seem to get the quad to scale with camera. 0 Answers
Resizing sprite? 1 Answer
spin casino -2 Answers
return all models to original location (image target) 0 Answers