Texture Packer + Unity Tutorial

This is a tutorial on how to use the TexturePacker extension for Unity. There are two different versions of TexturePacker – The stand alone TexturePacker, and the TexturePacker Unity Extension. Both have a limited function free version in addition to the full version.

To get started as quick as possible, here is Video Tutorials Section (will be upgraded shortly)

Basicly tutorial is splitted in 3 parts.

  • Introduction
  • Using with 3D object’s (animation, automatic UV)
  • Using with GUI (drawing GUI textures from Atlas, GUI Animation)

Introduction.

Why you should use texture atlases in your game:

  • Reduced build size
  • Reduced RAM consumption
  • Increased performance

Why use TexturePacker?
TexturePacker will allow you to easily and quickly create texture atlases for your game.

  • Rotation
  • Trimming / cropping
  • Scaling
  • Smoothing
  • Note: Some features are not available in the free version

Learn More here:

Why use the TexturePacker Unity Extension?

Unity GUI

  • Draw textures from the atlas
  • Supports TexturePacker features (rotation. crop, trim)
  • GUI animations

3D object

  • Auto generation of UV coordinates for Meshes
  • Rotated images for optimal space
  • Play animations from atlas
  • Preview in editor mode
  • Note: Some features are not available in the free version

Using with 3D objects

TexturePacker is available for download here:

The TexturePacker Unity extension is available here:
Free

Paid

Creating the Texture Atlas:

For the first tutorial, we’ll pack textures for our scene (Assets are available in the TexturePacker Extension package under Assets/Extensions/TexturePacker/Tutorial/Art/AtlasSource).

Open TexturePacker, and simply drag the textures inside. Now let’s configure our atlas.

Output Options
Data Format: Unity3D
Data File: YourProject/Assets/Resources/TutorialAtlas_data.txt Texture File: YourProject/Assets/Resources/TutorialAtlas.png
Texture Format: png

Geometry Options
Check the “Force Squared” toggle

Layout Options
Algorithm: MaxRect (Full version only) or Basic (available in the free version)
Allow Rotation: checked (Full version only) unchecked (free version)
Trim mode: none (Trim mode is only supported for GUI rendering)

Note
I has described most optimal options, but you can experiment with TexturePacker option to get the best result for your atlas.

Next, press the Publish button. Your altas should look like this:

Free version:


If you do not see the image, you can view the image by clicking directly on the URL

Paid version:


If you do not see the image, you can view the image by clicking directly on the URL

Atlas is ready, so we need material for it.

In Resources Folder Create->Material. Set material name as TutorialAtlasMaterial, attach TutorialAtlas.png as main material texture and change shader to Transparent->Diffuse.

*Note: The material will be created automatically by Asset Processor editor script. But you can create any number of other materials with different shader for atlas.

Using the texture atlas with a model:

Using the texture atlas with a model:
(Assets/Extensions/TexturePacker/Tutorial/Art/Models/Sword/sword_01)

It already has a texture and a material assigned. Replace this with a texture from the atlas:

  • Select the sword_01 gameobject (the one with the MeshRenderer component attached).

  • Attach TPMeshTextureEx component (TPHelper component will be added automaticly)

  • Choose Atlas and Texture in TPHelper menu

  • Atlas: TutorialAtlas

  • Texture: sword_01_dif

1261817--61408--$13 3-07 AM.png

If you do not see the image, you can view the image by clicking directly on the URL

The sword is now using the texture from the atlas.

*Note: Do not attach TPMeshTexture component, it will casue component lost if you replace TexturePacker.dll. Use it only for extending.

Using the texture atlas with skinned model
The usage is similar to a simple model.

  • Select the sword_01 gameobject (the one with the SkinnedMeshRenderer component attached).
  • Attach TPSkinnedMeshTextureEx component (TPHelper component will be added automaticly)
  • Choose Atlas and Texture in TPHelper menu

Play an animation from the atlas:
This approach is only useful if you want play animation on YOUR mesh (not simple plane). Trim / Crop is not supported with this approach. If you just want to play animation on plane from atlas, please follow Creating Sprite Animation steps. It has additional features implemented as:

  • Trim / Crop support
  • Animation can contain frames from different atlases.

