using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(MapComponent))]
public class MapComponentEditor : Editor
{
private MapComponent lastSelected;
private float cellSize;
private int height;
private int width;
private bool isGenerated;
private bool isPlaced;
void OnTriggerDownMove()
{
isPlaced = true;
}
/**
* On inspector's GUI update
* @param - <Object> target (inherited) - selected object
* @return - NONE
*/
public override void OnInspectorGUI()
{
// Get info about selected object
isGenerated = ((MapComponent)target).isGenerated;
isPlaced = (((MapComponent)target).gameObject.scene.buildIndex != -1);
// If new object selected - update displayed parameters
if (lastSelected != (MapComponent)target) {
lastSelected = (MapComponent)target;
cellSize = ((MapComponent)target).cellSize;
height = ((MapComponent)target).height;
width = ((MapComponent)target).width;
}
// If object is PREFAB - reset private defaults
if (!isPlaced) {
((MapComponent)target).isGenerated = false;
}
// Get user input
// Received = DisplayValue(Name, oldValue)
cellSize = EditorGUILayout.FloatField("Between cells", cellSize);
width = EditorGUILayout.IntField("Width", width);
height = EditorGUILayout.IntField("Height", height);
((MapComponent)target).tileBasicPrefab = EditorGUILayout.ObjectField("Node Prefab", ((MapComponent)target).tileBasicPrefab, typeof(GameObject), false) as GameObject;
((MapComponent)target).tileRoadPrefab = EditorGUILayout.ObjectField("Road Prefab", ((MapComponent)target).tileRoadPrefab, typeof(GameObject), false) as GameObject;
// Call generation onButton press or onSpawning
bool isJustSpawned = (isPlaced && !isGenerated);
if (GUILayout.Button("Generate") || isJustSpawned) {
Undo.IncrementCurrentGroup();
Undo.RegisterFullObjectHierarchyUndo(((MapComponent)target).gameObject, "Map generated");
((MapComponent)target).Generate(height, width, cellSize);
EditorUtility.SetDirty(((MapComponent)target).gameObject);
}
if (GUILayout.Button("Repair") ) {
Undo.IncrementCurrentGroup();
Undo.RegisterFullObjectHierarchyUndo(((MapComponent)target).gameObject, "Map repaired");
((MapComponent)target).Repair();
EditorUtility.SetDirty(((MapComponent)target).gameObject);
}
}
}