Example #1
0
        public static void ExportAtlas(string inputPath, string outputpath)
        {
            Console.WriteLine($"Loading asset file {inputPath}");
            AssetsManager assetManager = LoadAssetManager(inputPath);

            List <Texture2D> atlasTextures = new List <Texture2D>();
            string           gameVersion   = string.Empty;


            // find game version first
            foreach (var file in assetManager.assetsFileList)
            {
                foreach (var obj in file.Objects)
                {
                    switch (obj)
                    {
                    case TextAsset text:
                        if (text.m_Name == "version")
                        {
                            gameVersion = System.Text.Encoding.UTF8.GetString(text.m_Script);
                        }
                        break;
                    }
                }
            }
            string gameversionOutputPath = Path.Combine(outputpath, gameVersion);

            if (!Directory.Exists(gameversionOutputPath))
            {
                Directory.CreateDirectory(gameversionOutputPath);
            }

            string jsonOutputPath  = Path.Combine(gameversionOutputPath, $"spriteData.json");
            string spriteOutputDir = Path.Combine(gameversionOutputPath, $"sprites");

            if (!Directory.Exists(spriteOutputDir))
            {
                Directory.CreateDirectory(spriteOutputDir);
            }
            if (File.Exists(jsonOutputPath))
            {
                File.Delete(jsonOutputPath);
            }
            using (var fs = File.OpenWrite(jsonOutputPath))
                using (var json = new Utf8JsonWriter(fs, new JsonWriterOptions {
                    Indented = true
                }))
                {
                    json.WriteStartObject();
                    json.WriteString("gameVersion", gameVersion);
                    json.WriteString("unityAssetPath", inputPath);
                    json.WriteStartArray("imageData");
                    foreach (var file in assetManager.assetsFileList)
                    {
                        foreach (var obj in file.Objects)
                        {
                            switch (obj)
                            {
                            case Sprite spr:
                                bool foundAtlas = false;
                                if (spr.m_SpriteAtlas.TryGet(out var atlas))
                                {
                                    if (atlas.m_RenderDataMap.TryGetValue(spr.m_RenderDataKey, out var spriteAtlasData) && spriteAtlasData.texture.TryGet(out var texture))
                                    {
                                        Console.WriteLine($"Found atlas texture {texture.m_Name}");
                                        if (!atlasTextures.Contains(texture))
                                        {
                                            atlasTextures.Add(texture);
                                        }
                                    }
                                    if (atlas.m_Name == "fiveFretAtlas")
                                    {
                                        foundAtlas = true;
                                    }
                                }

                                if (!foundAtlas)
                                {
                                    continue;
                                }

                                var    image    = SpriteHelper.GetImage(spr);
                                string filename = $"{spr.m_Name}_{spr.m_PathID}.png";
                                string filePath = Path.Combine(spriteOutputDir, filename);

                                image.Save(filePath, ImageFormat.Png);
                                byte[] hashData;

                                using (var f = File.OpenRead(filePath))
                                {
                                    hashData = hash.ComputeHash(f);
                                }
                                string hashString = Convert.ToBase64String(hashData);

                                json.WriteStartObject();
                                json.WriteString("fileName", filename);
                                json.WriteString("name", spr.m_Name);
                                json.WriteString("hash", hashString);
                                json.WriteNumber("pathID", (int)spr.m_PathID);
                                json.WriteEndObject();

                                Console.WriteLine($"fiveFretAtlas Sprite {spr.m_Name} saved");
                                break;

                            case SpriteAtlas spriteAtlas:
                                Console.WriteLine($"Atlas {spriteAtlas.m_Name}");
                                break;
                            }
                        }
                    }
                    json.WriteEndArray();

                    json.WriteStartArray("spriteAtlas");
                    foreach (var texture in atlasTextures)
                    {
                        var bitmap = texture.ConvertToBitmap(false);

                        bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        bitmap.Save(Path.Combine(gameversionOutputPath, $"{texture.m_Name}.png"), ImageFormat.Png);
                        Console.WriteLine($"Atlas texture2d {texture.m_Name} saved");
                        json.WriteStringValue(texture.m_Name);
                    }
                    json.WriteEndArray();

                    json.WriteEndObject();
                }
        }