Exemple #1
0
    public GameObject[,] CreateGrid(bool isPointyTopped, float hexSize, Vector2 gridSize)
    {
        GameObject[,] hexs = new GameObject[(int)gridSize.x, (int)gridSize.y];
        for (int x = 0; x < gridSize.x; x++)
        {
            for (int y = 0; y < gridSize.y; y++)
            {
                var gObj = new GameObject(x + "," + y);
                gObj.transform.SetParent(this.transform);
                var meshfilter   = gObj.AddComponent <MeshFilter>();
                var meshrenderer = gObj.AddComponent <MeshRenderer>();

                //Create Mesh
                meshfilter.mesh = Hexagon.CreateHexagonMesh2D(isPointyTopped, hexSize);

                //Create Material
                meshrenderer.material = hexMaterial;

                //position
                if (!isPointyTopped)
                {
                    float height = (hexSize * 2);
                    float vert   = height * 0.75f;
                    float width  = (Mathf.Sqrt(3) * 0.5f) * height;

                    gObj.transform.position = new Vector3(width * x + width * 0.5f * (IsOdd(y) ? 1 : 0), 0, (height * 0.5f) * (y + 1) + height * 0.25f * y);
                }
                else
                {
                    float width  = (hexSize * 2);
                    float vert   = width * 0.75f;
                    float height = (Mathf.Sqrt(3) * 0.5f) * width;
                    float horiz  = height;

                    gObj.transform.position = calcWorldCoords(new Vector2(x, y));
                }

                hexs[x, y] = gObj;
            }
        }

        return(hexs);
    }