/* * <summary> * Exports <see cref="TextureAtlasFile"/> from a json.atlas file to * a <see cref="TextureAtlasFile"/>. * </summary> * <param name="filename"> * A json.atlas file contains texture regions and attributes. * </param> */ public static void TestExportToFile(TextureAtlasFile textAtlas, string filename) { using (FileStream fs = File.OpenWrite(filename)) { using (StreamWriter sr = new StreamWriter(fs)) { foreach (TextureInfo textInfo in textAtlas.Textures) { sr.WriteLine("\n" + textInfo.ImagePath); sr.WriteLine("size: " + textInfo.Width + ", " + textInfo.Height); sr.WriteLine("format: " + textInfo.Format); sr.WriteLine("filter: " + textInfo.FilterMin + ", " + textInfo.FilterMax); sr.WriteLine("repeat: " + textInfo.Repeat); foreach (SubTextureInfo subtextInfo in textInfo.Subtexture) { sr.WriteLine(subtextInfo.Name); sr.WriteLine(" rotate: " + subtextInfo.Rotate); sr.WriteLine(" xy: " + subtextInfo.X + ", " + subtextInfo.Y); sr.WriteLine(" size: " + subtextInfo.Width + ", " + subtextInfo.Height); sr.WriteLine(" orig: " + subtextInfo.OrigWidth + ", " + subtextInfo.OrigHeight); sr.WriteLine(" orig: " + subtextInfo.OffsetWidth + ", " + subtextInfo.OffsetHeight); sr.WriteLine(" index: " + subtextInfo.Index); } } } } }
/* * <summary> * Imports <see cref="TextureAtlasFile"/> from a json.atlas file to * a <see cref="TextureAtlasFile"/>. * </summary> * <param name="filename"> * A json.atlas file contains texture regions and attributes. * </param> */ public static TextureAtlasFile ImportFromFile(string filename) { FileParser lParser = new FileParser(); SectionTreeCollection tree; TextureAtlasFile textFile = new TextureAtlasFile(); TextureInfo textInfo; SubTextureInfo subtextInfo; List <string> values; tree = lParser.ReadFile(filename); foreach (SectionTreeCollection textureTree in tree.GetChildren()) { textInfo = new TextureInfo(textureTree.SectionName); // Set Texture the Properties values = textureTree.Keys["size"]; if (values != null && values.Count == 2) { try { textInfo.Width = int.Parse(values[0]); textInfo.Height = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); textInfo.Width = 0; textInfo.Height = 0; } } values = textureTree.Keys["filter"]; if (values != null && values.Count == 2) { TextureFilter filter = TextureFilter.Linear; KeyTypeUtilities.Parse <TextureFilter>(values[0], ref filter); textInfo.FilterMin = filter; KeyTypeUtilities.Parse <TextureFilter>(values[1], ref filter); textInfo.FilterMax = filter; } values = textureTree.Keys["format"]; if (values != null && values.Count == 1) { Format format = Format.Alpha; KeyTypeUtilities.Parse <Format>(values[0], ref format); textInfo.Format = format; } values = textureTree.Keys["repeat"]; if (values != null && values.Count == 1) { Repeat repeat = Repeat.xy; KeyTypeUtilities.Parse <Repeat>(values[0], ref repeat); textInfo.Repeat = repeat; } foreach (SectionTreeCollection subTextureTree in textureTree.GetChildren()) { subtextInfo = new SubTextureInfo(subTextureTree.SectionName); // Set Subtexture the Properties values = subTextureTree.Keys["xy"]; if (values != null && values.Count == 2) { try { subtextInfo.X = int.Parse(values[0]); subtextInfo.Y = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.X = 0; subtextInfo.Y = 0; } } values = subTextureTree.Keys["size"]; if (values != null && values.Count == 2) { try { subtextInfo.Width = int.Parse(values[0]); subtextInfo.Height = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.Width = 0; subtextInfo.Height = 0; } } values = subTextureTree.Keys["orig"]; if (values != null && values.Count == 2) { try { subtextInfo.OrigWidth = int.Parse(values[0]); subtextInfo.OrigHeight = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.OrigWidth = 0; subtextInfo.OrigHeight = 0; } } values = subTextureTree.Keys["offset"]; if (values != null && values.Count == 2) { try { subtextInfo.OffsetWidth = int.Parse(values[0]); subtextInfo.OffsetHeight = int.Parse(values[1]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.OffsetWidth = 0; subtextInfo.OffsetHeight = 0; } } values = subTextureTree.Keys["index"]; if (values != null && values.Count == 1) { try { subtextInfo.Index = int.Parse(values[0]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.OffsetWidth = 0; } } values = subTextureTree.Keys["rotate"]; if (values != null && values.Count == 1) { try { subtextInfo.Rotate = bool.Parse(values[0]); } catch (Exception ex) { Console.WriteLine(ex); subtextInfo.OffsetWidth = 0; } } textInfo.Subtexture.Add(subtextInfo); } textFile.Textures.Add(textInfo); } return(textFile); }
public override void OnImportAsset(AssetImportContext ctx) { Dictionary <string, List <SpriteResourceData> > animations = new Dictionary <string, List <SpriteResourceData> >(); TextureAtlasFile textureAtlasFile = TextureAtlasParserImporter.ImportFromFile(ctx.assetPath); string currentDirectory = UnityStringUtilities.RemoveFileName(ctx.assetPath); string resourcePath = ""; Debug.Log("assetPath: " + ctx.assetPath); Debug.Log("currentDirectory: " + currentDirectory); foreach (var texture in textureAtlasFile.Textures) { //Update the texture based off of the atlas Debug.Log("texture.ImagePath: " + currentDirectory + texture.ImagePath); resourcePath = currentDirectory + texture.ImagePath; TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(resourcePath); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.mipmapEnabled = false; textureImporter.filterMode = FilterMode.Point; //Remove Compression to maintain better quality textureImporter.textureCompression = TextureImporterCompression.Uncompressed; List <SpriteMetaData> metas = new List <SpriteMetaData>(); int yPos = 0; foreach (var subtexture in texture.Subtexture) { SpriteMetaData meta = new SpriteMetaData(); // Convert y from top to Unity's bottom yPos = texture.Height - subtexture.Height; yPos = yPos - subtexture.Y; meta.rect = new Rect(subtexture.X, yPos, subtexture.Width, subtexture.Height); if (subtexture.Index != -1) { //meta.name = subtexture.Name + "_" + subtexture.Index.ToString(); meta.name = SpriteResourceData.GenerateImageMetaName(subtexture.Name, subtexture.Index); if (animations.ContainsKey(subtexture.Name)) { animations[subtexture.Name].Add(new SpriteResourceData(subtexture.Index, subtexture.Name, resourcePath, subtexture.FPS, subtexture.Duration)); } else { animations.Add(subtexture.Name, new List <SpriteResourceData>() { new SpriteResourceData(subtexture.Index, subtexture.Name, resourcePath, subtexture.FPS, subtexture.Duration) }); } } else { meta.name = subtexture.Name; } metas.Add(meta); } textureImporter.spritesheet = metas.ToArray(); EditorUtility.SetDirty(textureImporter); textureImporter.SaveAndReimport(); } foreach (var animation in animations) { Debug.Log("animation[" + animation.Key + "]=" + animation.Value); animation.Value.Sort(); if (animation.Value.Count > 0) { MakeAnimation(ctx, animation.Key, animation.Value); animation.Value.Clear(); } } }