For the next tutorial, we will play an animation from the texture atlas on a cube:

  • Create a cube in the scene by selecting GameObhect->Create Other->Cube

  • Change the material on the cube to TutorialAtlasMaterial

  • Attach a TPMeshAnimation component

  • Configure the TPMeshAnimation component options as follows:

  • Atlas: TutorialAtlas

  • Frames: add 6 frames fireball_0001, fireball_0002, fireball_0003, fireball_0004, fireball_0005, fireball_0006

The component should look like this:
1261817--55432--$anim.png
If you do not see the image, you can view the image by clicking directly on the URL

Now press play and you will see fireball animation playing on the cube. You can try other meshes and experiment with different TPMeshAnimation options.

You always can automate this process using extensions, there are a few examples of how do this inside the package. See the MyFireAnimation, MyTeslaAnimation and MyMeshTexture classes.

The complete scene can be found here:Assets/Extensions/TexturePacker/Tutorial/Scenes/AtlasesAnim

The demo scene uses only one atlas. The entire scene with almost 100 objects and 50 animations has only 1 Draw Call, 1 Texture, and uses only 1MB of memory.

You can view a preview of the scene in the web player here:
https://dl.dropboxusercontent.com/u/83133800/TexturePacker/3D/build.html

Screenshot:
1261817--55433--$preview.png

Optimization.

There are further optimizations that can be done.
The scene uses only one texture for the whole scene - already good benefit - but we can squeeze even more. The size of our texture is 754x754, if we had choose RGBA 32 compression it would have consumed 2.2 MB. But setting the compression to PVRTC and stretching the texture to 1024 * 1024 (compression works only with POV2 textures) changed the memory consumption to only 1MB. With just a little quality loss, it can be scaled down to 512x512, reducing memory consumption to only 256KB.
The POV2 TexturePacker options allow you to always see how many textures you can add to your atlas.

Using with GUI

For the next tutorial, we will use the same atlas from our 3D scene, But you can always experiment and create new one, using the source GUI textures which are located under Assets/Extensions/TexturePacker/Tutorial/Art/AtlasSource.

The difference between creating a texture for a 3D object and for use in the GUI is that for GUI atlases you can use the Trim / Crop TexturePacker methods to save even more space (this option is available in paid version of TexturePacker)

First, we need to create our GUI drawing class. And assign it to a GameObject.
Here is an overview of some useful snippets for working with the GUI part of the TexturePacker extension.

Getting the Atlas:

TPackManager.getAtlas(MyAtlasName);

Getting the Texture:

TPAtlas atlas = TPackManager.getAtlas(MyAtlasName);
TPAtlasTexture texture =  atlas.getTexture("myTextureName");

Or

TPAtlasTexture texture = TPackManager.getAtlas(MyAtlasName).getPngTexture("myTextureName");

Drawing a texture:

void OnGUI() {
    TPackManager.draw(Rect rect, string atlas, string textureName);
}

AtlasGUIExample – an Example GUI class to draw textures from the atlas:

public class AtlasGUIExample : MonoBehaviour {

    private TPAtlasTexture play;
   
    private Texture2D unityTexture;
   
   
    void Awake() {
        play = TPackManager.getAtlas(Atlases.EXAMPLE).getPngTexture("play");
       
        //Warning to Get Texture2D from atlas, should remain the same size which was generated,
        //that's why we using another atlas here

        unityTexture = TPackManager.getAtlas(Atlases.EXAMPLE2).getUnityTexture("play");
    }
   
   
   
    void OnGUI() {
        play.draw(new Rect(0, 0, play.width * 0.5f, play.height * 0.5f));
       
        play.draw(new Rect(0, 75, play.width, play.height));
       
        GUI.DrawTexture(new Rect(0, 225, unityTexture.width, unityTexture.height ), unityTexture);
       
       
       
        TPackManager.draw(new Rect(200, 0, 122, 42), Atlases.EXAMPLE, "f_share.png");
        TPackManager.getAtlas(Atlases.EXAMPLE).draw(new Rect(200, 100, 122, 42), "t_share.png");
       
       
    }

AtlasGUIAnimationExample – An example GUI Animation class - (GUI animation is not available in the free version). Using the same atlas as for our 3D scene above.

using UnityEngine;
using System.Collections;

public class AtlasGUIAnimationExample : MonoBehaviour {

   
    private TPGUIAnimation anim;
    private TPGUIAnimation Scaledanim;
   
   
    void Awake() {
        anim        =  TPGUIAnimation.Create();
        Scaledanim  =  TPGUIAnimation.Create();
       
        for(int i = 1; i < 7; i++) {
            TPAtlasTexture frame = TPackManager.getAtlas(Atlases.EXAMPLE).getPngTexture("fireball_000" + i.ToString());
            anim.addFrame(frame);
            Scaledanim.addFrame(frame);
        }
       
        anim.pos = new Vector2(200, 200);
        anim.loop = true;
        anim.fps = 25;
        anim.Play();
       
        Scaledanim.pos = new Vector2(300, 200);
        Scaledanim.loop = true;
        Scaledanim.fps = 25;
        Scaledanim.scale = 0.5f;
        Scaledanim.Play();
       
       
       
    }
   
