dnnkeeper

Unity Selection Box

Sep 24th, 2020
879
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SelectionBoxTest : MonoBehaviour
  2. {
  3.     public Transform startTransform;
  4.     public Transform endTransform;
  5.  
  6.     public RectTransform selectionBox;
  7.  
  8.     // Update is called once per frame
  9.     void Update()
  10.     {
  11.         Vector3 startPos = Camera.main.WorldToScreenPoint(startTransform.position);
  12.  
  13.         Vector3 curPos = Camera.main.WorldToScreenPoint(endTransform.position);
  14.  
  15.         float width = curPos.x - startPos.x;
  16.         float height = curPos.y - startPos.y;
  17.  
  18.         Vector3 minScreenPos = new Vector3(Mathf.Min(curPos.x, startPos.x), Mathf.Min(curPos.y, startPos.y));
  19.         Vector3 maxScreenPos = new Vector3(Mathf.Max(curPos.x, startPos.x), Mathf.Max(curPos.y, startPos.y));
  20.  
  21.         selectionBox.pivot = Vector2.zero;
  22.         selectionBox.anchoredPosition = minScreenPos;
  23.         selectionBox.sizeDelta = new Vector2(Mathf.Abs(width), Mathf.Abs(height));
  24.     }
  25. }
RAW Paste Data