DrawText.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class DrawText : DrawShape
  6. {
  7. private RectTransform rectTransform_;
  8. private Vector3[] lineCorners_;
  9. // Start and end vertices (in absolute coordinates)
  10. private readonly List<Vector2> _vertices = new List<Vector2>(2);
  11. private void Awake()
  12. {
  13. rectTransform_ = GetComponent<RectTransform>();
  14. Debug.Log("DrawText awake");
  15. lineCorners_ = new Vector3[4];
  16. // 2 вершины будут рассчитываться, другие 2 получаться
  17. lineCorners_[1] = new Vector3();
  18. lineCorners_[3] = new Vector3();
  19. }
  20. public override void StartDraw(Vector2 position)
  21. {
  22. _vertices.Add(position);
  23. _vertices.Add(position);
  24. }
  25. public override void StopDraw(Vector2 position)
  26. {
  27. Image image = GetComponent<Image>();
  28. image.enabled = false;
  29. }
  30. public override void UpdateShape(Vector2 newPosition)
  31. {
  32. _vertices[_vertices.Count - 1] = newPosition;
  33. rectTransform_.sizeDelta = new Vector2(Mathf.Abs(_vertices[0].x - _vertices[1].x), Mathf.Abs(_vertices[0].y - _vertices[1].y));
  34. rectTransform_.transform.position = new Vector2(Mathf.Min(_vertices[0].x, _vertices[1].x), Mathf.Min(_vertices[0].y, _vertices[1].y));
  35. }
  36. public override void Clear()
  37. {
  38. }
  39. }