    void OnGUI() {
        anim.draw();
       
        Scaledanim.draw();
    }
}

You can view this here in the web player:
https://dl.dropboxusercontent.com/u/83133800/TexturePacker/previewGUI/previewGUI.html

In conclusion, here are some of the advantages you will get from using TexturePacker:

  • Fast and easy atlas generation
  • Easy to remove and/or add texture from the atlas
  • Built in animation solution
  • Reduced build size
  • Reduced RAM consumption
  • Increased performance

Special Thanks:

NassacGames for free 6 Medieval Shields pack package, it was used
for building graphic part.

Jaimi, eskimojoe for help with this tutorial.

If you have any comments, questions or suggestions, feel free contact support team via e-mail: stans.assets@gmail.com

I hope you enjoyed the tutorial and good luck with your game :slight_smile:

  • I do not see my atlas in TPHelper drop down menu.

Your atlas wasn’t registered by TPAssetProcessor.

  • Did you put atlas file inside Resources folder?

  • Is data file has “_data.txt” postfix?

  • Did you use Unity3D format when generating atlas in Texture Packer?

  • If you steel have the same issue, please contact me by e-mail.

  • I have big performance hit when instantiating character with TPSkinnedMeshTextureEx or MeshTextureEx attached, how to fix this?

It happens because script creates new mesh with recalculated UV on Awake. That makes you available to use same mesh for different textures. But this kind of behavior can cause performance hit on object Instantiate. Problem is mostly actual for big meshes, characters for example.
You can fix this by checking “Use Shared Mesh” on TextureMesh component. It will remove mesh recreation and UV calculation, but you would not able to use this mesh for different atlas texture.

  • I’ve create second material for my atlas with another shader, but I can not assigned it to the model.

Uncheck “Replace Material” on TPHelper component. Now you able to assign any material to your model.

After importing your tools, is it possible to move them to another location?

You can move it anywhere you what to. It absolutely not place related.
The only one exception is Asset/Plugins folder. Unity will not look for Editor folder there. If you want to place this tool inside Asset/Plugins folder, then you have to move Editor folder of TexturePacker in any other place.

I try to replace a texture in TpMeshTextureEx on the Play mode , it just switches variables(tx/texture) but it does Not refreshes The visible texture/result .

After you change texture name or path to the atlas you should call applayUV function of TPMeshTexture component. It will recalculate UV and you will get visible result.

Hi - cheers for making such a useful extension. I thought I’d let you know about a bug I found and how I fixed it. My game’s platform textures go into an atlas via Texture Packer. I use the same mesh for three platforms of different sizes - ranging from 10x10x10 meters to 30x10x30 meters. I use non-uniform scale on the 30x10x30 game object to enlarge it - scale values X3, Y1, Z3.

I discovered that that messed with the UVs, causing the textures to get misaligned on each surface.

This was happening because I applied the non-uniform scale on the game object that contained the mesh and your texture packer components. So I changed it to being X1, Y1, Z1 and made it a child of another game object that had the X3, Y1, Z3 scale values. The UV properties were now fine, the mesh was extended to the right dimensions and all was good.

Let me know if that was unclear - I can upload pictures and further explain.

I’m having trouble getting this to work at all. I’m pretty sure I’m following the steps, but all I end up with is flat shading on the models.

Any ideas what I might be doing wrong?

Okay… exited Unity, relaunched, and everything is now fine. Very odd, but working :slight_smile:

Hi SteveJ, Thanks for sharing your experience. I assume that atlas was changed from external source withour reimport.

Let me explain little bit more.

