Unity Hex Mapeditor for auto-connection

Unity Hex Mapeditor for auto-connection is use logic algorithm to put hex cell and auto-connect related near hex cell with suite prototype model(prefab).

This Unity project is for Hex MapEditor. The project is created by Unity 2019.4.1f1 (64-bit). This is a part of my Unity Game. I wish the logic can help other people who want to do the hex auto-connection.

 

Download: the complete package "AkMapEditorForHex.unitypackage"

First video demos the height equal 0 and 1 to put hexes on the map.

 

setp1: please Import Package in Project View

step2: Tools/Level Creator/New Level Scene

step3: click Level to fill default floor with green hex height=0

HexClickLevel.png - Unity

 

step4: Show Palette and select Hex Tab. Select HexGreen or HexRed with height = 0 or 1 to put(HexDraw) on the floor in the scene view.

 


 Stage1 Tab has some buildings, you can put by HexDraw button and click hex floor.  The "View" can move view position.

 

If you see the dark building, you can add a directional light to the scene.

 

Second video demos the auto-connection for related near hexes.

 

Step1: Switch to AutoHex Tab and click HexGreen1


 

Step2: Click Level in Hierarchy view and click "HexDraw" to put Hex on the floor. The hexes you put will be auto-connection together.

 

 

Coding

The main code is in LevelInspector.cs(Assets/AkMapEditor/Tools/LevelCreator/Editor).

1.load prefab

	hexDict = new Dictionary<int, GameObject>();

	for (int index = 0; index <= 63; index++)
	{
		string ppath = @"Assets/AKMapEditor/PrefabsHex/hex" + string.Format("{0:00}", index) + ".prefab";
		GOHex00 = AssetDatabase.LoadAssetAtPath(ppath, typeof(GameObject)) as GameObject;
		hexDict.Add(index, GOHex00);
	}

