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) }); } } } }