BattleFieldMaker.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Runtime.CompilerServices;
  2. using UnityEngine;
  3. public class MapMakerMain : MonoBehaviour
  4. {
  5. // Terrain width and height must be a power of two (e.g., 64, 128, 256)
  6. // for a valid heightmap resolution (power of two + 1).
  7. [SerializeField] private int mapWidth = 128;
  8. [SerializeField] private int mapHeight = 128;
  9. [SerializeField] private BFMTerrainType initialTerrainType = BFMTerrainType.Plain;
  10. [SerializeField][Range(0.1f, 5.0f)] private float hilliness = 1.0f;
  11. [SerializeField][Range(1f, 20f)] private float noiseScale = 10f;
  12. [SerializeField][Range(10f, 200f)] private float terrainMaxHeight = 20f;
  13. private BFMTerrainGenerator terrainGenerator;
  14. private NavMeshGenerator navMeshGenerator;
  15. private Terrain currentTerrain;
  16. void Start()
  17. {
  18. terrainGenerator = gameObject.AddComponent<BFMTerrainGenerator>();
  19. navMeshGenerator = gameObject.AddComponent<NavMeshGenerator>();
  20. // Initialize the map with specified dimensions
  21. InitializeComponents();
  22. GenerateMap(mapWidth, mapHeight);
  23. }
  24. #if UNITY_EDITOR
  25. private bool m_DelayedUpdateScheduled = false;
  26. private void OnValidate()
  27. {
  28. if (m_DelayedUpdateScheduled) return;
  29. UnityEditor.EditorApplication.delayCall += () =>
  30. {
  31. m_DelayedUpdateScheduled = false;
  32. // OnValidate can be called on prefabs, or when the component is not fully initialized.
  33. // We only want to run this on scene objects that are not being destroyed.
  34. if (this == null || !gameObject.scene.IsValid())
  35. {
  36. return;
  37. }
  38. InitializeComponents();
  39. GenerateMap(mapWidth, mapHeight);
  40. };
  41. m_DelayedUpdateScheduled = true;
  42. }
  43. #endif
  44. private void InitializeComponents()
  45. {
  46. // Use GetComponent first to avoid adding multiple components if they already exist
  47. if (terrainGenerator == null)
  48. {
  49. terrainGenerator = GetComponent<BFMTerrainGenerator>();
  50. if (terrainGenerator == null)
  51. {
  52. terrainGenerator = gameObject.AddComponent<BFMTerrainGenerator>();
  53. }
  54. navMeshGenerator = GetComponent<NavMeshGenerator>();
  55. if (navMeshGenerator == null)
  56. {
  57. navMeshGenerator = gameObject.AddComponent<NavMeshGenerator>();
  58. }
  59. }
  60. }
  61. private void GenerateMap(int width, int height)
  62. {
  63. // Cleanup old terrain if it exists
  64. if (currentTerrain != null)
  65. {
  66. if (Application.isPlaying)
  67. {
  68. Destroy(currentTerrain.gameObject);
  69. }
  70. else
  71. {
  72. // Use DestroyImmediate when in the editor and not in play mode.
  73. DestroyImmediate(currentTerrain.gameObject);
  74. }
  75. }
  76. // Configure TerrainGenerator from MapMakerMain settings
  77. terrainGenerator.terrainType = initialTerrainType;
  78. terrainGenerator.hilliness = hilliness;
  79. terrainGenerator.noiseScale = noiseScale;
  80. terrainGenerator.terrainMaxHeight = terrainMaxHeight;
  81. // Generate terrain and NavMesh based on specified dimensions
  82. // Adjust the arguments below to match the correct overload of GenerateTerrain
  83. currentTerrain = terrainGenerator.GenerateTerrain(width, height);
  84. if (currentTerrain == null)
  85. {
  86. Debug.LogError("Terrain generation failed. Aborting map generation.");
  87. return;
  88. }
  89. navMeshGenerator.terrain = currentTerrain;
  90. navMeshGenerator.GenerateNavMesh();
  91. }
  92. }