A few weeks ago I switched my strategy game development from XNA to Unity. The main reason was cross-platform deployment. As I’m targeting XBLA as the end platform for my game I needed to make sure that the technology I went with was able to run there but in the event I couldn’t get it published on that platform I didn’t want all of my work to go to waste. In came Unity. If needed, I can turn around all that I’ve done for the Xbox and deploy to Windows or iOS devices. Since we are still a small company, this flexibility is necessary.

Another of the huge benefits I’ve seen with Unity is the ability to use their toolset as a game editor, allowing me to lay out the levels without creating an additional set of tools. Over the past week I’ve been creating my own custom editor windows and components to handle the creation of levels for the game. One of the things I needed to be able to do was create not only a hex grid but be able to change the materials associated with each hex quickly.  The first part, creation of the hex grid based on width and height input came together pretty fast, but the editor for the hex material has taken a bit longer. Originally I created a dropdown when a hex is selected with the list of the materials available, but even with a 20×20 grid, doing a dropdown for 400 items gets tiring fast. I needed a quicker way of changing the item in the dropdown and wanted to be able to select material based on keypress as well. Now selecting a single hex and hitting a key allows the material to be switched really quickly. Since I had a hard time finding examples as to how to use keypresses in an editor window I’m going to make mine available for anyone else that might need something like this. You can find the code below.


public class HexEditor : Editor
{
/// OnEnable - hit when the script is triggered
public void OnEnable()
{
SceneView.onSceneGUIDelegate = Update;
}

/// <summary>
/// Update the specified sceneview.
/// </summary>
void Update(SceneView sceneview)
{
Event e = Event.current;

if ((e.isKey && e.character >= '0') && (e.isKey && e.character <= '9'))
{
int index = e.character - '0';
ChangeTile(HexTileInfo.HexType.HEXTYPE_EMPTY + index);
}
}

/// <summary>
/// Raises the inspector GUI event.
/// </summary>
public override void OnInspectorGUI()
{
ChangeTile((HexTileInfo.HexType)EditorGUILayout.EnumPopup("Tile",((HexTileInfo)target).hexType));
}

/// <summary>
/// Changes the tile - Switches between the type of material needing to be assigned
/// </summary>
private void ChangeTile(HexTileInfo.HexType type)
{
if ((type < HexTileInfo.HexType.HEXTYPE_EMPTY) || (type >= HexTileInfo.HexType.HEXTYPE_NUMTYPES))
{
return;
}

// set the type for this hex
((HexTileInfo)target).hexType=type;}
}