Skip to content

Instantly share code, notes, and snippets.

@Enigo
Created September 3, 2023 10:54
Show Gist options
  • Save Enigo/91f32ffa4496690252373b18eca6263f to your computer and use it in GitHub Desktop.
Save Enigo/91f32ffa4496690252373b18eca6263f to your computer and use it in GitHub Desktop.
public class BoardCreator : MonoBehaviour
{
public GameObject Hex;
// make positive to achieve opposite layout
private const float Offset = -0.5f;
private const float HexDistance = 1.2f;
private void Start()
{
SetupRowLayout();
}
private void SetupRowLayout()
{
var rows = 5;
var columns = 5;
float x = Offset, z = 0f;
var hexes = new Dictionary<Vector2Int, GameObject>(rows * columns);
for (var row = 1; row <= rows; row++)
{
for (var column = 1; column <= columns; column++)
{
var positionInGrid = new Vector2Int(row, column);
var hex = Instantiate(Hex, new Vector3(x, 0, z),
Quaternion.identity, transform);
hexes[positionInGrid] = hex;
x += HexDistance;
}
x = row % 2 == 0 ? Offset : 0;
z += HexDistance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment