DrawArrow.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. public class DrawArrow : DrawShape
  5. {
  6. private RectTransform rectTransform_;
  7. private readonly List<Vector2> _vertices = new List<Vector2>(2);
  8. private void Awake()
  9. {
  10. rectTransform_ = GetComponent<RectTransform>();
  11. }
  12. public static Rect RectTransformToScreenSpace(RectTransform transform)
  13. {
  14. Vector2 size = Vector2.Scale(transform.rect.size, transform.lossyScale);
  15. return new Rect((Vector2)transform.position - (size * 0.5f), size);
  16. }
  17. public override void StartDraw(Vector2 position)
  18. {
  19. _vertices.Add(position);
  20. _vertices.Add(position);
  21. rectTransform_.pivot = new Vector2(0, 0.5f);
  22. rectTransform_.position = position;
  23. Vector2 temp = rectTransform_.anchoredPosition; // без этой штуки не работает!
  24. }
  25. public override void StopDraw(Vector2 position)
  26. {
  27. }
  28. public override void UpdateShape(Vector2 newPosition)
  29. {
  30. _vertices[_vertices.Count - 1] = newPosition;
  31. var length = Vector2.Distance(_vertices[0], _vertices[1]);
  32. rectTransform_.sizeDelta = new Vector2(length, 20);
  33. float angle = Mathf.Atan2(_vertices[1].y - _vertices[0].y, _vertices[1].x - _vertices[0].x) * Mathf.Rad2Deg;
  34. transform.eulerAngles = new Vector3(0, 0, angle);
  35. }
  36. public override void Clear()
  37. {
  38. }
  39. }