  • After Atlas was imported, frames data was cashed.
  • Atlas was changed from external source, but frames data wasn’t recalculated
  • After you exited Unity (or just triggered asset reimport by any other action, modifying script fro example ) frame data was recalculated.

This is very rare but know issue and it already fixed in upcoming version. Texture Packer 2.0 already submitted.

Few spoilers about version 2.0

  • It will have a Editor windows and custom inspectors
  • Solution for “on plane” animation with Trim/Crop and An
  • You will be able to preview animation in editor mode.

I’ll do full announce as soon as it will be available.

Edit: This issue has been solved and was not due to TexturePacker.

Hi lacost, thanks for the tutorial. I am hoping you can help me out with an issue I am having with achieving pixel perfect renders in a simple game. My problem seems to be that in play mode I lose the top line of pixels on my plane. I think it is best to just show you what is going on. This is a basic tile I created in Photoshop followed by the same tile taken from the atlas texture that TP created.

1334734--64479--$BaseTile.png1334734--64479--$BaseTile.png

Both are the same. My camera is set to orthographic, size 320, and my game aspect is set to 960x640. I have created a basic 2 tri mesh that is 64 units by 64 units and has no material. (The rightmost object in pics) I then duplicated that mesh and added the TPMeshTextureEx script and picked the Atlas texture and the corresponding png from the texture drop down list. (The middle object in pics) I also created a primitive cube and scaled it to (64,64,1). (The leftmost object in pics) The following is a screen capture from the game view in Unity.

1334734--64481--$PlayTile.png

I spaced the two planes and cube so there is one unit in between them. You may have to zoom in your browser to get a better look but the middle tile is short one row of pixels at the top. The following is a screen capture of the exact same setup in the editor view.

1334734--64482--$EditorTile.png

You can see the one pixel or one unit offset in between the tiles, but in the editor I am not having a problem with missing the top pixel row. In the texture settings for the atlas texture I have turned off Generate Mip Maps and have used both Clamp and Repeat for Wrap Mode, and I have the Filter Mode set to Point. I have played around with various Max Size and Format settings as well without being able to solve my problem. I am using the built-in Unlit/Transparent shader on the middle tile, the basic primitive shader on the leftmost tile and obviously no shader on the rightmost tile.

I am at a loss as to how to fix this. My best guess is that it has to do with the UV’s, and maybe precision due to float values, but I am not sure.

Any assistance or insight or information you can provide me that may assist me with wrapping my brain around this would be much appreciated. Thanks… :slight_smile:

Hi, Pardue.
I need closer look to understand what you trying to achieve, and provide some help.
Pm sent.

So my issue has nothing to do with TexturePacker as I can create the same issue without TexturePacker. I also don’t seem to have the issue on my other computer and I found the issue goes away if I move the tiles up and over .5 units. Thanks lacost for all your assistance, the support with TexturePacker has been outstanding!

Animation in Texture Packer 2.0

2.0 version contains more advanced function for sprite render and sprite animation. Main features:

  • Trim / Crop, Rotation and Scale support
  • Full Editor integration
  • You can use frames for one animation from different atlases
  • Animation preview in editor
  • Draw calls batching

Note: This is “on mesh” animation, it doesn’t use Unity GUI system. To play animation using Unity GUI, please read about plugin GUI classes.

Creating Sprite Animation

You can use your or tutorial atlas to create 2D sprite animation.
Go to the
Game Object → Create Other → Texture Packer → Sprite Animation
This will create new TP Sprite Animation object.
Now you can add some frame to the animation. Press Add button. Add button will call Texture Packer custom window or bring it up, if you already have it.

Note: For opening Texture Packer custom window go to the Windows-> Texture Packer.
1336362--64747--$skitch.png
If you do not see the image, you can view the image by clicking directly on the URL

In Texture Packer window, just select frames for your animation. You can use Ctrl/Cmd and Shift button to easily select frames you want for this animation. After frames are selected press “Add to animation” button.
Note: You can add more frames from different atlas (just switch) atlas in Texture Packer window and add another frames. And of course you can add the same frames multiple times to the animation if you want to.


If you do not see the image, you can view the image by clicking directly on the URL


If you do not see the image, you can view the image by clicking directly on the URL

That is it. That is all you have to do to set up the sprite animation from atlas.

Creating Simple Sprite

That is useful if you want atlas texture to be rendered on simple plane Mesh, or if you want trim support. Go to
Game Object → Create Other → Texture Packer → Sprite Texture
And follow the same steps as form Creating Sprite Animation.

Texture Packer Window


If you do not see the image, you can view the image by clicking directly on the URL

