Advertisement
Guest User

Polygony Ebaniy

a guest
Jun 6th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. public class DrawPolygon : MonoBehaviour
  2. {
  3. public LineRenderer lineRenderer; //рендерер линий
  4. public GameObject panel;
  5. public InputField dummyInputField;
  6. public List<InputField> inpFields = new List<InputField>();
  7. public List<Vector3> points = new List<Vector3>(); //список вершин многоугольника
  8. public InputField verticesCountInp;
  9. public int verticesCount;
  10. public void DrawOnClick()
  11. {
  12. GetVectorList();
  13. lineRenderer.SetPositions(points.ToArray());
  14. }
  15.  
  16. // Use this for initialization
  17. void Start()
  18. {
  19. inpFields.Clear();
  20. lineRenderer.startWidth = 0.05f;
  21. lineRenderer.endWidth = 0.05f;
  22. }
  23.  
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. int _verticesCount;
  28. try
  29. {
  30. _verticesCount = Int32.Parse(verticesCountInp.text);
  31. }
  32. catch (FormatException)
  33. {
  34. _verticesCount = 0;
  35. }
  36. if (verticesCount != _verticesCount)
  37. {
  38. verticesCount = _verticesCount;
  39. }
  40.  
  41. if (inpFields.Count < verticesCount * 2)
  42. {
  43. for (int i = inpFields.Count; i < verticesCount * 2; i++)
  44. {
  45. inpFields.Add(Instantiate(dummyInputField, panel.transform, false));
  46. }
  47. }
  48. if (inpFields.Count > verticesCount * 2)
  49. {
  50. for (int i = inpFields.Count; i > verticesCount * 2; i -= 1)
  51. {
  52. Destroy(inpFields[i]);
  53. //inpFields.RemoveAt(i);
  54. }
  55. }
  56.  
  57.  
  58. }
  59. public void GetVectorList()
  60. {
  61. for (int i = 0; i <= inpFields.Count; i += 2)
  62. {
  63. try
  64. {
  65. points[i / 2].Set(Single.Parse(inpFields[i].text, CultureInfo.InvariantCulture), Single.Parse(inpFields[i++].text, CultureInfo.InvariantCulture), 0);
  66. }
  67. catch (FormatException)
  68. {
  69. points[i / 2].Set(0, 0, 0);
  70. Debug.LogError("WrongInput at " + points[i / 2] + "vertice.");
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement