Exemplo n.º 1
0
    public void CreateMapWithGrid(int[,] grid)
    {
        if (_tileRenderer == null)
        {
            this.Awake();
        }
        else
        {
            this.Clear();
        }

        for (int x = 0; x < grid.GetLength(0); ++x)
        {
            for (int y = 0; y < grid.GetLength(1); ++y)
            {
                string prefabKey = TilingHelper.GetTileType(TilingHelper.GetNeighbors(grid, x, y, _tileRenderer.OffMapIsFilled));
                if (_geometryPrefabs.ContainsKey(prefabKey))
                {
                    GameObject    prefab      = _geometryPrefabs[prefabKey];
                    IntegerVector intPosition = _tileRenderer.PositionForTile(x, y);
                    Vector3       position    = new Vector3(intPosition.X, intPosition.Y, 0);
                    GameObject    geom        = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
                    geom.transform.parent        = this.transform;
                    geom.transform.localPosition = position;
                }
            }
        }
    }
Exemplo n.º 2
0
    private void createMapUsingMesh(int[,] grid)
    {
        float originX = this.transform.position.x;
        float originY = this.transform.position.y;
        float originZ = this.transform.position.z;

        int numTiles     = this.Width * this.Height;
        int numTriangles = numTiles * 2;

        // Generate mesh data
        List <Vector3> vertices = new List <Vector3>();
        List <Vector3> normals  = new List <Vector3>();
        List <Vector2> uvs      = new List <Vector2>();

        int[] triangles = new int[numTriangles * 3]; // Clockwise order of vertices within triangles (for correct render direction)

        for (int y = 0; y < this.Height; ++y)
        {
            for (int x = 0; x < this.Width; ++x)
            {
                int tileIndex     = this.Width * y + x;
                int triangleIndex = tileIndex * 2 * 3;

                // Create 4 verts
                Vector3 bottomLeft  = new Vector3(originX + x * this.TileRenderSize, originY + y * this.TileRenderSize, originZ);
                Vector3 bottomRight = new Vector3(bottomLeft.x + this.TileRenderSize, bottomLeft.y, originZ);
                Vector3 topLeft     = new Vector3(bottomLeft.x, bottomLeft.y + this.TileRenderSize, originZ);
                Vector3 topRight    = new Vector3(bottomRight.x, topLeft.y, originZ);

                // Indices of verts
                int bottomLeftVert  = vertices.Count;
                int bottomRightVert = bottomLeftVert + 1;
                int topLeftVert     = bottomRightVert + 1;
                int topRightVert    = topLeftVert + 1;

                // Assign vert indices to triangles
                triangles[triangleIndex]     = topLeftVert;
                triangles[triangleIndex + 1] = bottomRightVert;
                triangles[triangleIndex + 2] = bottomLeftVert;

                triangles[triangleIndex + 3] = topLeftVert;
                triangles[triangleIndex + 4] = topRightVert;
                triangles[triangleIndex + 5] = bottomRightVert;

                // Handle UVs
                Vector2[] spriteUVs = _sprites[TilingHelper.GetTileType(TilingHelper.GetNeighbors(grid, x, y, this.OffMapIsFilled))].uv;

                Vector2 bottomLeftUV  = spriteUVs[0];
                Vector2 bottomRightUV = spriteUVs[1];
                Vector2 topLeftUV     = spriteUVs[2];
                Vector2 topRightUV    = spriteUVs[3];

                // Add vertices and vertex data to mesh data
                vertices.Add(bottomLeft);
                vertices.Add(bottomRight);
                vertices.Add(topLeft);
                vertices.Add(topRight);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                normals.Add(Vector3.up);
                uvs.Add(bottomLeftUV);
                uvs.Add(bottomRightUV);
                uvs.Add(topLeftUV);
                uvs.Add(topRightUV);
            }
        }

        // Populate a mesh
        Mesh mesh = new Mesh();

        mesh.vertices  = vertices.ToArray();
        mesh.normals   = normals.ToArray();
        mesh.uv        = uvs.ToArray();
        mesh.triangles = triangles;

        // Assign mesh to behaviors
        this.GetComponent <MeshFilter>().mesh = mesh;
        this.renderer.material.mainTexture    = this.Atlas;
    }