Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using MoreMountains.Tools;
- using UnityEngine;
- // Only import UnityEditor if we're in the editor.
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- public class GridManager : Singleton<GridManager> {
- [Tooltip ("width is the x axis")]
- [HideInInspector]
- public int width;
- [Tooltip ("Length is the z axis")]
- [HideInInspector] public int length;
- [Tooltip ("Buffer is the amount of space on each edge")]
- [HideInInspector] public int buffer;
- [SerializeField] private Material baseMat,
- offsetMat,
- startMat,
- endMat;
- [SerializeField] private Tile tilePrefab;
- [SerializeField] List<Tile> tiles;
- private float tileSizeY;
- [Header ("Components")]
- [SerializeField] BoxCollider wholeGridCollider;
- // Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
- private void Start () {
- GenerateGrid ();
- }
- public void GenerateGrid () {
- if (this.transform.childCount != 0) {
- DestroyPreviousChildren ();
- }
- for (int x = 0; x < width; x++) {
- for (int z = 0; z < length; z++) {
- float actualX = transform.position.x + x;
- float actualZ = transform.position.z + z;
- var spawnedTile = Instantiate (tilePrefab, new Vector3 (actualX, 0, actualZ), Quaternion.identity, transform);
- spawnedTile.name = $"Tile {x} {z}";
- tileSizeY = spawnedTile.gameObject.transform.localScale.y;
- tiles.Add (spawnedTile);
- var isOffset = (x % 2 == 0 && z % 2 != 0) || (x % 2 != 0 && z % 2 == 0);
- spawnedTile.Init (isOffset, baseMat, offsetMat, startMat, endMat);
- }
- }
- updatePathSpawner ();
- updateBoxColliderBounds ();
- }
- public void DestroyPreviousChildren () {
- tiles.Clear ();
- for (int i = this.transform.childCount; i > 0; --i) {
- DestroyImmediate (this.transform.GetChild (0).gameObject);
- }
- }
- private void updatePathSpawner () {
- PathSpawner.Instance.setPathValues (width, length, buffer, tiles);
- PathSpawner.Instance.ChooseStartPoint ();
- PathSpawner.Instance.ChooseEndPoint ();
- }
- void updateBoxColliderBounds () {
- wholeGridCollider.size = new Vector3 (width, tileSizeY, length);
- wholeGridCollider.center = new Vector3 (width / 2, tileSizeY / 2, length / 2);
- }
- }
- // Again, class will be declared only if we're in the editor.
- #if UNITY_EDITOR
- [CustomEditor (typeof (GridManager))]
- public class GridManagerEditor : Editor {
- private SerializedProperty length, width, buffer;
- private void OnEnable () {
- length = serializedObject.FindProperty ("length");
- width = serializedObject.FindProperty ("width");
- buffer = serializedObject.FindProperty ("buffer");
- }
- public override void OnInspectorGUI () {
- base.OnInspectorGUI ();
- GridManager gridManager = (GridManager) target;
- // Update the serializedObject to match the internal state if required.
- serializedObject.Update ();
- EditorGUILayout.BeginHorizontal ();
- EditorGUI.BeginChangeCheck ();
- EditorGUILayout.PropertyField (width);
- EditorGUILayout.PropertyField (length);
- EditorGUILayout.EndHorizontal ();
- EditorGUILayout.PropertyField (buffer);
- if (EditorGUI.EndChangeCheck ()) {
- gridManager.GenerateGrid ();
- }
- // Apply changes made before this call.
- serializedObject.ApplyModifiedProperties ();
- //button code
- EditorGUILayout.BeginHorizontal ();
- if (GUILayout.Button ("Generate Grid")) {
- gridManager.GenerateGrid ();
- }
- if (GUILayout.Button ("Destroy Tiles")) {
- gridManager.DestroyPreviousChildren ();
- }
- EditorGUILayout.EndHorizontal ();
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement