1
\$\begingroup\$

I'm trying to dynamically generate scene representing chess board made from sprites. I'm using two textures for these:
- pieces.png (taken from here: http://opengameart.org/sites/default/files/chess_2.png)
- tiles.png (drawn by hand, consists of two 128x128 squares filled with different colors)

The best result I achieved so far is a board with overlapping tiles:overlapping chess tiles

The sprite mode for these textures is "Single" and pixels per unit are set to "128" (unsure whether I should've done this or not)

The textures are being sliced to sprites in my MonoBehavior.Start:

            var tiles = new Dictionary<int, Tile> ();
            var sprites = new Dictionary<int, Sprite> ();

            // Load all tilesets
            foreach (var externalTileset in map.tilesets) {
                    var tilesetAsset = (TextAsset)Resources.Load ("Boards/" + externalTileset.source);
                    var tileset = (Tileset)tilesetSerializer.Deserialize (new StringReader (tilesetAsset.text));

                    Texture2D texture = (Texture2D)Resources.Load ("Boards/" + tileset.image.source.Replace (".png", ""), typeof(Texture2D));
                    Debug.Log ("Texture for " + tileset.image.source + " loaded: " + texture.width + 'x' + texture.height);

                    int tilesPerRow = tileset.image.width / tileset.tilewidth;
                    int totalTiles = (tileset.image.width / tileset.tilewidth) * (tileset.image.height / tileset.tileheight);
                    for (int tileId = 0; tileId < totalTiles; tileId++) {
                            float x = tileset.tilewidth * (tileId % tilesPerRow);
                            float y = (tileId / tilesPerRow + 1) * tileset.tileheight;
                            sprites [externalTileset.firstgid + tileId] = Sprite.Create (texture, new Rect (x, texture.height - y, tileset.tilewidth, tileset.tileheight), new Vector2 (0.5f, 0.5f));
                    }

                    if (tileset.tiles != null) {
                            foreach (var tile in tileset.tiles) {
                                    tiles.Add (externalTileset.firstgid + tile.id, tile);
                            }
                    }
            }

And when sprites are added to newly created game objects:

        float minX = -4;
        float maxX = 4;
        float minY = -4;
        float maxY = 4;

        for (int i = 0; i < map.height; i++) {
                float y = minY + i * (maxY - minY) / map.height;
                for (int j = 0; j < map.width; j++) {
                        float x = minX + j * (maxX - minX) / map.height;
                        if (pieces [i, j] != null) {
                                GameObject piece = new GameObject (pieces [i, j].properties ["Color"] + ' ' + pieces [i, j].properties ["Type"]);
                                SpriteRenderer renderer = piece.AddComponent<SpriteRenderer> ();
                                renderer.sprite = piecesSprites [i, j];
                                piece.transform.position = new Vector2 (x, y);

                        }
                        if (tileSprites [i, j] != null) {
                                GameObject tile = new GameObject ("Tile");
                                SpriteRenderer renderer = tile.AddComponent<SpriteRenderer> ();
                                renderer.sprite = tileSprites [i, j];
                                tile.transform.position = new Vector2 (x, y);
                        }
                }
        }

I believe I can hack something to make this work, but I want to know how to do it properly What's wrong with my code or scene/resources setup?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Solved! http://docs.unity3d.com/ScriptReference/Sprite.Create.html I should've used proper Sprite factory method and passed pixel to units:

Sprite.Create (texture, new Rect (x, texture.height - y, tileset.tilewidth, tileset.tileheight), new Vector2 (0.5f, 0.5f), 128)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .