Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class camera : MonoBehaviour {
  5.  
  6. // Use this for initialization
  7. void Start ()
  8. {
  9. // set the desired aspect ratio (the values in this example are
  10. // hard-coded for 16:9, but you could make them into public
  11. // variables instead so you can set them at design time)
  12. float targetaspect = 16.0f / 9.0f;
  13.  
  14. // determine the game window's current aspect ratio
  15. float windowaspect = (float)Screen.width / (float)Screen.height;
  16.  
  17. // current viewport height should be scaled by this amount
  18. float scaleheight = windowaspect / targetaspect;
  19.  
  20. // obtain camera component so we can modify its viewport
  21. Camera camera = GetComponent<Camera>();
  22.  
  23. // if scaled height is less than current height, add letterbox
  24. if (scaleheight < 1.0f)
  25. {
  26. Rect rect = camera.rect;
  27.  
  28. rect.width = 1.0f;
  29. rect.height = scaleheight;
  30. rect.x = 0;
  31. rect.y = (1.0f - scaleheight) / 2.0f;
  32.  
  33. camera.rect = rect;
  34. }
  35. else // add pillarbox
  36. {
  37. float scalewidth = 1.0f / scaleheight;
  38.  
  39. Rect rect = camera.rect;
  40.  
  41. rect.width = scalewidth;
  42. rect.height = 1.0f;
  43. rect.x = (1.0f - scalewidth) / 2.0f;
  44. rect.y = 0;
  45.  
  46. camera.rect = rect;
  47. }
  48. }
  49.  
  50. // Update is called once per frame
  51. void Update () {
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement