Beispiel #1
0
    // Use this for initialization
    void Start()
    {
        TileJsonReader.TextureInfo textureInfo = new TileJsonReader.TextureInfo();

        textureInfo.textureName  = "groundBlock.png";
        textureInfo.pixelPerUnit = 32;
        textureInfo.width        = 64;
        textureInfo.Height       = 96;

        List <TileJsonReader.TileInfo> tiles = new List <TileJsonReader.TileInfo>();

        for (int tile_num = 0; tile_num < 1; tile_num++)
        {
            TileJsonReader.TileInfo m_tile = new TileJsonReader.TileInfo();
            m_tile.tileTag   = "GroundBlock";
            m_tile.tileType  = "Tile";
            m_tile.tileName  = "GroundBlock";
            m_tile.tileWidth = 32;
            var metas = new List <TileJsonReader.SpriteMeta>();

            for (int row = 0; row < 6; row++)
            {
                for (int col = 0; col < 4; col++)
                {
                    TileJsonReader.SpriteMeta meta = new TileJsonReader.SpriteMeta();
                    int x = col * 16;
                    int y = row * 16;
                    meta.name   = SpriteIDTable[col + row * 4];                // SET ID
                    meta.x      = x;
                    meta.y      = y;
                    meta.pivotX = 0.5f;
                    meta.pivotY = 0.5f;
                    meta.width  = 16;
                    meta.height = 16;
                    metas.Add(meta);
                }
            }
            m_tile.spriteMetaData = metas.ToArray();
            tiles.Add(m_tile);
        }
        textureInfo.tiles = tiles.ToArray();
        string dst = TileJsonReader.WriteJSON(textureInfo);

        File.WriteAllText("./Assets/SimpleSpriteTile2D/Sample/sample.json", dst, Encoding.UTF8);
    }
        Sprite[] GetAllSpritesByTileInfo(TileJsonReader.TileInfo tile, Texture2D texture)
        {
            List <Sprite> result = new List <Sprite>();
            List <string> names  = new List <string>();

            foreach (var meta in tile.spriteMetaData)
            {
                names.Add(tile.tileName + "_" + meta.name);
            }
            Object[] sprites = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(texture));
            foreach (var tex in sprites)
            {
                var t = tex.name;
                if (names.Contains(t))
                {
                    result.Add((Sprite)tex);
                }
            }
            return(result.ToArray());
        }