Example #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);
    }
        //Load sheet info from json file
        List <SpriteMetaData> LoadSpriteSheetFromClass(TileJsonReader.TextureInfo textureInfo)
        {
            List <SpriteMetaData> sheet = new List <SpriteMetaData>();

            foreach (var tileInfo in textureInfo.tiles)
            {
                foreach (var c in tileInfo.spriteMetaData)
                {
                    var  meta = new SpriteMetaData();
                    Rect r    = new Rect();
                    r.width    = c.width;
                    r.height   = c.height;
                    r.x        = c.x;
                    r.y        = textureInfo.Height - c.y - r.height;
                    meta.rect  = r;
                    meta.name  = tileInfo.tileName + "_" + c.name;
                    meta.pivot = new Vector2(c.pivotX, c.pivotY);
                    sheet.Add(meta);
                }
            }
            return(sheet);
        }
        bool BatchCreatePrefabs()
        {
            string input_text = "[]";

            if (spritesheet_json == null)
            {
                Debug.Log("Prefab Creator Error: JSON file Found.");
                return(false);
            }
            //TileJsonReader jsonReader = new TileJsonReader();
            TileJsonReader.TextureInfo textureInfo = new TileJsonReader.TextureInfo();
            input_text = spritesheet_json.text;
            bool   result = TileJsonReader.ReadJson(input_text, out textureInfo);
            string texture_name_with_ext = Path.GetFileName(AssetDatabase.GetAssetPath(texture));

            if (texture_name_with_ext.ToLower() != textureInfo.textureName.ToLower())
            {
                EditorUtility.DisplayDialog("Error", string.Format("texture name in json not vaild.texture is {0}, but got {1} in json", texture_name_with_ext, textureInfo.textureName), "OK", "Cancel");
                return(false);
            }
            if (result == false)
            {
                EditorUtility.DisplayDialog("Error", string.Format("JSON {0} not vaild.", spritesheet_json.name), "OK", "Cancel");
                return(false);
            }
            SaveSpriteSheet(LoadSpriteSheetFromClass(textureInfo), texture);
            Debug.Log(string.Format("Got Texture... Reading {0} tiles", textureInfo.tiles.Length));
            foreach (var tileInfo in textureInfo.tiles)
            {
                switch (tileInfo.tileType)
                {
                case "Tile":
                    TileChilds tileChilds = new TileChilds();
                    tileChilds.AA = new ChildSprites(GetSpritesByName(tileInfo.tileName + "_" + "AANW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "AANE", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "AASW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "AASE", texture));
                    tileChilds.CT = new ChildSprites(GetSpritesByName(tileInfo.tileName + "_" + "CTNW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CTNE", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CTSW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CTSE", texture));
                    tileChilds.UD = new ChildSprites(GetSpritesByID("UDNW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "UDNE", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "UDSW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "UDSE", texture));
                    tileChilds.LR = new ChildSprites(GetSpritesByName(tileInfo.tileName + "_" + "LRNW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "LRNE", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "LRSW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "LRSE", texture));
                    tileChilds.CV = new ChildSprites(GetSpritesByName(tileInfo.tileName + "_" + "CVNW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CVNE", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CVSW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CVSE", texture));
                    tileChilds.CC = new ChildSprites(GetSpritesByName(tileInfo.tileName + "_" + "CCNW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CCNE", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CCSW", texture),
                                                     GetSpritesByName(tileInfo.tileName + "_" + "CCSE", texture));

                    GameObject          obj            = new GameObject(tileInfo.tileName);
                    ST2D_ComponentTiled base_comp      = obj.AddComponent <ST2D_ComponentTiled>();
                    ST2D_AssistantTiled base_Assistant = obj.AddComponent <ST2D_AssistantTiled>();
                    base_comp.pixelPerUnit_texture = textureInfo.pixelPerUnit;
                    base_comp.tile_size            = tileInfo.tileWidth;
                    base_comp.tileChilds           = tileChilds;
                    base_comp.zOrder  = zOrder;
                    base_comp.tileTag = tileInfo.tileTag;
                    if (custom_mat != null)
                    {
                        base_comp.mat = custom_mat;
                    }
                    base_Assistant.prefabTileAlign = (float)tileInfo.tileWidth / (float)textureInfo.pixelPerUnit;
                    base_comp.initTileObject();
                    if (!System.IO.Directory.Exists(dstDir))
                    {
                        System.IO.Directory.CreateDirectory(dstDir);
                    }
                    if (System.IO.File.Exists(dstDir + tileInfo.tileName + ".prefab"))
                    {
                        File.Delete(dstDir + tileInfo.tileName + ".prefab");
                    }
                    PrefabUtility.CreatePrefab(dstDir + tileInfo.tileName + ".prefab", obj);
                    GameObject.DestroyImmediate(obj);
                    Debug.Log(string.Format("Prefab {0} Created.", dstDir + tileInfo.tileName + ".prefab"));
                    break;

                case "Simple":
                    GameObject           objSimple  = new GameObject(tileInfo.tileName);
                    ST2D_ComponentSimple base_comp1 = objSimple.AddComponent <ST2D_ComponentSimple>();
                    ST2D_AssistantSimple base_ast   = objSimple.AddComponent <ST2D_AssistantSimple>();
                    base_comp1.m_sprites = GetAllSpritesByTileInfo(tileInfo, texture);
                    if (custom_mat != null)
                    {
                        base_comp1.mat = custom_mat;
                    }
                    base_comp1.zOrder        = zOrder;
                    base_comp1.tileTag       = tileInfo.tileTag;
                    base_ast.prefabTileAlign = (float)tileInfo.tileWidth / (float)textureInfo.pixelPerUnit;
                    base_comp1.initSimpleTile();
                    if (!System.IO.Directory.Exists(dstDir))
                    {
                        System.IO.Directory.CreateDirectory(dstDir);
                    }
                    if (System.IO.File.Exists(dstDir + tileInfo.tileName + ".prefab"))
                    {
                        File.Delete(dstDir + tileInfo.tileName + ".prefab");
                    }
                    PrefabUtility.CreatePrefab(dstDir + tileInfo.tileName + ".prefab", objSimple);
                    GameObject.DestroyImmediate(objSimple);
                    Debug.Log(string.Format("Prefab {0} Created.", dstDir + tileInfo.tileName + ".prefab"));
                    break;
                }
            }
            return(true);
        }