Exemplo n.º 1
0
        private bool PrepareTilset()
        {
            // use with UNITY JSON
            //this.pyxelTilemap = JsonUtility.FromJson<PyxelEditTilemap>(this.tilemapJSON.text);

            // use with JSONObject class (http://wiki.unity3d.com/index.php?title=JSONObject)
            this.pyxelTilemap = DeserializeJSON();

            string path      = AssetDatabase.GetAssetPath(pyxelEdit.tilemapJSON.GetInstanceID());
            string assetPath = System.IO.Path.GetDirectoryName(path) + "/" + pyxelEdit.tilemapJSON.name + "_tileset.asset";

            this.tileset = AssetDatabase.LoadAssetAtPath <Tileset>(assetPath);
            bool createNewTileset = !this.tileset;

            if (createNewTileset)
            {
                this.tileset = ScriptableObject.CreateInstance <Tileset>();
            }

            string pathToTexture = System.IO.Path.GetDirectoryName(path) + "/" + pyxelEdit.tilemapJSON.name + ".png";

            Texture2D tilesetTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(pathToTexture, typeof(Texture2D));

            if (tilesetTexture == null)
            {
                Debug.LogError("PyxelEdit ERROR: missing texture at " + pathToTexture + " !");
                return(false);
            }
            else
            {
                tileset.PixelsPerUnit = this.pyxelTilemap.tilewidth;
                tileset.TilePxSize    = new Vector2(this.pyxelTilemap.tilewidth, this.pyxelTilemap.tileheight);
                tileset.AtlasTexture  = tilesetTexture;
                tileset.SliceOffset   = pyxelEdit.sliceOffset;
                tileset.SlicePadding  = pyxelEdit.slicePadding;
                tileset.Slice();
                TilesetEditor.OptimizeTextureImportSettings(tileset.AtlasTexture);
                if (createNewTileset)
                {
                    AssetDatabaseUtility.CreateAssetAndDirectories(tileset, assetPath);
                }
                EditorUtility.SetDirty(this.tileset);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                return(true);
            }
        }
Exemplo n.º 2
0
        private PyxelEditTilemap DeserializeJSON()
        {
            PyxelEditTilemap tilemap = new PyxelEditTilemap();

            JSONObject jsonObject = JSONObject.Parse(pyxelEdit.tilemapJSON.ToString());

            tilemap.tileheight = (int)jsonObject["tileheight"].Number;
            tilemap.tileshigh  = (int)jsonObject["tileshigh"].Number;
            tilemap.tilewidth  = (int)jsonObject["tilewidth"].Number;
            tilemap.tileswide  = (int)jsonObject["tileswide"].Number;
            JSONArray layersData  = jsonObject["layers"].Array;
            int       layersCount = layersData.Length;

            tilemap.layers = new LayerRefs[layersCount];
            for (int i = 0; i < layersCount; i++)
            {
                tilemap.layers[i]        = new LayerRefs();
                tilemap.layers[i].name   = layersData[i].Obj["name"].Str;
                tilemap.layers[i].number = (int)layersData[i].Obj["number"].Number;

                JSONArray tilesData  = layersData[i].Obj["tiles"].Array;
                int       tilesCount = tilesData.Length;
                tilemap.layers[i].tiles = new TileRefs[tilesCount];

                for (int t = 0; t < tilesCount; t++)
                {
                    tilemap.layers[i].tiles[t] = new TileRefs();

                    tilemap.layers[i].tiles[t].flipX = tilesData[t].Obj["flipX"].Boolean;
                    tilemap.layers[i].tiles[t].index = (int)tilesData[t].Obj["flipX"].Number;
                    tilemap.layers[i].tiles[t].rot   = (int)tilesData[t].Obj["rot"].Number;
                    tilemap.layers[i].tiles[t].tile  = (int)tilesData[t].Obj["tile"].Number;
                    tilemap.layers[i].tiles[t].x     = (int)tilesData[t].Obj["x"].Number;
                    tilemap.layers[i].tiles[t].y     = (int)tilesData[t].Obj["y"].Number;
                }
            }
            return(tilemap);
        }