bool CreateTilePrefab() { if ((texture == null) || (dstPrefabName == "")) { return(false); } if (autoGenerateTileBySprite) { // Do not use .json template , generate tiles from texture only. // TILE TEXTURE MUST BE LIKE THIS: // [SIZE X SIZE] [SIZE X SIZE] // [SIZE X SIZE] [SIZE X SIZE] // [SIZE X SIZE] [SIZE X SIZE] TileChilds tileChilds = new TileChilds(); tileChilds.AA = new ChildSprites(GetSpritesByID("AANW", texture), GetSpritesByID("AANE", texture), GetSpritesByID("AASW", texture), GetSpritesByID("AASE", texture)); tileChilds.CT = new ChildSprites(GetSpritesByID("CTNW", texture), GetSpritesByID("CTNE", texture), GetSpritesByID("CTSW", texture), GetSpritesByID("CTSE", texture)); tileChilds.UD = new ChildSprites(GetSpritesByID("UDNW", texture), GetSpritesByID("UDNE", texture), GetSpritesByID("UDSW", texture), GetSpritesByID("UDSE", texture)); tileChilds.LR = new ChildSprites(GetSpritesByID("LRNW", texture), GetSpritesByID("LRNE", texture), GetSpritesByID("LRSW", texture), GetSpritesByID("LRSE", texture)); tileChilds.CV = new ChildSprites(GetSpritesByID("CVNW", texture), GetSpritesByID("CVNE", texture), GetSpritesByID("CVSW", texture), GetSpritesByID("CVSE", texture)); tileChilds.CC = new ChildSprites(GetSpritesByID("CCNW", texture), GetSpritesByID("CCNE", texture), GetSpritesByID("CCSW", texture), GetSpritesByID("CCSE", texture)); GameObject obj = new GameObject(dstPrefabName); ST2D_ComponentTiled base_comp = obj.AddComponent <ST2D_ComponentTiled>(); ST2D_AssistantTiled base_Assistant = obj.AddComponent <ST2D_AssistantTiled>(); base_comp.pixelPerUnit_texture = pixelPerUnit_texture; base_comp.tile_size = tile_Width; base_comp.tileChilds = tileChilds; base_comp.zOrder = zOrder; base_comp.tileTag = tileTag; if (custom_mat != null) { base_comp.mat = custom_mat; } base_Assistant.prefabTileAlign = (float)tile_Width / (float)pixelPerUnit_texture; base_comp.initTileObject(); if (!System.IO.Directory.Exists(dstDir)) { System.IO.Directory.CreateDirectory(dstDir); } if (System.IO.File.Exists(dstDir + dstPrefabName + ".prefab")) { Debug.Log(string.Format("Warning: Prefab {0} already exists.", (dstDir + dstPrefabName + ".prefab"))); bool warning = EditorUtility.DisplayDialog("Warning", string.Format("Warning: Prefab {0} already exists.\n OverWrite?", (dstDir + dstPrefabName + ".prefab")), "OK", "Cancel"); if (warning == true) { File.Delete(dstDir + dstPrefabName + ".prefab"); PrefabUtility.CreatePrefab(dstDir + dstPrefabName + ".prefab", obj); GameObject.DestroyImmediate(obj); return(true); } } PrefabUtility.CreatePrefab(dstDir + dstPrefabName + ".prefab", obj); GameObject.DestroyImmediate(obj); return(true); } return(false); }
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); }