Grid.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// Сетка
  8. /// </summary>
  9. public class Grid : MonoBehaviour
  10. {
  11. //public GameObject grid;
  12. //public float Length = 40;
  13. public int Quantity = 20;
  14. //public bool isOn;
  15. public enum Axis { XY, XZ, YZ };
  16. public Axis axis = Axis.XZ;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. CreateGrid();
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. }
  26. public void CreateGrid()
  27. {
  28. //if (transform.localScale.x > transform.localScale.z) Quantity = Convert.ToInt32(transform.localScale.x);
  29. //if (transform.localScale.z > transform.localScale.x) Quantity = Convert.ToInt32(transform.localScale.z);
  30. for (int i = 0; i < Quantity; i++)
  31. {
  32. for (float j = 0; j < 1; j += 0.1f)
  33. {
  34. var width = 0.01f;
  35. if (j == 0)
  36. width = 0.1f;
  37. if (j == 0.5f) width = 0.05f;
  38. switch (axis)
  39. {
  40. case Axis.YZ:
  41. NewLine(new Vector3(0, 0, i + j), new Vector3(0, 0, i + j), new Vector3(0, Quantity * 2, i + j), "LineY_" + (i + j).ToString("0.#"), width, transform.gameObject);
  42. NewLine(new Vector3(0, i + j, 0), new Vector3(0, i + j, 0), new Vector3(0, i + j, Quantity * 2), "LineZ_" + (i + j).ToString("0.#"), width, transform.gameObject);
  43. break;
  44. case Axis.XZ:
  45. NewLine(new Vector3(0, 0, i + j), new Vector3(0, 0, i + j), new Vector3(Quantity * 2, 0, i + j), "LineX_" + (i + j).ToString("0.#"), width, transform.gameObject);
  46. NewLine(new Vector3(i + j, 0, 0), new Vector3(i + j, 0, 0), new Vector3(i + j, 0, Quantity * 2), "LineZ_" + (i + j).ToString("0.#"), width, transform.gameObject);
  47. break;
  48. case Axis.XY:
  49. NewLine(new Vector3(i + j, 0, 0), new Vector3(i + j, 0, 0), new Vector3(i + j, Quantity * 2, 0), "LineY_" + (i + j).ToString("0.#"), width, transform.gameObject);
  50. NewLine(new Vector3(0, i + j, 0), new Vector3(0, i + j, 0), new Vector3(Quantity * 2, i + j, 0), "LineX_" + (i + j).ToString("0.#"), width, transform.gameObject);
  51. break;
  52. }
  53. }
  54. }
  55. transform.position = new Vector3(0, 0.01f, 0);
  56. }
  57. public static LineRenderer NewLine(Vector3 pos, Vector3 start, Vector3 end, string name_line, float width = 0.1f, GameObject parent = null)
  58. {
  59. var line = Instantiate(Resources.Load("GameObjects/Line", typeof(LineRenderer))) as LineRenderer;
  60. line.transform.position = pos;
  61. line.name = name_line;
  62. line.SetPosition(line.positionCount++, start);
  63. line.SetPosition(line.positionCount++, end);
  64. line.startColor = Color.gray;
  65. line.endColor = Color.gray;
  66. line.material.color = Color.gray;
  67. if(parent != null)line.transform.parent = parent.transform;
  68. //line.name = name_line;
  69. //line.transform.localScale = localScale;
  70. //line.transform.position = position;
  71. line.startWidth = width;
  72. return line;
  73. }
  74. }