You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EnvironmentGenerator.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class EnvironmentGenerator : MonoBehaviour
  6. {
  7. [Tooltip("Width of the whole ground plane")]
  8. public float width = 1;
  9. [Tooltip("Height of the whole ground plane")]
  10. public float height = 1;
  11. [Tooltip("Amount of planes drawn horizontally in the whole plane area.")]
  12. [Range(1, 20)]
  13. public uint amountOfHorizontalElements = 5;
  14. [Tooltip("Amount of planes drawn vertically in the whole plane area.")]
  15. [Range(1, 20)]
  16. public uint amountOfVerticalElements = 5;
  17. [Tooltip("The material the ground uses")]
  18. public Material groundMaterial;
  19. [Tooltip("Only for testing.")]
  20. public bool testDestroy = false;
  21. public GameObject[,] CurrentEnvironment { get; private set; }
  22. private void Start()
  23. {
  24. CreateNewEnvironment();
  25. }
  26. private void Update()
  27. {
  28. TestEnvironmentCreation();
  29. }
  30. /// <summary>
  31. /// Only for testing, can be deleted if not needed anymore
  32. /// </summary>
  33. private void TestEnvironmentCreation()
  34. {
  35. if (testDestroy)
  36. {
  37. UpdateEnvironment();
  38. testDestroy = !testDestroy;
  39. }
  40. }
  41. /// <summary>
  42. /// Creates a grid of planes
  43. /// </summary>
  44. public void CreateNewEnvironment()
  45. {
  46. CurrentEnvironment = new GameObject[amountOfHorizontalElements, amountOfVerticalElements];
  47. float widthSingleEleme = width / amountOfHorizontalElements; //width of a single quad
  48. float heightSingleElem = height / amountOfVerticalElements; //height of a single quad
  49. // calculate the left-upper-point of the whole environment plane to use it as a startpoint for creating the grid
  50. Vector3 leftUpperPoint = transform.position + (Vector3.left * (width / 2) + (Vector3.forward * (height / 2)));
  51. Vector3 currentStartPoint = leftUpperPoint + new Vector3(widthSingleEleme / 2, 0, -heightSingleElem / 2);
  52. //First draw all horizontal planes than do the next line in the grid
  53. for (int idxX = 0; idxX < amountOfVerticalElements; idxX++)
  54. {
  55. CreateHorizontalPlaneLine(currentStartPoint, widthSingleEleme, heightSingleElem, idxX);
  56. currentStartPoint += Vector3.back * heightSingleElem;
  57. }
  58. }
  59. // Create a horizontal line of Planes
  60. private void CreateHorizontalPlaneLine(Vector3 startPoint, float widthPlane = 1, float heightPlane = 1, int idxX = 1)
  61. {
  62. Vector3 currentStartPoint = startPoint;
  63. for (int idxY = 0; idxY < amountOfHorizontalElements; idxY++)
  64. {
  65. CreatePlane(widthPlane, heightPlane, idxX, currentStartPoint, idxY);
  66. currentStartPoint += Vector3.right * widthPlane;
  67. }
  68. }
  69. // Create a single Plane
  70. private void CreatePlane(float widthPlane, float heightPlane, int idxX, Vector3 currentStartPoint, int idxY)
  71. {
  72. GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Quad);
  73. plane.transform.localEulerAngles = new Vector3(90, 0, 0);
  74. plane.transform.position = currentStartPoint;
  75. plane.transform.localScale = new Vector3(widthPlane, heightPlane, 1);
  76. plane.transform.SetParent(this.transform);
  77. plane.GetComponent<Renderer>().material = groundMaterial;
  78. CurrentEnvironment[idxX, idxY] = plane;
  79. }
  80. /// <summary>
  81. /// Destroy all Planes and set the environment array back to null
  82. /// </summary>
  83. public void DeleteCurrentEnvironment()
  84. {
  85. foreach (Transform child in transform)
  86. {
  87. if (child != this)
  88. {
  89. Destroy(child.gameObject);
  90. }
  91. }
  92. CurrentEnvironment = null;
  93. }
  94. /// <summary>
  95. /// Deletes the current environment if there is any and creates a new one
  96. /// </summary>
  97. public void UpdateEnvironment()
  98. {
  99. DeleteCurrentEnvironment();
  100. CreateNewEnvironment();
  101. }
  102. }