Esempio n. 1
0
        public void CreateSpriteTile(GameObject group, TileObject tileObject)
        {
            int        tileID = (int)tileObject.gid;
            GameObject g      = new GameObject(tileObject.name);

            g.transform.SetParent(group.transform);
            float y = tmxFile.height * tmxFile.tileHeight - tileObject.y;

            g.transform.localPosition    = new Vector3(tileObject.x, y, 0) / pixelsPerUnit;
            g.transform.localEulerAngles = Vector3.forward * -tileObject.rotation;

            SpriteRenderer sprite = g.AddComponent <SpriteRenderer>();

            sprite.flipX = TMXFile.FlippedHorizontally(tileObject.gid);
            sprite.flipY = TMXFile.FlippedVertically(tileObject.gid);
            TileSet tileSet = tmxFile.GetTileSetByTileID(tileID);

            if (tileSet != null && tileSet.image != null && !string.IsNullOrEmpty(tileSet.image.source))
            {
                g.transform.localScale = new Vector3(tileObject.width, tileObject.height, pixelsPerUnit) / pixelsPerUnit;
                int       tileSetIndex = System.Array.IndexOf(tmxFile.tileSets, tileSet);
                Material  mat          = tileSetMaterials[tileSetIndex];
                Texture2D tex          = mat.mainTexture as Texture2D;
                TileRect  r            = tileSet.GetTileSpriteRect(tileID);
                Rect      rect         = new Rect(r.x, r.y, r.width, r.height);
                Vector2   pivot        = Vector2.zero;
                sprite.sprite = Sprite.Create(tex, rect, pivot, pixelsPerUnit, 0, SpriteMeshType.FullRect);
            }
            else
            {
                int tileSetIndex = System.Array.IndexOf(tmxFile.tileSets, tileSet);
                sprite.sprite = tileSetSprites[tileSetIndex][tileID - tileSet.firstGID];
            }

            if (idToPhysics.ContainsKey(tileID))
            {
                float             yOff = tmxFile.tileHeight / pixelsPerUnit;
                Vector2[]         path = System.Array.ConvertAll(idToPhysics[tileID], (p) => new Vector2(p.x, p.y + yOff));
                PolygonCollider2D poly = g.AddComponent <PolygonCollider2D>();
                poly.pathCount = 1;
                poly.SetPath(0, path);
            }

            SetProperties(g, tileObject.properties);
        }
Esempio n. 2
0
 public bool FlippedVertically(int index)
 {
     return(TMXFile.FlippedVertically(tileFlags[index]));
 }