AspectUtility.cs 4.0 KB

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