Advertisement
Guest User

Rect Placer

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. public static Rect getRectAtLoc(Vector2 menuSize, HorizontalLoc hori, VerticalLoc vert, bool usePadding, float padding = 0f)
  2. {
  3. var outputRect = new Rect();
  4. int width = (int) menuSize.x;
  5. int height = (int) menuSize.y;
  6.  
  7. int xpos;
  8. int ypos;
  9.  
  10. switch (hori)
  11. {
  12. case HorizontalLoc.Left:
  13. xpos = 0;
  14. if (usePadding)
  15. {
  16. xpos += (int) padding;
  17. }
  18. break;
  19. case HorizontalLoc.LeftMid:
  20. xpos = (((Screen.width / 2) / 2) - (width / 2));
  21. break;
  22. case HorizontalLoc.Center:
  23. xpos = ((Screen.width / 2) - (width / 2));
  24. break;
  25. case HorizontalLoc.RightMid:
  26. xpos = (((Screen.width / 2) + (Screen.width / 4)) - (width / 2));
  27. break;
  28. case HorizontalLoc.Right:
  29. xpos = Screen.width - width;
  30. if (usePadding)
  31. {
  32. xpos -= (int) padding;
  33. }
  34. break;
  35. default:
  36. goto case HorizontalLoc.Center;
  37. }
  38.  
  39. switch (vert)
  40. {
  41. case VerticalLoc.Top:
  42. ypos = 0;
  43. if (usePadding)
  44. {
  45. ypos += (int) padding;
  46. }
  47. break;
  48. case VerticalLoc.TopMid:
  49. ypos = ((Screen.height / 2) / 2) - (height / 2);
  50. break;
  51. case VerticalLoc.Center:
  52. ypos = ((Screen.height / 2) - (height / 2));
  53. break;
  54. case VerticalLoc.BottomMid:
  55. ypos = ((Screen.height / 2) + (Screen.height / 4) - (height / 2));
  56. break;
  57. case VerticalLoc.Bottom:
  58. ypos = Screen.height - height;
  59. if (usePadding)
  60. {
  61. ypos -= (int) padding;
  62. }
  63. break;
  64. default:
  65. goto case VerticalLoc.Center;
  66. }
  67.  
  68.  
  69. if (ypos + height > Screen.height)
  70. {
  71. int difference = Screen.height - (ypos + height);
  72. ypos -= difference;
  73.  
  74. if (usePadding)
  75. {
  76. ypos -= (int) padding;
  77. }
  78. }
  79. else if (ypos < 0)
  80. {
  81. ypos = 0;
  82. if (usePadding)
  83. {
  84. ypos += (int) padding;
  85. }
  86. }
  87. else if (xpos + width > Screen.width)
  88. {
  89. int difference = Screen.width - (xpos + width);
  90. ypos -= difference;
  91.  
  92. if (usePadding)
  93. {
  94. xpos -= (int) padding;
  95. }
  96. }
  97. else if (xpos < 0)
  98. {
  99. xpos = 0;
  100.  
  101. if (usePadding)
  102. {
  103. xpos += (int) padding;
  104. }
  105. }
  106.  
  107. outputRect = new Rect(xpos, ypos, width, height);
  108.  
  109. return outputRect;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement