// Texture packer import public static tk2dSpriteCollectionData CreateFromTexturePacker( tk2dRuntime.SpriteCollectionSize spriteCollectionSize, string texturePackerFileContents, Texture texture ) { List<string> names = new List<string>(); List<Rect> rects = new List<Rect>(); List<Rect> trimRects = new List<Rect>(); List<Vector2> anchors = new List<Vector2>(); List<bool> rotated = new List<bool>(); int state = 0; System.IO.TextReader tr = new System.IO.StringReader(texturePackerFileContents); // tmp state bool entryRotated = false; bool entryTrimmed = false; string entryName = ""; Rect entryRect = new Rect(); Rect entryTrimData = new Rect(); Vector2 textureDimensions = Vector2.zero; Vector2 anchor = Vector2.zero; // gonna write a non-allocating parser for this one day. // all these substrings & splits can't be good // but should be a tiny bit better than an xml / json parser... string line = tr.ReadLine(); while (line != null) { if (line.Length > 0) { char cmd = line[0]; switch (state) { case 0: { switch (cmd) { case 'i': break; // ignore version field for now case 'w': textureDimensions.x = Int32.Parse(line.Substring(2)); break; case 'h': textureDimensions.y = Int32.Parse(line.Substring(2)); break; // total number of sprites would be ideal to statically allocate case '~': state++; break; } } break; case 1: { switch (cmd) { case 'n': entryName = line.Substring(2); break; case 'r': entryRotated = Int32.Parse(line.Substring(2)) == 1; break; case 's': { // sprite string[] tokens = line.Split(); entryRect.Set( Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4]) ); } break; case 'o': { // origin string[] tokens = line.Split(); entryTrimData.Set( Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4]) ); entryTrimmed = true; } break; case '~': { names.Add(entryName); rotated.Add(entryRotated); rects.Add(entryRect); if (!entryTrimmed) { // The entryRect dimensions will be the wrong way around if the sprite is rotated if (entryRotated) entryTrimData.Set(0, 0, entryRect.height, entryRect.width); else entryTrimData.Set(0, 0, entryRect.width, entryRect.height); } trimRects.Add(entryTrimData); anchor.Set( (int)(entryTrimData.width / 2), (int)(entryTrimData.height / 2) ); anchors.Add( anchor ); entryName = ""; entryTrimmed = false; entryRotated = false; } break; } } break; } } line = tr.ReadLine(); } return CreateFromTexture( texture, spriteCollectionSize, textureDimensions, names.ToArray(), rects.ToArray(), trimRects.ToArray(), anchors.ToArray(), rotated.ToArray() ); }
/// <summary> /// Create a sprite collection at runtime from a texture and user specified regions. /// Please ensure that names, regions & anchor arrays have same dimension. /// Use <see cref="tk2dBaseSprite.CreateFromTexture"/> if you need to create only one sprite from a texture. /// </summary> public static tk2dSpriteCollectionData CreateFromTexture(Texture2D texture, tk2dRuntime.SpriteCollectionSize size, string[] names, Rect[] regions, Vector2[] anchors) { return tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, names, regions, anchors); }
/// <summary> /// Create a sprite (and gameObject) displaying the region of the texture specified. /// Use <see cref="tk2dSpriteCollectionData.CreateFromTexture"/> if you need to create a sprite collection /// with multiple sprites. /// Convenience alias of tk2dBaseSprite.CreateFromTexture<tk2dSprite>(...) /// </summary> public static GameObject CreateFromTexture(Texture2D texture, tk2dRuntime.SpriteCollectionSize size, Rect region, Vector2 anchor) { return tk2dBaseSprite.CreateFromTexture<tk2dSprite>(texture, size, region, anchor); }
/// <summary> /// Create a sprite collection at runtime from a texturepacker exported file. /// Ensure this is exported using the "2D Toolkit" export mode in TexturePacker. /// You can find this exporter in Assets/TK2DROOT/tk2d/Goodies/TexturePacker/Exporter /// You can use also use this to load sprite collections at runtime. /// </summary> public static tk2dSpriteCollectionData CreateFromTexturePacker(tk2dRuntime.SpriteCollectionSize size, string texturePackerData, Texture texture) { return tk2dRuntime.SpriteCollectionGenerator.CreateFromTexturePacker(size, texturePackerData, texture); }