コード例 #1
0
ファイル: tk2dUnpacker.cs プロジェクト: nickc01/WeaverCore
        static SpriteCollectionData GetCollectionData(UnityEngine.Object spriteCollectionData)
        {
            var rawSprites            = GetSpriteDefinitionsRaw(spriteCollectionData);
            SpriteCollectionData data = new SpriteCollectionData();

            data.CollectionName    = (string)CollectionDataType.GetField("spriteCollectionName").GetValue(spriteCollectionData);
            data.MainTexture       = (Texture)((Array)CollectionDataType.GetField("textures").GetValue(spriteCollectionData)).GetValue(0);
            data.GUID              = (string)CollectionDataType.GetField("spriteCollectionName").GetValue(spriteCollectionData);
            data.TextureFilterMode = (FilterMode)CollectionDataType.GetField("textureFilterMode").GetValue(spriteCollectionData);
            data.Version           = (int)CollectionDataType.GetField("version").GetValue(spriteCollectionData);

            data.Definitions = new SpriteDefinitionData[rawSprites.GetLength(0)];

            Debug.Log("Collection Name = " + data.CollectionName);

            Debug.Log("Sprite Count = " + rawSprites.GetLength(0));

            for (int i = 0; i < rawSprites.GetLength(0); i++)
            {
                var rawSprite = rawSprites.GetValue(i);

                var definition = new SpriteDefinitionData();
                definition.Name = (string)SpriteDefinitionType.GetField("name").GetValue(rawSprite) ?? ("unknown_" + i.ToString());

                Debug.Log("Sprite = " + definition.Name);
                Debug.Log("Sprite ID = " + i);

                var oldPositions = (Vector3[])SpriteDefinitionType.GetField("positions").GetValue(rawSprite);
                var newPositions = new Vector2[oldPositions.GetLength(0)];
                for (int j = 0; j < oldPositions.GetLength(0); j++)
                {
                    newPositions[j] = oldPositions[j];
                }
                definition.Positions   = newPositions;
                definition.TextureSize = (Vector2)SpriteDefinitionType.GetField("texelSize").GetValue(rawSprite);
                definition.UVs         = (Vector2[])SpriteDefinitionType.GetField("uvs").GetValue(rawSprite);
                var flippedEnum = SpriteDefinitionType.GetField("flipped").GetValue(rawSprite);
                var enumType    = flippedEnum.GetType();
                definition.Flipped  = Enum.GetName(enumType, flippedEnum) == "Tk2d";
                data.Definitions[i] = definition;
            }

            return(data);
        }
コード例 #2
0
ファイル: tk2dUnpacker.cs プロジェクト: nickc01/WeaverCore
        static IEnumerable <tk2DSpriteData> ReadSpritesFromData(SpriteCollectionData data)
        {
            var mainTexture = data.MainTexture as Texture2D;

            using (var progress = new ProgressBar(data.Definitions.GetLength(0) - 1, "Unpacking", "Unpacking TK2DSpriteDefinition", true))
            {
                using (var readContext = new ReadableTextureContext(mainTexture))
                {
                    for (int i = 0; i < data.Definitions.GetLength(0); i++)
                    {
                        progress.GoToNextStep();
                        var sprite = data.Definitions[i];

                        Vector2 uvOffset = new Vector2(0.001f, 0.001f);

                        Vector2 PostProcessedBL = Vector2.zero;
                        Vector2 PostProcessedTR = Vector2.zero;

                        bool rotated = false;

                        if (sprite.UVs[0].x == sprite.UVs[1].x && sprite.UVs[2].x == sprite.UVs[3].x)
                        {
                            rotated = true;
                        }


                        if (rotated)
                        {
                            PostProcessedBL = sprite.UVs[1];
                            PostProcessedTR = sprite.UVs[2];
                        }
                        else
                        {
                            PostProcessedBL = sprite.UVs[0];
                            PostProcessedTR = sprite.UVs[3];
                        }

                        int PreBLx = Mathf.RoundToInt((PostProcessedBL.x * mainTexture.width) - uvOffset.x);
                        int PreTRy = Mathf.RoundToInt(((PostProcessedBL.y) * mainTexture.height) - uvOffset.y);
                        int PreTRx = Mathf.RoundToInt((PostProcessedTR.x * mainTexture.width) + uvOffset.x);
                        int PreBLy = Mathf.RoundToInt(((PostProcessedTR.y) * mainTexture.height) + uvOffset.y);

                        int PreWidth  = Mathf.Abs(PreBLx - PreTRx);
                        int PreHeight = Mathf.Abs(PreBLy - PreTRy);

                        Orientation orientation = Orientation.Up;

                        if (PostProcessedBL.x < PostProcessedTR.x && PostProcessedBL.y < PostProcessedTR.y)
                        {
                            orientation = Orientation.Up;
                        }
                        else if (PostProcessedBL.x < PostProcessedTR.x && PostProcessedBL.y > PostProcessedTR.y)
                        {
                            orientation = Orientation.Right;
                        }
                        else if (PostProcessedBL.x > PostProcessedTR.x && PostProcessedBL.y > PostProcessedTR.y)
                        {
                            orientation = Orientation.Down;
                        }
                        else if (PostProcessedBL.x > PostProcessedTR.x && PostProcessedBL.y < PostProcessedTR.y)
                        {
                            orientation = Orientation.Left;
                        }

                        Vector2Int Min = new Vector2Int(Mathf.RoundToInt(Mathf.Min(PreBLx, PreTRx)), Mathf.RoundToInt(Mathf.Min(PreBLy, PreTRy)));

                        Vector2Int Max = new Vector2Int(Mathf.RoundToInt(Mathf.Max(PreBLx, PreTRx)), Mathf.RoundToInt(Mathf.Max(PreBLy, PreTRy)));

                        Vector2Int SpriteDimensions = new Vector2Int(Mathf.Abs(Max.x - Min.x) + 1, Mathf.Abs(Max.y - Min.y) + 1);

                        Texture2D texture = new Texture2D(SpriteDimensions.x, SpriteDimensions.y);

                        if (Min.x < 0 || Min.y < 0 || Min.x + SpriteDimensions.x - 1 >= mainTexture.width || Min.y + SpriteDimensions.y - 1 >= mainTexture.height)
                        {
                            continue;
                        }

                        var test = mainTexture.GetPixels(Min.x, Min.y, SpriteDimensions.x, SpriteDimensions.y);

                        texture.SetPixels(test);


                        switch (orientation)
                        {
                        case Orientation.Up:
                            texture.Rotate(RotationType.None);
                            break;

                        case Orientation.Right:
                            texture.Rotate(RotationType.Right);
                            break;

                        case Orientation.Down:
                            texture.Rotate(RotationType.HalfFullRotation);
                            break;

                        case Orientation.Left:
                            texture.Rotate(RotationType.Left);
                            break;

                        default:
                            break;
                        }


                        if (sprite.Flipped)
                        {
                            TextureUtilities.FlipHorizontally(texture);
                        }
                        texture.name = sprite.Name;

                        var worldSize = GetWorldSize(sprite.Positions);

                        //TODO
                        yield return(new tk2DSpriteData()
                        {
                            Name = sprite.Name,
                            Pivot = GetPivot(sprite.Positions),
                            Texture = texture,
                            UVDimensions = new Vector2Int(PreWidth, PreHeight),
                            PixelsPerUnit = texture.width / worldSize.x,
                            SpriteCoords = new Rect(Min.x, Min.y, Max.x - Min.x + 1, Max.y - Min.y + 1)
                        });
                    }
                }
            }
        }