Advertisement
Guest User

Untitled

a guest
Jan 8th, 2024
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AspectRatioUtility : MonoBehaviour
  6. {
  7. void Start()
  8. {
  9. Adjust();
  10. }
  11.  
  12. public void Adjust()
  13. {
  14. float targetaspect = 16.0f / 9.0f;
  15.  
  16. float windowaspect = (float)Screen.width / (float)Screen.height;
  17.  
  18. float scaleheight = windowaspect / targetaspect;
  19.  
  20. Camera camera = GetComponent<Camera>();
  21.  
  22. if (scaleheight < 1.0f)
  23. {
  24. Rect rect = camera.rect;
  25.  
  26. rect.width = 1.0f;
  27. rect.height = scaleheight;
  28. rect.x = 0;
  29. rect.y = (1.0f - scaleheight) / 2.0f;
  30.  
  31. camera.rect = rect;
  32. }
  33. else
  34. {
  35. float scalewidth = 1.0f / scaleheight;
  36.  
  37. Rect rect = camera.rect;
  38.  
  39. rect.width = scalewidth;
  40. rect.height = 1.0f;
  41. rect.x = (1.0f - scalewidth) / 2.0f;
  42. rect.y = 0;
  43.  
  44. camera.rect = rect;
  45. }
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement