Guest User

Untitled

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