Skip to content

Instantly share code, notes, and snippets.

@jrunestone
Last active July 19, 2016 16:25
Show Gist options
  • Save jrunestone/8fa06196fd11b9a27c449f39a0a0c17a to your computer and use it in GitHub Desktop.
Save jrunestone/8fa06196fd11b9a27c449f39a0a0c17a to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class QuadTextureScaler : MonoBehaviour
{
private Renderer rend;
private float textureWidthInUnits;
private float textureHeightInUnits;
private float pixelsPerUnit;
private void Start() {
rend = GetComponent<Renderer>();
Texture texture = rend.material.mainTexture;
pixelsPerUnit = Screen.height / (Camera.main.orthographicSize * 2);
textureWidthInUnits = texture.width / pixelsPerUnit;
textureHeightInUnits = texture.height / pixelsPerUnit;
}
private void Update () {
rend.material.mainTextureScale = new Vector2(transform.localScale.x / textureWidthInUnits, transform.localScale.y / textureHeightInUnits);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment