Created
December 24, 2014 06:12
-
-
Save cloudRoutine/499c9eb96119a42aebc3 to your computer and use it in GitHub Desktop.
Grid Controller - Unity Editor Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using UnityEngine; | |
| using UnityEditor; | |
| using HexMap; | |
| using ExtensionMethods; | |
| public class GridController : EditorWindow | |
| { | |
| GridGizmo gGizmo ; | |
| public int GridRows = 10 ; | |
| public int GridCols = 10 ; | |
| public int HexSize = 10 ; | |
| private int gridRows_p ; | |
| private int gridCols_p ; | |
| private int hexSize_p ; | |
| public Vector3 GridOrigin ; | |
| public HexType GridStyle = HexType.Point ; | |
| private HexType gridStyle_p ; | |
| public HexOffset GridOffset = HexOffset.Odd ; | |
| private HexOffset gridOffset_p ; | |
| public Color GridColor = Color.red ; | |
| public Material MeshMaterial ; | |
| public Font MapFont; | |
| public bool showCornerMarkers = false ; | |
| public bool showEdgeMarkers = false ; | |
| public bool showHexMarkers = false ; | |
| public bool showHexGrid = false ; | |
| public bool showBoundingBox = false ; | |
| public string MapName = "Map" ; | |
| public string GridName = "Grid" ; | |
| public bool PrefabFill = false ; | |
| public bool PlaceBlocks = false ; | |
| public bool PlaceMeshesInGameworld = false ; | |
| public int count = 0 ; | |
| private GameObject mapHolder; | |
| private GameObject hexmeshes ; | |
| // Add menu item named "My Window" to the Window menu | |
| [MenuItem("Project Tools/Grid Controller")] | |
| static void Init() | |
| { | |
| GridController Controller = (GridController)EditorWindow.GetWindow(typeof(GridController)); | |
| } | |
| #region Grid Controller GUI | |
| void ShowButtons() | |
| { | |
| EditorGUILayout.BeginHorizontal(); | |
| EditorGUILayout.BeginVertical(); | |
| if (GUILayout.Button("Show Grid") && gGizmo != null ) | |
| { | |
| showHexGrid = !showHexGrid; | |
| gGizmo.showHexGrid = showHexGrid; | |
| } | |
| if (GUILayout.Button("Show Hex Markers") && gGizmo != null ) | |
| { | |
| showHexMarkers = !showHexMarkers; | |
| gGizmo.showHexMarkers = showHexMarkers; | |
| } | |
| EditorGUILayout.EndVertical(); | |
| EditorGUILayout.BeginVertical(); | |
| if (GUILayout.Button("Show Corners") && gGizmo != null ) | |
| { | |
| showCornerMarkers = !showCornerMarkers; | |
| gGizmo.showCornerMarkers = showCornerMarkers; | |
| } | |
| if (GUILayout.Button("Show Edges") && gGizmo != null ) | |
| { | |
| showEdgeMarkers = !showEdgeMarkers; | |
| gGizmo.showEdgeMarkers = showEdgeMarkers; | |
| } | |
| EditorGUILayout.EndVertical(); | |
| EditorGUILayout.EndHorizontal(); | |
| if (GUILayout.Button("Show Bounding Box") && gGizmo != null ) | |
| { | |
| showBoundingBox = !showBoundingBox; | |
| gGizmo.showBoundingBox = showBoundingBox; | |
| } | |
| } | |
| void GridComponents() | |
| { | |
| EditorGUILayout.BeginVertical(); | |
| if ( gGizmo != null ) | |
| { | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ( "HexType", GUILayout.Width ( 60 ) ); | |
| gGizmo.GridStyle = ( HexType ) EditorGUILayout.EnumPopup ( gGizmo.GridStyle, GUILayout.MinWidth ( 40 ) ); | |
| GUILayout.Label ( " OffsetType", GUILayout.Width ( 75 ) ); | |
| gGizmo.GridOffset = ( HexOffset ) EditorGUILayout.EnumPopup ( gGizmo.GridOffset, GUILayout.MinWidth ( 40 ) ); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ( "Edge", GUILayout.Width ( 50) ); | |
| gGizmo.EdgeColor = EditorGUILayout.ColorField ( gGizmo.EdgeColor, GUILayout.MinWidth ( 30 ) ); | |
| GridColor = gGizmo.GridColor; | |
| GUILayout.Label ( "Corner", GUILayout.Width ( 75 ) ); | |
| gGizmo.CornerColor = EditorGUILayout.ColorField ( gGizmo.CornerColor, GUILayout.MinWidth ( 30 ) ); | |
| GridColor = gGizmo.GridColor; | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ( "Grid", GUILayout.Width ( 50 ) ); | |
| gGizmo.GridColor = EditorGUILayout.ColorField ( gGizmo.GridColor, GUILayout.MinWidth ( 30 ) ); | |
| GridColor = gGizmo.GridColor; | |
| GUILayout.Label ( "Hex Marker", GUILayout.Width ( 75 ) ); | |
| gGizmo.CenterColor = EditorGUILayout.ColorField ( gGizmo.CenterColor, GUILayout.MinWidth ( 30 ) ); | |
| GridColor = gGizmo.GridColor; | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ( "Bounding Box", GUILayout.Width ( 100 ) ); | |
| gGizmo.BoundingBox = EditorGUILayout.ColorField ( gGizmo.BoundingBox, GUILayout.MinWidth ( 30 ) ); | |
| GridColor = gGizmo.GridColor; | |
| EditorGUILayout.EndHorizontal(); | |
| } | |
| EditorGUILayout.EndVertical(); | |
| } | |
| void GridDimensions() | |
| { | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ( "Cols", GUILayout.Width ( 50 ) ); | |
| //gGizmo.GridCols = EditorGUILayout.IntField ( gGizmo.GridCols, GUILayout.MinWidth ( 45 ) ); | |
| GridCols = EditorGUILayout.IntField ( GridCols, GUILayout.MinWidth ( 45 ) ); | |
| GUILayout.Label ( "Rows", GUILayout.Width ( 50 ) ); | |
| //gGizmo.GridRows = EditorGUILayout.IntField ( gGizmo.GridRows, GUILayout.MinWidth ( 45 ) ); | |
| GridRows = EditorGUILayout.IntField ( GridRows, GUILayout.MinWidth ( 45 ) ); | |
| GUILayout.Label ( "HexSize", GUILayout.Width ( 60 ) ); | |
| //gGizmo.HexSize = EditorGUILayout.IntField ( gGizmo.HexSize, GUILayout.MinWidth ( 45 ) ); | |
| HexSize = EditorGUILayout.IntField ( HexSize, GUILayout.MinWidth ( 45 ) ); | |
| if (gGizmo != null ) | |
| { | |
| gGizmo.GridRows = GridRows; | |
| gGizmo.GridCols = GridCols; | |
| gGizmo.HexSize=HexSize; | |
| Repaint(); | |
| } | |
| EditorGUILayout.EndHorizontal() ; | |
| } | |
| void PrefabMat() | |
| { | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ("Map Font", GUILayout.Width(100) ); | |
| MapFont = (Font) EditorGUILayout.ObjectField ( MapFont, typeof(Font), true ); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ("Mesh Material", GUILayout.Width(100) ); | |
| MeshMaterial = (Material) EditorGUILayout.ObjectField (MeshMaterial , typeof(Material), true); | |
| EditorGUILayout.EndHorizontal(); | |
| } | |
| void NameLabel() | |
| { | |
| EditorGUILayout.BeginVertical(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ("Grid Gizmo", GUILayout.Width(100) ); | |
| gGizmo = (GridGizmo) EditorGUILayout.ObjectField ( gGizmo, typeof(GridGizmo), true ); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ("Grid Name", GUILayout.Width(100) ); | |
| if ( gGizmo != null ) | |
| { | |
| gGizmo.name = EditorGUILayout.TextField(gGizmo.name); | |
| } | |
| else | |
| { | |
| GUILayout.Label ( "No GridGizmo Selected" ); | |
| } | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.BeginHorizontal(); | |
| GUILayout.Label ("Map Name", GUILayout.Width(100) ); | |
| MapName = EditorGUILayout.TextField(MapName); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.EndVertical(); | |
| } | |
| void GenerationButtons() | |
| { | |
| EditorGUILayout.BeginHorizontal(); | |
| if (GUILayout.Button("Create Hex Grid")) | |
| { | |
| CreateGridGizmo(); | |
| } | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.BeginHorizontal(); | |
| if (GUILayout.Button("Generate Mesh Map")) | |
| { | |
| Ext.TryDo(gGizmo,MapName,MeshMaterial,MapFont, GenerateMap.GenerateMapMeshes); | |
| } | |
| EditorGUILayout.EndHorizontal(); | |
| if (GUILayout.Button("Generate Combined Mesh Map")) | |
| { | |
| Ext.TryDo(gGizmo,MapName,MeshMaterial,MapFont, GenerateMap.GenerateCombinedMesh); | |
| } | |
| } | |
| void OnGUI () | |
| { | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.BeginHorizontal(); | |
| if ( gGizmo != null ) | |
| { | |
| gGizmo.transform.position = EditorGUILayout.Vector3Field( "Grid Origin", gGizmo.transform.position ) ; | |
| Repaint(); | |
| } | |
| else | |
| { | |
| GUILayout.Label("No Grid Selected"); | |
| } | |
| EditorGUILayout.BeginVertical(); | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.Space(); | |
| if (GUILayout.Button("Reset Origin") && gGizmo != null ) | |
| { | |
| gGizmo.transform.position = Vector3.zero; | |
| } | |
| EditorGUILayout.EndVertical(); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.Space () ; | |
| ShowButtons () ; | |
| EditorGUILayout.Space () ; | |
| GridComponents () ; | |
| EditorGUILayout.Space () ; | |
| EditorGUILayout.Space () ; | |
| GridDimensions () ; | |
| EditorGUILayout.Space () ; | |
| EditorGUILayout.Space () ; | |
| PrefabMat () ; | |
| EditorGUILayout.Space () ; | |
| EditorGUILayout.Space () ; | |
| NameLabel () ; | |
| EditorGUILayout.Space () ; | |
| GenerationButtons () ; | |
| EditorGUILayout.Space(); | |
| EditorGUILayout.BeginHorizontal(); | |
| if ( gGizmo != null ) | |
| { | |
| GUILayout.Label ("Show Grid During Gameplay?" , GUILayout.Width(180) ); | |
| gGizmo.DrawGizmosDuringGameplay = EditorGUILayout.Toggle( gGizmo.DrawGizmosDuringGameplay ); | |
| } | |
| EditorGUILayout.EndHorizontal(); | |
| SceneView.RepaintAll(); | |
| } | |
| #endregion | |
| void CreateGridGizmo() | |
| { | |
| GameObject Gizmo = new GameObject(); | |
| string gname = ( gGizmo == null ) ? GridName : gGizmo.name; | |
| Gizmo.name = gname; | |
| //Gizmo.AddComponent<GridGizmo>(); | |
| gGizmo = Gizmo.AddComponent<GridGizmo>(); | |
| Gizmo.transform.position = Vector3.zero; | |
| // gGizmo = Gizmo.GetComponent<GridGizmo>(); | |
| gGizmo.GridRows = GridRows; | |
| gGizmo.GridCols = GridCols; | |
| gGizmo.HexSize = HexSize; | |
| gGizmo.GridStyle = GridStyle; | |
| gGizmo.GridOffset = GridOffset; | |
| gGizmo.GridColor = GridColor; | |
| gGizmo.SetupGrid(); | |
| gGizmo.showHexGrid = true; | |
| Selection.activeGameObject = Gizmo; | |
| } | |
| public void SetupGrid () | |
| { | |
| if ( gGizmo != null ) | |
| { | |
| if ( gGizmo.GridRows != GridCols | |
| || gGizmo.GridCols != GridRows | |
| || gGizmo.HexSize != HexSize | |
| || gGizmo.GridStyle != GridStyle | |
| || gGizmo.GridOffset != GridOffset | |
| || gGizmo.transform.position != GridOrigin ) | |
| { | |
| GridRows = gGizmo.GridRows; | |
| GridCols = gGizmo.GridCols; | |
| HexSize = gGizmo.HexSize; | |
| GridStyle = gGizmo.GridStyle; | |
| GridOffset = gGizmo.GridOffset; | |
| GridColor = gGizmo.GridColor; | |
| gGizmo.SetupGrid (); | |
| } | |
| } | |
| } | |
| public void TargetGridGizmo() | |
| { | |
| if ( Selection.activeGameObject | |
| && Selection.activeGameObject.GetComponent<GridGizmo>() != null | |
| && Selection.activeGameObject.GetComponent<GridGizmo>() != gGizmo ) | |
| { | |
| gGizmo = Selection.activeGameObject.GetComponent<GridGizmo>(); | |
| GridCols = gGizmo.GridCols ; | |
| GridRows = gGizmo.GridRows ; | |
| HexSize = gGizmo.HexSize ; | |
| GridStyle = gGizmo.GridStyle ; | |
| GridOffset = gGizmo.GridOffset ; | |
| GridOrigin = gGizmo.transform.position; | |
| GridName = gGizmo.name; | |
| showHexGrid = gGizmo.showHexGrid; | |
| showCornerMarkers = gGizmo.showCornerMarkers; | |
| showEdgeMarkers = gGizmo.showEdgeMarkers; | |
| showBoundingBox = gGizmo.showBoundingBox; | |
| showHexMarkers = gGizmo.showHexMarkers; | |
| Repaint(); | |
| } | |
| } | |
| public void MaintainMinimumSize () | |
| { | |
| GridRows = ( GridRows < 1 ) ? GridRows = 1 : GridRows; | |
| GridCols = ( GridCols < 1 ) ? GridCols = 1 : GridCols; | |
| HexSize = ( HexSize < 1 ) ? HexSize = 1 : HexSize; | |
| } | |
| void UpdateGridOrigin( Vector3 pos) | |
| { | |
| GridOrigin = pos; | |
| } | |
| public void OnEnable() | |
| { | |
| GridGizmo.positionBroadcast += UpdateGridOrigin; | |
| this.autoRepaintOnSceneChange = true; | |
| } | |
| void Update () | |
| { | |
| TargetGridGizmo(); | |
| MaintainMinimumSize (); | |
| SetupGrid (); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment