Skip to content

Instantly share code, notes, and snippets.

@sirgallifrey
Last active September 26, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirgallifrey/11276440 to your computer and use it in GitHub Desktop.
Save sirgallifrey/11276440 to your computer and use it in GitHub Desktop.
//Author: Adilson Schmitt Junior ,aka SirGallifrey
//This script is just to show how you could resize a objet to fit a orthographic camera fustrum size.
using UnityEngine;
using System.Collections;
public class ScaleObjectToCamera : MonoBehaviour {
public float widthPerc = 1.0f;
public float heightPerc = 1.0f;
private GameObject target;
private Camera camera;
private CameraSize _cameraSize;
void Start () {
_cameraSize = new CameraSize();
camera = GetComponent<Camera>();
target = GameObject.CreatePrimitive(PrimitiveType.Quad);
target.transform.position = transform.position;
target.transform.Translate(new Vector3(0,0,1));
}
// Update is called once per frame
void Update () {
//TODO: Fazer isso dentro do update não é interessante por que terão muitos resizes desnecessários. o legal é dar o resize somente quando o tamanho da tela mudar.
ResizeTarget();
}
void ResizeTarget() {
Bounds bounds = target.GetComponent<MeshFilter>().mesh.bounds;
target.transform.localScale = new Vector3(cameraSize.width*widthPerc/bounds.size.x, cameraSize.height*heightPerc/bounds.size.y);
}
public CameraSize cameraSize {
get {
_cameraSize.height = 2 * camera.orthographicSize;
_cameraSize.width = _cameraSize.height * camera.aspect;
return _cameraSize;
}
}
}
public class CameraSize {
public float height;
public float width;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment