TK 2 D Manual
TK 2 D Manual
Version 1.06
Table of Contents
2D Toolkit..................................................................................................................................................... 3 Getting Started............................................................................................................................................. 4 Sprite Collections.................................................................................................................................... 4 Sprites.................................................................................................................................................... 4 Static Sprite Batcher............................................................................................................................... 4 Sprite Animations.................................................................................................................................... 4 Animated Sprites.................................................................................................................................... 5 Fonts....................................................................................................................................................... 5 TextMeshes............................................................................................................................................ 5 Everything in detail...................................................................................................................................... 6 Sprite Collection...................................................................................................................................... 6 Sprite...................................................................................................................................................... 7 Sprite Animation...................................................................................................................................... 8 Animated Sprite...................................................................................................................................... 8 Font........................................................................................................................................................ 9 TextMesh................................................................................................................................................ 9 Hints and Tips............................................................................................................................................ 10 Pixel perfect sprites and fonts............................................................................................................... 10 Positioning sprites................................................................................................................................. 10 Multiple copies of the same sprite......................................................................................................... 10 Changing the default sprite material..................................................................................................... 10 The textures are being padded too much.............................................................................................. 11 Help! My textures are blocky and look compressed..............................................................................11 I don't like your implementation of Sprites / TextMeshes. I'd like to write my own.................................11 How much performance do all the options cost?..................................................................................11 I have a frame rate spike when I instantiate a large number of sprites.................................................11 How do I create a Sprite in code?......................................................................................................... 11 I'd like to switch sprites by name. The API seems awfully lacking.........................................................11 I have created a texture which is 1024x1024, why is my atlas 2048x2048?.........................................12 I am setting .text and .color on my tk2dTextMesh, and nothing is changing. What gives?....................12 API............................................................................................................................................................. 13 tk2dSprite............................................................................................................................................. 13 tk2dAnimatedSprite.............................................................................................................................. 13 tk2dTextMesh........................................................................................................................................ 13
2D Toolkit.
Thank you for purchasing 2D Toolkit. 2D Toolkit is a set of tools to provide an efficient 2D sprite and text system which integrates seamlessly into the Unity environment. Among the features provided in 2D Toolkit are: Sprites Fire and forget atlasing system. Set it up once, and it will be automatically regenerated when it needs to be. Automatically cut up sprites, eliminates empty space in textures and saves altas area and fill-rate. Supports arbitrarily sized sprites. Set up anchor points so your sprites rotate at the right points, and to line up animated sprites. Supports pixel perfect rendering on orthographic and perspective cameras. Efficient batching system for background static sprites. Create combined static sprite meshes seamlessly and efficiently. Scale sprites without breaking dynamic batching. As much data as possible is precomputed offline, runtime transformations are kept to a minimum. User friendly editor, creating and using sprites has never been simpler! Sprite Animations Easily build large libraries of sprite animations for your projects. Supports arbitrarily sized sprites for animation. Sprites for animations can be sourced from any number of atlases. Easy to use in-game API. Text Use bitmap fonts created in BMFont / Hiero. Tint and scale your text meshes without breaking dynamic batching. Supports vertical gradients.
Sprites.
1. Create a sprite by clicking on Create > tk2d > Sprite in the Hierarchy Window. 2. Your sprite now appears in the Hierarchy Window and in the viewport. It automatically picks a sprite id based on the information that is available. 3. Click on the sprite object in the Scene Tab or in the Hierarchy Window, and the Sprite editor inspector appears.
Sprite Animations.
Sprite animations hold a collection of named sprite animations which you can set up in the interface, or instantiate from script. 1. Create a sprite animation collection by clicking on Create > tk2d > Sprite Animation in the Project Window. 2. Click on the Add clip button to add an animation clip. 3. Set up the number of frames, and select your sprites for the animations. 4. Set up the wrap mode.
Animated Sprites.
An animated sprite is a special type of sprite which can play animations. 1. Create an animated sprite by clicking on Create > tk2d > Animated Sprite in the Hierarchy Window. 2. Select the appropriate default sprite by expanding the sprite foldout. 3. Select a default animation, and if necessary, tick the Play Automatically box to have the animation start playing as soon as the game starts. This is really convenient for looping background animations.
Fonts.
A Font imports a text BMFont exported from the excellent BMFont program (http://www.angelcode.com/products/bmfont/). You can also use Hiero (http://slick.cokeandcode.com/demos/hiero.jnlp) on Mac or Windows if you prefer. 2d Toolkit supports both xml and text font formats. The only limitation is that it only supports a single page of textures. The maximum ASCII character imported is set to 128 by default. You can increase this on the Font object if additional characters are necessary. For best results, use the following settings: BMFont Export options: 32 bit, Channels A outline, RGB one (no outline) or glyph (when an outline has been set up). Hiero in Gylph Cache, Set page width and height so you end up with 1 page exactly. Increase padding around characters. 1. Create a Font by clicking on Create > tk2d > Font in the Project Window. This is ideally done in the same directory the font was exported to in the previous stage. 2. Create a new material with using the texture associated with the BMFont. 3. Assign the xml / text font, texture and material to the Font object. 4. Click Commit.
TextMeshes.
A TextMesh draws a custom string using a selected Font. 1. Create a TextMesh by clicking on Create > tk2d > TextMesh in the Hierarchy Window. 2. Select how many characters you wish to display on this TextMesh. Your string will be truncated if it exceeds this limit.
Congratulations, you have now mastered the basics of the 2d toolkit! Read on for some more advanced uses.
Name Name your sprites so you can easily find them. By default, they inherit the name of the source texture, but you may have multiple textures with the same name. You can also use the syntax category1/category2/name to get the drop down to group names together. Additive When ticked, the sprite is treat as additive. This is useful for glow sprites and particles. Scale A default scale to apply to the sprite. Anchor Selects the default anchor point for the sprite. When the anchor point is set to Custom, you will be able to edit the actual anchor points. The anchor point will be represented by a flickering dot on the preview pane. Dice Ticking this will allow you to dice your texture. This will create multiple polygons from your sprite and tightly pack them in the texture sheet, allowing you to use large sparse textures. Set up the dice width and height, while keeping an eye on the vertices and triangle count in the preview pane. Dicing a texture too much could result in a lot of polygon wastage. Pad Override the default padding when you see padding related errors on your sprite. The options available are BlackZeroAlpha which is useful for when representing antialiased sprites, and Extend, which is useful for tightly packed tiles.
Sprite
Collection & Sprite Select a sprite collection, and a sprite from within it. Color This color is applied to the vertex colors, so you can use this in your custom shaders to do various things. The provided shaders will tint the texture by the vertex colors. Scale Change the scale on the sprite. This is the preferred way to change scale on your sprite, as opposed to changing the local scale on the transform. Changing scale here will not break dynamic batching. Make Pixel Perfect Clicking this will make the sprite draw pixel perfect for the current main camera, or if available, based on the Pixel Perfect Helper. If Always is selected, this sprite will resize itself to display 1:1 on startup. This will change the scale parameter.
Sprite Animation
Clips Lists the currently available animation clips. + adds a new clip and removes the currently selected one. Name You should give your animation clips unique names. Num Frames - Number of frames in the current animation clip. Frame Rate The default framerate for the clip. Wrap Mode Options are: Loop The animation is looped indefinitely. LoopSection A part of the animation is played once and a second section is looped indefinitely. Once The animation plays once and then stops on the last frame. PingPong The animation sequence is played forward, then in reverse. The last frames are only played once. Eg. 0 1 2 3 2 1 - Single Simply switches to this sprite. This is useful for continuity in your code, and also for placeholders before you have any animations set up. Reverse - Reverses all the frames in the currently selected clip. AutoFill Looks at the last frame, and looks for following sprite Ids. Eg. If your last frame is spritesheet/1, it will look for spritesheet/2, 3, 4 and so on until it can't find any more ids. V/H changes display direction of frames. Trigger When set, this frame will fire the FrameEvent callback. Trigger info / float / int Values to use as reference in the callback.
Animated Sprite
The Sprite foldout displays a default sprite inspector when expanded. You can use this to set up a default sprite to display. AnimLib Selects an animation library Clip Selects a clip from the selected animation library Play automatically Plays this clip automatically on start up.
Font
BMFont Drop your BMFont here. The system supports both text and xml BMFonts. Material Default material used by this BMFont. Texture Texture created by BMFont. Dupe Caps Tick this when you only have caps or lower case characters exported in your BMFont. This is useful when you want to maximize texture space, and don't need lower case / upper case characters. Flip Texture Y Textures generated in Hiero need to be flipped. Num Characters Total number of characters to import. For example, if you do not need a full ASCII set, you can set it to 128. Target Height and Target Ortho Size - See Hints and Tips > Pixel Perfect Sprites and Fonts. Commit Commits changes to this Font object.
TextMesh
Font Select a font from the project. Max Chars Number of character to allocate memory for. The TextMesh will not display more than this number of characters. It is not a good idea to change this while the game is running. Text Default text Anchor Anchor point for the text Kerning Enable or disable kerning on this TextMesh. It is a lot quicker to have kerning disabled, so only turn it on when really needed. Scale Scale the text mesh without breaking instancing. Make Pixel Perfect - Clicking this will make the sprite draw pixel perfect for the current main camera, or if available, based on the Pixel Perfect Helper. If Always is selected, this sprite will resize itself to display 1:1 on startup. This will change the scale parameter. Use Gradient and Colors Color your text, or apply a vertical gradient on it.
Target Height
Target device pixel height. For example, if your target resolution is 960x640, your Target Height should be 640. Target ortho size (as set up on camera). This corresponds directly to the Size parameter on your camera when Projection is set to Orthographic. Leave it at 1 when using Perspective cameras.
By default, the sprites are created targeting a 640 high device (i.e. iPhone4 in landscape mode), with an ortho camera with an ortho size of 1.0. This should work perfectly even when switching from 960x640 480x320 as on the iPhone 3g & iPhone 3gs, as long as the texture is mip-mapped. However, when designing an universal app for iPad in addition to the iPhone, the iPad version will not display pixel perfect automatically. This can be rectified in a few different ways, this is generally dependent on the type of game you are developing. Set up an object with a PixelPerfectHelper behaviour in the game. Set the appropriate target resolution (height) in the box. Eg. When targetting iPad, set the height to 768 / 1024 depending on whether the device is held in landscape or portrait mode. Any objects with Always make pixel perfect ticked will scale appropriately to display 1:1 on startup. Adjust the camera to correct appropriately. Eg. To scale from iPhone4 to iPad, you'll need to scale up the ortho size by 1.2. Use a completely different set of assets targeting the iPad resolution.
Positioning sprites.
The entire Unity mesh positioning tools are available to you when positioning sprites. Try using vertex snap mode to get sprites aligned closely, for instance, to build a tiled background. You can also use any of the alignment tools you can get on the Asset Store.
I don't like your implementation of Sprites / TextMeshes. I'd like to write my own.
Sure! Feel free to do so. The data from the back end is available for you to consume in any way you wish. Check tk2dSprite.cs and tk2dTextMesh.cs on how the data is used. As almost everything is precomputed, you can almost use the data as is!
I'd like to switch sprites by name. The API seems awfully lacking.
This is by design. If you need to switch sprites by name, create a SpriteAnimation with as many clips as you need, each with a unique name. For each of these clips, set the wrap mode to Single and set up the sprite. You can now switch between sprites by calling Play(clipname). As an added bonus, if you need to convert any of these to animations or if you need to switch to sprites from multiple collections, you are all set up to go!
I am setting .text and .color on my tk2dTextMesh, and nothing is changing. What gives?
You need to call Commit on the textmesh for your changes to be committed. The reason for this is you may change text and color and/or size in the same frame, and each of these will invalidate the mesh data, and by calling Commit yourself, you are saving the system from regenerating data multiple times.
API.
Below are the most commonly used member functions and properties.
tk2dSprite
color spriteId Sets the color of a sprite Sets the spriteId. You can also use GetSpriteIdByName to get a spriteId by name. If you need to do this, it is better to use an Animated Sprite with clips set to Single Sets the scale on this sprite Sets the scale so the sprite displays pixel perfect. You will need to call this if you change the camera and need to update the sprite to display pixel perfect.
scale MakePixelPerfect()
tk2dAnimatedSprite
Play(id) Play(string) Stop animationCompleteDelegate Plays an animation clip by name or id. Stops a currently playing animation. Set up an animationCompleteDelegate to receive notifications when an animation stops playing. This is very powerful when coupled with c# anonymous functions. Set up an animationEventDelegate to receive notifications when an animation frame has a trigger set.
animationEventDelegate
tk2dTextMesh
Commit() text color / color2 / useGradient Commits all changes. You will need to call this after changing an of the members listed below. Currently displayed text Color is the main color, or the top color when useGradient is enabled. Color2 is only used when useGradient is enabled, and represents the bottom color. Change the anchor point for this text mesh Change the scale of this text mesh
anchor scale