2.HexDraw

		private void HexDraw(Vector2 mousePosition)
		{
			Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(ray, out hit, 100))
			{
				GameObject go = hit.collider.gameObject;
				HexagonCell hexagonCell = go.GetComponent();
				int col = hexagonCell.cellIndexX;
				int row = hexagonCell.cellIndexY;
				//Debug.Log("col=" + col + ",row=" + row);

				GameObject parentLayer = GameObject.Find("CubeParent");

				HexUtil.Point point2d = new HexUtil.Point(col, row);

				Vector2 vec2 = hexUtil.ArrayToPixel(point2d);

				HexController hexController = _pieceSelected as HexController;

				PaletteItem item = _pieceSelected.gameObject.GetComponent();

				if (item.category == PaletteItem.Category.AutoHex)
				{
					HexMapData[col,row] = true;

					if (HexGOMapData[point2d.X, point2d.Y] != null)
					{
						DestroyImmediate(HexGOMapData[point2d.X, point2d.Y]);
					}

					GameObject go1 = GetHexCellGO(GetLeftBottom(point2d));
					GameObject go2 = GetHexCellGO(GetLeft(point2d));
					GameObject go3 = GetHexCellGO(GetLeftTop(point2d));
					GameObject go4 = GetHexCellGO(GetRightTop(point2d));
					GameObject go5 = GetHexCellGO(GetRight(point2d));
					GameObject go6 = GetHexCellGO(GetRightBottom(point2d));

					if (go1 == null && go2 == null && go3 == null && go4 == null && go5 == null && go6 == null)
					{
						GameObject goZero = hexDict[0];
						HexGOMapData[point2d.X, point2d.Y] = Instantiate(goZero, new Vector3(vec2.x + HexUtil.m_Width * HexUtil.Cos30, 0, vec2.y + HexUtil.m_Width), Quaternion.identity) as GameObject;
					}
					else
					{
						GetHexCellGO(point2d);
					}

				}
				else if (item.category == PaletteItem.Category.Hex)
				{
					if (hexController.height == 0)
					{
						GameObject gobj = Instantiate(_pieceSelected.gameObject, new Vector3(vec2.x, 0, vec2.y), Quaternion.identity) as GameObject;

						gobj.transform.parent = parentLayer.transform;

						HexagonCell hexCell = gobj.GetComponent();
						hexCell.m_Width = HexUtil.m_Width;
						hexCell.cellIndexX = col;
						hexCell.cellIndexY = row;
						hexCell.height = hexController.height;

						hexCell.CreateCell();

						_myTarget.Pieces[col + row * _myTarget.TotalColumns] = gobj.GetComponent();
					}
					else
					{
						GameObject heightObj = null;

						if (_pieceSelected.gameObject.name == "HexCellGreen")
						{
							heightObj = AssetDatabase.LoadAssetAtPath(@"Assets/AKMapEditor/Prefabs/HexsH/hexGreenH1.prefab", typeof(GameObject)) as GameObject;
						}
						else if (_pieceSelected.gameObject.name == "HexCellRed")
						{
							heightObj = AssetDatabase.LoadAssetAtPath(@"Assets/AKMapEditor/Prefabs/HexsH/hexRedH1.prefab", typeof(GameObject)) as GameObject;
						}

						GameObject gobj = Instantiate(heightObj, new Vector3(vec2.x, 0, vec2.y) + new Vector3(HexUtil.m_Width * HexUtil.Cos30, 0, HexUtil.m_Width), Quaternion.Euler(0, 0, 0)) as GameObject;

						gobj.transform.parent = parentLayer.transform;

						HexagonCell hexCell = gobj.GetComponent();
						hexCell.m_Width = HexUtil.m_Width;
						hexCell.cellIndexX = col;
						hexCell.cellIndexY = row;
						hexCell.height = hexController.height;

						hexCell.CreateCell();

						_myTarget.Pieces[col + row * _myTarget.TotalColumns] = gobj.GetComponent();
					}
				}
				else if (item.category == PaletteItem.Category.Stage1)
				{
					GameObject gobj = Instantiate(_pieceSelected.gameObject, new Vector3(vec2.x, hexController.height * 0.5f * HexUtil.m_Width, vec2.y), _pieceSelected.gameObject.transform.rotation) as GameObject;

					gobj.transform.parent = parentLayer.transform;

					HexagonCell hexCell = gobj.GetComponent();
					hexCell.m_Width = HexUtil.m_Width;
					hexCell.cellIndexX = col;
					hexCell.cellIndexY = row;
					hexCell.height = hexController.height;

					//hexCell.CreateCell();

					_myTarget.Pieces[col + row * _myTarget.TotalColumns] = gobj.GetComponent();

				}

				DestroyImmediate(go);

				//Debug.Log("Success " + hit.collider.tag);
				//if (hit.collider.tag == "wormyguy")
				//{
				//	Debug.Log("Success");
				//}
			}
		}

 

3.Get the Value of Hex and Get the prefab to Instantiate

		private GameObject GetHexCellGO(HexUtil.Point point2d)
		{
			Vector2 vec2 = hexUtil.ArrayToPixel(point2d);

			int value = 0;

			bool b =  GetHexCellBool(point2d)?true:false;

			if (b == true)
			{
				if (HexGOMapData[point2d.X, point2d.Y] != null)
				{
					DestroyImmediate(HexGOMapData[point2d.X, point2d.Y]);
				}

				value += GetHexCellBool(GetLeftBottom(point2d)) ? 1 : 0;
				value += GetHexCellBool(GetLeft(point2d)) ? 2 : 0;
				value += GetHexCellBool(GetLeftTop(point2d)) ? 4 : 0;
				value += GetHexCellBool(GetRightTop(point2d)) ? 8 : 0;
				value += GetHexCellBool(GetRight(point2d)) ? 16 : 0;
				value += GetHexCellBool(GetRightBottom(point2d)) ? 32 : 0;

				GameObject go = hexDict[value];
				HexGOMapData[point2d.X, point2d.Y] = Instantiate(go, new Vector3(vec2.x + HexUtil.m_Width * HexUtil.Cos30, 0, vec2.y + HexUtil.m_Width), go.transform.rotation) as GameObject;

				return go;
			}
			else
			{
				value = 0;

				return null;
			}			
		}

4.prototype use 13 models of 3ds Max to create 64 prefabs

For example, aspect to prototype 5 = 1 +4

num is the value of one hex. type is prototype model. rotate is the rotation of num prefab use type model.

 

Reference:

Framework references the book (Extending Unity with Editors Scripting).

Logic and Collage reference the book (遊戲程式設計概論 C++)