  • Atlas choose drop down. Use it to switch between atlases in your project.
  • Adds selected frames to the selected sprite animation.
  • Toggle between display or not display frames extension.
  • Search field. Start typing in this search field to filter frames result
  • Cancel search. Press this button to disable filter and clear search field.

Sprite Animation Inspector

1336362--64767--$skitch (4).png
If you do not see the image, you can view the image by clicking directly on the URL

  • Info Box. It will display useful information about the animation

  • Animation settings:

  • Gizmos Color. Color of the box around animation.

  • Current Frame. Current frame of animation which is displayed in editor. When you press play animation will start playing from this frame.

  • Frame Rate. Frame rate of selected animation.

  • Play On Start. If this option is active, animation will start playing when animation game object will receive Unity Start event.

  • Loop. If the options is active, animation will play a round.

  • Force Selection. When you selecting animation by clicking on it in the editor scene, Game object with TPSpriteAnimation component will be selected instead animation sprite. To disable this behaviour uncheck this select box.

  • Control buttons:

  • Add. Button will open Texture Packer window, or bring it up if already opened.

  • Clear. Clear all animation frames.

  • Update. Update animation view. Can be useful if something goes wrong, and view wasn’t updated automatically.

Optimization tips
Last Optimization section was about RAM and Texture Atlases size saving. Let’s talk about CPU. One of the main features of this extension is Draw Calls saving. This is possible thanks Unity Dynamic batching.
When you creating different animation they use the same material with make dynamic batching possible. To use it with the full power you should understand how the dynamic batching work, and render part of this extension work.

When the sprite animation is running it applying new scale and local position to the sprite every frame. It means that TP Sprite is always non-uniform object. With means it can be batched with other non-uniform objects, but if you will use the same material on object with uniform scale it will cause 1 additional draw call.

Using frames for different atlas is a great feature. But you should understand that animation sprite will switch materials while playing in this case. Every additional material on scene this is additional draw call.
Learn more about batching.

This looks very interesting. A basic YouTube style video tutorial would be a mighty nice thing.

Yea, you right. I am working on the video, it should be available during next few weeks. Thanks for the interest.

where is the register point ?is topleft(0,0) or bottommid(width/2,height)?
can I change the register point?

Sprite Registration point is topleft(0,0)

It depends what you trying to achieve by changing the registration point.
There is no current functionality with allow to change sprite pivot. But I can easily add such feature.
Any way now you change change pivot point by parenting to another object. Like with any other game-object.
Lem me know if you need more explanations.
Cheers!

thx,good work,I hope the developer can record a video tutorial, and then upload youtube, this is just suggestions, hoping not to waste too much time developers

Hello,

First of all thank you for this asset. I have some questions about the tutorial though.

The beginning part I can’t get to work (or I don’t understand). You say this: "For the first tutorial, we’ll pack textures for our scene (Assets are available in the TexturePacker Extension package under Assets/Extensions/TexturePacker/Tutorial/Art/AtlasSource).

Open TexturePacker, and simply drag the textures inside. Now let’s configure our atlas."

But I opened up the Texture Packer window, and tried dragging some textures onto the window, and nothing happened. I also tried placing some textures inside the Assets/Extensions/TexturePacker/Tutorial/Art/AtlasSource folder, and then opening up the Texture Packer window, and again my textures did not show up?

I don’t know what I’m doing wrong. Thanks for any help.

Hello Velo222.

Here is little misunderstanding I guess. I ment Texture Packer program. This is aaset working with texture atlases created via TexturePacker program. You have to create atlas in TexturePacker program (not TexturePacker Unity Extension) and then start work with extension.
Here is what I mean by this step.

If you have any other questions, please fell free to contact me via PM or e-mail.

Ohhh okay. Thank you :slight_smile:

Sorry, I was confused.

Hi,
Can we use this on meshes using tiled textures !.

Hello, sathya. Sorry for slow replay.
Can you explain more what are you mean by this.