Esempio n. 1
0
    Texture2D GetChildSprite(Texture2D spriteSheetTexture, ChildSpriteCoordinates childSprites)
    {
        int       x = childSprites.x, y = childSprites.y, width = childSprites.width, height = childSprites.height;
        Texture2D childSpriteTexture = new Texture2D(width, height);

        Color[] data = spriteSheetTexture.GetPixels(x, spriteSheetTexture.height - y - height, width, height);
        childSpriteTexture.SetPixels(data);
        childSpriteTexture.Apply();
        return(childSpriteTexture);
    }
Esempio n. 2
0
    void GenerateSpritesAndAnimations(bool createSprites, bool createAnimations)
    {
        string separator         = Path.DirectorySeparatorChar.ToString();
        string dataPath          = Path.GetFullPath("Assets" + separator + "BinaryFiles" + separator + "Data");
        string spritePath        = Path.GetFullPath("Assets" + separator + "Resources" + separator + "Sprites");
        string compiledFilesPath = Path.GetFullPath("Assets" + separator + "CompiledFiles");
        string animationsPath    = "Assets" + separator + "Resources" + separator + "Animations" + separator;
        List <AnimationFrameReferences> animationFrames = new List <AnimationFrameReferences>();

        foreach (string file in Directory.GetFiles(dataPath, "*.adf"))
        {
            string fileName = Path.GetFileName(file);

            byte[] bytes = File.ReadAllBytes(file);

            BinaryReader reader = new BinaryReader(new MemoryStream(bytes));
            StreamWriter writer = new StreamWriter(compiledFilesPath + separator + Path.GetFileName(file) + "-header.txt");

            byte fileType    = reader.ReadByte();
            int  extraLength = reader.ReadInt32() + 1;
            writer.WriteLine("Type: {0}, Extra Length: {1}", fileType, extraLength);

            writer.Write("Extra Bytes: ");

            for (int i = 0; i < extraLength - 1; i++)
            {
                writer.Write("{0,4}", reader.ReadByte());                                       // Not sure if offset is needed here
            }
            byte offset = reader.ReadByte();

            writer.WriteLine();

            int numberOfFrames = ApplyOffset(reader.ReadInt32(), offset);

            writer.WriteLine("NumberOfFrames: {0}", numberOfFrames);

            ChildSpriteCoordinates[] childSprites = new ChildSpriteCoordinates[numberOfFrames];
            int totalSprites = 0;
            for (int i = 0; i < numberOfFrames; i++)
            {
                int  id = ApplyOffset(reader.ReadInt32(), offset);
                byte animationLength = ApplyOffsetByte(reader.ReadByte(), offset);
                if (animationLength == 1)
                {
                    int imageId = id;
                    int x       = ApplyOffset(reader.ReadInt32(), offset);
                    int y       = ApplyOffset(reader.ReadInt32(), offset);
                    int width   = ApplyOffset(reader.ReadInt32(), offset);
                    int height  = ApplyOffset(reader.ReadInt32(), offset);
                    childSprites[totalSprites++] = new ChildSpriteCoordinates(imageId, x, y, width, height);

                    writer.WriteLine("Id: {0,4} X: {1,4} Y: {2,4} W: {3,4} H: {4,4}", imageId, x, y, width, height);
                }
                else
                {
                    int animationId = id;
                    AnimationFrameReferences animReference = new AnimationFrameReferences(animationId);
                    writer.Write("Animation Id: {0,6} Frame Ids:", animationId);
                    for (int j = 0; j < animationLength; ++j)
                    {
                        int frameId = ApplyOffset(reader.ReadInt32(), offset);
                        animReference.AddFrame(frameId);
                        writer.Write(" {0,6}", frameId);
                    }
                    byte delimiter = ApplyOffsetByte(reader.ReadByte(), offset);
                    animationFrames.Add(animReference);

                    writer.WriteLine(" Delimiter: {0}", delimiter);
                }
            }
            if (numberOfFrames != totalSprites)
            {
                Debug.Log(fileName + " Frames: " + numberOfFrames + " Sprites: " + totalSprites);
            }
            if (offset != bytes[bytes.Length - 2])
            {
                Debug.Log(fileName + " Offset: " + offset + " SecondLastByte: " + bytes[bytes.Length - 2]);
            }
            int unknown = ApplyOffset(reader.ReadInt32(), offset);
            writer.WriteLine("U: {0}", unknown);
            writer.Close();

            if (createSprites)
            {
                int    length = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
                byte[] buffer = reader.ReadBytes(length);
                byte[] data   = new byte[RealSize(buffer.Length, 0x315)];
                for (int k = 0; k < buffer.Length; k++)
                {
                    data[k - (k / 790)] = ApplyOffsetByte(buffer[k], offset);
                }
                string compiledFileNamePath = compiledFilesPath + separator + fileName.Substring(0, fileName.IndexOf('.'));
                if (unknown == 36)
                {
                    File.WriteAllBytes(compiledFileNamePath + ".wav", data);
                    continue;
                }

                BMPLoader bmpLoader = new BMPLoader();
                BMPImage  bmpImg    = bmpLoader.LoadBMP(data);
                if (bmpImg == null)
                {
                    continue;
                }

                Texture2D tex = bmpImg.ToTexture2D();
                RemoveBlackBackground(tex);
                SaveTextureAsPNG(tex, compiledFileNamePath + ".png");

                for (int i = 0; i < totalSprites; ++i)
                {
                    tex = GetChildSprite(bmpImg.ToTexture2D(), childSprites[i]);
                    RemoveBlackBackground(tex);
                    SaveTextureAsPNG(tex, spritePath + separator + childSprites[i].frameId + ".png");
                }
            }
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        if (createAnimations)
        {
            foreach (AnimationFrameReferences animReference in animationFrames)
            {
                Sprite[] animSprites = new Sprite[animReference.frameList.Count];
                int      spriteIndex = 0;
                foreach (int frameId in animReference.frameList)
                {
                    animSprites[spriteIndex++] = Resources.Load <Sprite>("Sprites" + separator + frameId); //atlas.GetSprite(spriteId);
                }
                CreateAnimation(animationsPath, animReference.animationId.ToString(), animSprites, 8, true);
            }
        }
        Debug.Log("End");
    }