Advertisement
jamieTheCoder

GridManager

Dec 14th, 2022 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using MoreMountains.Tools;
  4. using UnityEngine;
  5.  
  6. // Only import UnityEditor if we're in the editor.
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. public class GridManager : Singleton<GridManager> {
  11.     [Tooltip ("width is the x axis")]
  12.     [HideInInspector]
  13.     public int width;
  14.     [Tooltip ("Length is the z axis")]
  15.     [HideInInspector] public int length;
  16.     [Tooltip ("Buffer is the amount of space on each edge")]
  17.     [HideInInspector] public int buffer;
  18.     [SerializeField] private Material baseMat,
  19.     offsetMat,
  20.     startMat,
  21.     endMat;
  22.     [SerializeField] private Tile tilePrefab;
  23.     [SerializeField] List<Tile> tiles;
  24.     private float tileSizeY;
  25.     [Header ("Components")]
  26.     [SerializeField] BoxCollider wholeGridCollider;
  27.  
  28.     // Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
  29.     private void Start () {
  30.         GenerateGrid ();
  31.     }
  32.     public void GenerateGrid () {
  33.         if (this.transform.childCount != 0) {
  34.             DestroyPreviousChildren ();
  35.         }
  36.         for (int x = 0; x < width; x++) {
  37.             for (int z = 0; z < length; z++) {
  38.                 float actualX = transform.position.x + x;
  39.                 float actualZ = transform.position.z + z;
  40.                 var spawnedTile = Instantiate (tilePrefab, new Vector3 (actualX, 0, actualZ), Quaternion.identity, transform);
  41.                 spawnedTile.name = $"Tile {x} {z}";
  42.                 tileSizeY = spawnedTile.gameObject.transform.localScale.y;
  43.                 tiles.Add (spawnedTile);
  44.                 var isOffset = (x % 2 == 0 && z % 2 != 0) || (x % 2 != 0 && z % 2 == 0);
  45.                 spawnedTile.Init (isOffset, baseMat, offsetMat, startMat, endMat);
  46.             }
  47.         }
  48.         updatePathSpawner ();
  49.         updateBoxColliderBounds ();
  50.     }
  51.     public void DestroyPreviousChildren () {
  52.         tiles.Clear ();
  53.         for (int i = this.transform.childCount; i > 0; --i) {
  54.             DestroyImmediate (this.transform.GetChild (0).gameObject);
  55.         }
  56.     }
  57.     private void updatePathSpawner () {
  58.         PathSpawner.Instance.setPathValues (width, length, buffer, tiles);
  59.         PathSpawner.Instance.ChooseStartPoint ();
  60.         PathSpawner.Instance.ChooseEndPoint ();
  61.     }
  62.     void updateBoxColliderBounds () {
  63.         wholeGridCollider.size = new Vector3 (width, tileSizeY, length);
  64.         wholeGridCollider.center = new Vector3 (width / 2, tileSizeY / 2, length / 2);
  65.     }
  66. }
  67. // Again, class will be declared only if we're in the editor.
  68. #if UNITY_EDITOR
  69. [CustomEditor (typeof (GridManager))]
  70. public class GridManagerEditor : Editor {
  71.     private SerializedProperty length, width, buffer;
  72.     private void OnEnable () {
  73.         length = serializedObject.FindProperty ("length");
  74.         width = serializedObject.FindProperty ("width");
  75.         buffer = serializedObject.FindProperty ("buffer");
  76.     }
  77.     public override void OnInspectorGUI () {
  78.         base.OnInspectorGUI ();
  79.  
  80.         GridManager gridManager = (GridManager) target;
  81.  
  82.         // Update the serializedObject to match the internal state if required.
  83.         serializedObject.Update ();
  84.         EditorGUILayout.BeginHorizontal ();
  85.         EditorGUI.BeginChangeCheck ();
  86.         EditorGUILayout.PropertyField (width);
  87.         EditorGUILayout.PropertyField (length);
  88.         EditorGUILayout.EndHorizontal ();
  89.         EditorGUILayout.PropertyField (buffer);
  90.         if (EditorGUI.EndChangeCheck ()) {
  91.             gridManager.GenerateGrid ();
  92.         }
  93.  
  94.         // Apply changes made before this call.
  95.         serializedObject.ApplyModifiedProperties ();
  96.         //button code
  97.         EditorGUILayout.BeginHorizontal ();
  98.         if (GUILayout.Button ("Generate Grid")) {
  99.             gridManager.GenerateGrid ();
  100.         }
  101.         if (GUILayout.Button ("Destroy Tiles")) {
  102.             gridManager.DestroyPreviousChildren ();
  103.         }
  104.         EditorGUILayout.EndHorizontal ();
  105.     }
  106. }
  107. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement