AspectUtility.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. public class AspectUtility : MonoBehaviour {
  3. public float _wantedAspectRatio = 1.5f;
  4. public bool landscapeModeOnly = true;
  5. static public bool _landscapeModeOnly = true;
  6. static float wantedAspectRatio;
  7. public static Camera cam;
  8. public static Camera backgroundCam;
  9. void Awake() {
  10. _landscapeModeOnly = landscapeModeOnly;
  11. cam = Camera.allCameras[0];
  12. wantedAspectRatio = _wantedAspectRatio;
  13. SetCamera();
  14. }
  15. float resetTimer = 0f;
  16. private void Update() {
  17. resetTimer += Time.deltaTime;
  18. if (resetTimer > 5f) {
  19. resetTimer = 0f;
  20. SetCamera();
  21. }
  22. }
  23. public static void SetCamera() {
  24. float currentAspectRatio = 0.0f;
  25. if (Screen.orientation == ScreenOrientation.LandscapeRight ||
  26. Screen.orientation == ScreenOrientation.LandscapeLeft) {
  27. Debug.Log("Landscape detected...");
  28. currentAspectRatio = (float) Screen.width / Screen.height;
  29. } else {
  30. Debug.Log("Portrait detected...?");
  31. if (Screen.height > Screen.width && _landscapeModeOnly) {
  32. currentAspectRatio = (float) Screen.height / Screen.width;
  33. } else {
  34. currentAspectRatio = (float) Screen.width / Screen.height;
  35. }
  36. }
  37. // If the current aspect ratio is already approximately equal to the desired aspect ratio,
  38. // use a full-screen Rect (in case it was set to something else previously)
  39. Debug.Log("currentAspectRatio = " + currentAspectRatio + ", wantedAspectRatio = " + wantedAspectRatio);
  40. if ((int) (currentAspectRatio * 100) / 100.0f == (int) (wantedAspectRatio * 100) / 100.0f) {
  41. cam.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
  42. if (backgroundCam) {
  43. Destroy(backgroundCam.gameObject);
  44. }
  45. return;
  46. }
  47. // Pillarbox
  48. if (currentAspectRatio > wantedAspectRatio) {
  49. float inset = 1.0f - wantedAspectRatio/currentAspectRatio;
  50. cam.rect = new Rect(inset/2, 0.0f, 1.0f-inset, 1.0f);
  51. }
  52. // Letterbox
  53. else {
  54. float inset = 1.0f - currentAspectRatio/wantedAspectRatio;
  55. cam.rect = new Rect(0.0f, inset/2, 1.0f, 1.0f-inset);
  56. }
  57. if (!backgroundCam) {
  58. // Make a new camera behind the normal camera which displays black; otherwise the unused space is undefined
  59. backgroundCam = new GameObject("BackgroundCam", typeof(Camera)).GetComponent<Camera>();
  60. backgroundCam.depth = int.MinValue;
  61. backgroundCam.clearFlags = CameraClearFlags.SolidColor;
  62. backgroundCam.backgroundColor = Color.black;
  63. backgroundCam.cullingMask = 0;
  64. }
  65. }
  66. public static int screenHeight {
  67. get {
  68. return (int) (Screen.height * cam.rect.height);
  69. }
  70. }
  71. public static int screenWidth {
  72. get {
  73. return (int) (Screen.width * cam.rect.width);
  74. }
  75. }
  76. public static int xOffset {
  77. get {
  78. return (int) (Screen.width * cam.rect.x);
  79. }
  80. }
  81. public static int yOffset {
  82. get {
  83. return (int) (Screen.height * cam.rect.y);
  84. }
  85. }
  86. public static Rect screenRect {
  87. get {
  88. return new Rect(cam.rect.x * Screen.width, cam.rect.y * Screen.height, cam.rect.width * Screen.width, cam.rect.height * Screen.height);
  89. }
  90. }
  91. public static Vector3 mousePosition {
  92. get {
  93. Vector3 mousePos = Input.mousePosition;
  94. mousePos.y -= (int) (cam.rect.y * Screen.height);
  95. mousePos.x -= (int) (cam.rect.x * Screen.width);
  96. return mousePos;
  97. }
  98. }
  99. public static Vector2 guiMousePosition {
  100. get {
  101. Vector2 mousePos = Event.current.mousePosition;
  102. mousePos.y = Mathf.Clamp(mousePos.y, cam.rect.y * Screen.height, cam.rect.y * Screen.height + cam.rect.height * Screen.height);
  103. mousePos.x = Mathf.Clamp(mousePos.x, cam.rect.x * Screen.width, cam.rect.x * Screen.width + cam.rect.width * Screen.width);
  104. return mousePos;
  105. }
  106. }
  107. }