Advertisement
Guest User

Untitled

a guest
Feb 1st, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SC_SelectionManager : MonoBehaviour
  6. {
  7. public Transform myCube;
  8. public Texture topLeftBorder;
  9. public Texture bottomLeftBorder;
  10. public Texture topRightBorder;
  11. public Texture bottomRightBorder;
  12. public float duration;
  13.  
  14. private Vector3 rectangleStartPosition = Vector3.zero;
  15. private Rect rectangleReference;
  16. private float t = 0;
  17.  
  18. Texture2D _borderTexture;
  19. Texture2D borderTexture
  20. {
  21. get
  22. {
  23. if (_borderTexture == null)
  24. {
  25. _borderTexture = new Texture2D(1, 1);
  26. _borderTexture.SetPixel(0, 0, Color.white);
  27. _borderTexture.Apply();
  28. }
  29.  
  30. return _borderTexture;
  31. }
  32. }
  33.  
  34. bool selectionStarted = false;
  35. Vector3 mousePosition1;
  36.  
  37. public static List<SC_Selectable> selectables = new List<SC_Selectable>();
  38. List<int> selectedObjects = new List<int>();
  39.  
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. // Begin selection
  44. if (Input.GetMouseButtonDown(0))
  45. {
  46. selectionStarted = true;
  47. mousePosition1 = Input.mousePosition;
  48. }
  49. // End selection
  50. selectionStarted = false;
  51. rectangleStartPosition = rectangleReference.center; // Remember the current position. Do that elsewhere if you need to start the movement from a different position.
  52. t = 0; // I assume that you want to reset the movement when you release the mouse
  53.  
  54. if (selectionStarted)
  55. {
  56. // Detect which Objects are inside selection rectangle
  57. Camera camera = Camera.main;
  58. selectedObjects.Clear();
  59. for (int i = 0; i < selectables.Count; i++)
  60. {
  61. Bounds viewportBounds = GetViewportBounds(camera, mousePosition1, Input.mousePosition);
  62. if (viewportBounds.Contains(camera.WorldToViewportPoint(selectables[i].transform.position)))
  63. {
  64. selectedObjects.Add(i);
  65. }
  66. }
  67. }
  68. }
  69.  
  70. void OnGUI()
  71. {
  72. if (selectionStarted)
  73. {
  74. Rect rect = GetScreenRect(mousePosition1, Input.mousePosition);
  75. DrawScreenRectBorder(rect, 2, Color.cyan);
  76.  
  77. rectangleReference = rect;
  78. }
  79. else
  80. {
  81. DrawScreenRectBorder(rectangleReference, 2, Color.cyan);
  82. var cubeworld = WorldToGuiPoint(myCube.position);
  83. t += Time.deltaTime / duration;
  84. rectangleReference.center = Vector3.Lerp(rectangleStartPosition, cubeworld, t);
  85. //rectangleReference.center = cubeworld;
  86. }
  87.  
  88. // Draw selection edges
  89. if (selectedObjects.Count > 0)
  90. {
  91. Camera camera = Camera.main;
  92. for (int i = 0; i < selectedObjects.Count; i++)
  93. {
  94. DrawSelectionIndicator(camera, selectables[selectedObjects[i]].GetObjectBounds());
  95. }
  96. }
  97. }
  98.  
  99. public Vector3 WorldToGuiPoint(Vector3 position)
  100. {
  101. var guiPosition = Camera.main.WorldToScreenPoint(position);
  102. guiPosition.y = Screen.height - guiPosition.y;
  103.  
  104. return guiPosition;
  105. }
  106.  
  107. void DrawScreenRectBorder(Rect rect, float thickness, Color color)
  108. {
  109. // Top
  110. DrawBorderRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
  111. // Left
  112. DrawBorderRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
  113. // Right
  114. DrawBorderRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
  115. // Bottom
  116. DrawBorderRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
  117. }
  118.  
  119. void DrawBorderRect(Rect rect, Color color)
  120. {
  121. GUI.color = color;
  122. GUI.DrawTexture(rect, borderTexture);
  123. GUI.color = Color.white;
  124. }
  125.  
  126. Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
  127. {
  128. // Move origin from bottom left to top left
  129. screenPosition1.y = Screen.height - screenPosition1.y;
  130. screenPosition2.y = Screen.height - screenPosition2.y;
  131. // Calculate corners
  132. var topLeft = Vector3.Min(screenPosition1, screenPosition2);
  133. var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
  134. // Create Rect
  135. return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
  136. }
  137.  
  138. Bounds GetViewportBounds(Camera camera, Vector3 screenPosition1, Vector3 screenPosition2)
  139. {
  140. Vector3 v1 = camera.ScreenToViewportPoint(screenPosition1);
  141. Vector3 v2 = camera.ScreenToViewportPoint(screenPosition2);
  142. Vector3 min = Vector3.Min(v1, v2);
  143. Vector3 max = Vector3.Max(v1, v2);
  144. min.z = camera.nearClipPlane;
  145. max.z = camera.farClipPlane;
  146.  
  147. Bounds bounds = new Bounds();
  148. bounds.SetMinMax(min, max);
  149. return bounds;
  150. }
  151.  
  152. void DrawSelectionIndicator(Camera camera, Bounds bounds)
  153. {
  154. Vector3 boundPoint1 = bounds.min;
  155. Vector3 boundPoint2 = bounds.max;
  156. Vector3 boundPoint3 = new Vector3(boundPoint1.x, boundPoint1.y, boundPoint2.z);
  157. Vector3 boundPoint4 = new Vector3(boundPoint1.x, boundPoint2.y, boundPoint1.z);
  158. Vector3 boundPoint5 = new Vector3(boundPoint2.x, boundPoint1.y, boundPoint1.z);
  159. Vector3 boundPoint6 = new Vector3(boundPoint1.x, boundPoint2.y, boundPoint2.z);
  160. Vector3 boundPoint7 = new Vector3(boundPoint2.x, boundPoint1.y, boundPoint2.z);
  161. Vector3 boundPoint8 = new Vector3(boundPoint2.x, boundPoint2.y, boundPoint1.z);
  162.  
  163. Vector2[] screenPoints = new Vector2[8];
  164. screenPoints[0] = camera.WorldToScreenPoint(boundPoint1);
  165. screenPoints[1] = camera.WorldToScreenPoint(boundPoint2);
  166. screenPoints[2] = camera.WorldToScreenPoint(boundPoint3);
  167. screenPoints[3] = camera.WorldToScreenPoint(boundPoint4);
  168. screenPoints[4] = camera.WorldToScreenPoint(boundPoint5);
  169. screenPoints[5] = camera.WorldToScreenPoint(boundPoint6);
  170. screenPoints[6] = camera.WorldToScreenPoint(boundPoint7);
  171. screenPoints[7] = camera.WorldToScreenPoint(boundPoint8);
  172.  
  173. Vector2 topLeftPosition = Vector2.zero;
  174. Vector2 topRightPosition = Vector2.zero;
  175. Vector2 bottomLeftPosition = Vector2.zero;
  176. Vector2 bottomRightPosition = Vector2.zero;
  177.  
  178. for (int a = 0; a < screenPoints.Length; a++)
  179. {
  180. //Top Left
  181. if (topLeftPosition.x == 0 || topLeftPosition.x > screenPoints[a].x)
  182. {
  183. topLeftPosition.x = screenPoints[a].x;
  184. }
  185. if (topLeftPosition.y == 0 || topLeftPosition.y > Screen.height - screenPoints[a].y)
  186. {
  187. topLeftPosition.y = Screen.height - screenPoints[a].y;
  188. }
  189. //Top Right
  190. if (topRightPosition.x == 0 || topRightPosition.x < screenPoints[a].x)
  191. {
  192. topRightPosition.x = screenPoints[a].x;
  193. }
  194. if (topRightPosition.y == 0 || topRightPosition.y > Screen.height - screenPoints[a].y)
  195. {
  196. topRightPosition.y = Screen.height - screenPoints[a].y;
  197. }
  198. //Bottom Left
  199. if (bottomLeftPosition.x == 0 || bottomLeftPosition.x > screenPoints[a].x)
  200. {
  201. bottomLeftPosition.x = screenPoints[a].x;
  202. }
  203. if (bottomLeftPosition.y == 0 || bottomLeftPosition.y < Screen.height - screenPoints[a].y)
  204. {
  205. bottomLeftPosition.y = Screen.height - screenPoints[a].y;
  206. }
  207. //Bottom Right
  208. if (bottomRightPosition.x == 0 || bottomRightPosition.x < screenPoints[a].x)
  209. {
  210. bottomRightPosition.x = screenPoints[a].x;
  211. }
  212. if (bottomRightPosition.y == 0 || bottomRightPosition.y < Screen.height - screenPoints[a].y)
  213. {
  214. bottomRightPosition.y = Screen.height - screenPoints[a].y;
  215. }
  216. }
  217.  
  218. if (topLeftBorder != null && topRightBorder != null &&
  219. bottomLeftBorder != null && bottomRightBorder != null)
  220. {
  221. /*GUI.DrawTexture(new Rect(topLeftPosition.x - 16, topLeftPosition.y - 16, 16, 16), topLeftBorder);
  222. GUI.DrawTexture(new Rect(topRightPosition.x, topRightPosition.y - 16, 16, 16), topRightBorder);
  223. GUI.DrawTexture(new Rect(bottomLeftPosition.x - 16, bottomLeftPosition.y, 16, 16), bottomLeftBorder);
  224. GUI.DrawTexture(new Rect(bottomRightPosition.x, bottomRightPosition.y, 16, 16), bottomRightBorder);
  225. */
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement