Last active
July 19, 2016 16:25
-
-
Save jrunestone/8fa06196fd11b9a27c449f39a0a0c17a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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