TexturePacker to Sprite

Texture Packer Importer

If you watch the video, you see, that if you are clicking on an atlas you have two options:

  • Process to Mesh
  • Process to Prefab

For the new introduced texture type “Sprite” in Unity 4.3 I wrote a third import option, which is called “Process to Sprites”.
Your find the script in the attachments ().

This will import sprites from your atlas file (which was generated by the Texture Packer), set the texture to texture type “sprite” and sprite mode to “multiple”.

Example:

Go to the atlas file → right click → TexturePacker → Process to Sprites

After the script has finished you will see something like this.
1453060--78665--$Result.PNG

1453060–79008–$TexturePackerImporter.unitypackage (16.9 KB)

Was baffled seeing this with no responses, but it just so happens you put it up a few hours ago. Just started getting into 2D today, carrying a whole bunch of TexturePacker atlases/sheets from my previous 2D work in Starling. Thank you for thinking of this!

One thing however, does the atlas need to be in a certain format for the menu items to become usable? I have XML (blech) atlases that I have no problem reconfiguring if it makes this work. Also, thanks for reminding me that I had a pro license for TexturePacker up until this past September!

Fake Edit: Just watched the video, and yeah JSON is most certainly the way to go. Will report back when I’ve made some progress. :slight_smile:

Yeah, I was also baffled that no one else has wrote it before…

The atlas must have following format:

{
	"frames": 
	{

		"frame1":
		{
			"frame": {"x":2,"y":2,"w":866,"h":718},
			"rotated": false,
			"trimmed": false,
			"spriteSourceSize": {"x":0,"y":0,"w":866,"h":718},
			"sourceSize": {"w":866,"h":718}
		},
		
		"frame2": { ... }, 
		...
	},
	"meta": 
	{
		"app": "http://www.codeandweb.com/texturepacker ",
		"version": "1.0",
		"image": "menuIcons.png",
		"format": "RGBA8888",
		"size": {"w":1024,"h":2048},
		"scale": "1",
		"smartupdate": ""
	}
}

TexturePacker creates this file for you.

If you have an other format from other programs you have to write your on parser. But this is not difficult, basically you have to do following:

		TextAsset txt = (TextAsset)Selection.activeObject;

		string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(txt));

		List<SpriteMetaData> sprites = TexturePacker.ProcessToSprites(txt.text);

		string path = rootPath + "/" + meta.image;
		TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;              
 
               //load texture, and assign sprites
		texImp.spritesheet = sprites.ToArray();
		texImp.textureType = TextureImporterType.Sprite;
		texImp.spriteImportMode =SpriteImportMode.Multiple;

		AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate );

Sprites is a list of SpiteMetaData. Here some pseudo code:

public static List<SpriteMetaData> ProcessToSprites(string text) {

.... parse XML ...

    List<SpriteMetaData> sprites = List<SpriteMetaData>();

    foreach (var sprite in xmlFile) {
        SpriteMetaData smd = new SpriteMetaData();
        smd.rect = new Rect(x,y,with,height);
        smd.alignment =  1;
        smd.name = name;
        smd.pivot = new Rect(x,y);

        sprites.Add(smd);
    } 

    return sprites;
}

That’s all :wink:

Hi

Just got my hands on TexturePacker as a beginner to all of this!

Tried to follow the instructions above but got the following error?

1455517–78974–$error.psd (1.15 MB)

thanks for the share , I wrote similar things but ended up not be able to edit directly on current made SpriteMetaData, so I ended up write on sprite metafile directly.

Look like create the list from scratch work

The screen shot tells me that the error occurs on this line

AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate );

Unity is trying to update the texture, but if there are some invalid values it throws an error. I was able to reproduce this exception by changing the width of a frame to a too large value in the atlas file.

"easy.png":
{
	"frame": {"x":2,"y":722,"w":3283,"h":286},  <------- Sprite is outside of the texture
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":283,"h":286},     
	"sourceSize": {"w":283,"h":286}
},

I updated the script, now it is checking if the sprite is inside the texture. If not, there will be a warning on the console.

Please update the script and try again! If it is still not working you can post your atlas file and I will have a look at it.

Thumbs up for this and the video!

I took this package and it’s predecessor and have created a repo on GitHub and have faked the history with the original commit by Mitch Thompson and then the contribution by Harald Lurger.

I plan to add some more tests as I believe their is at least one bug in the current implementation. I am not sure what is the best way to distribute the .unitypackage file, I don’t really feel like checking in the package itself. If you clone the repo you can run:

Hopefully having the package on GitHub will help other people to contribute. Also, who likes having to hunt through YouTube comments for the latest version of a plug-in. :smile:

https://github.com/haysclark/unity3d-texturepackerimporter

Thanks for getting that up n’ running :slight_smile: I’ve been a bit too busy to keep it updated heh. I’ll probably contribute a bit more before global gamejam 2014 starts - maybe even some sprite trimming/slicing like the sprite features in Unity Pro.

–mitch

No problem. :smile: I am making some minor tweaks to ‘Process > Sprites’. It loses the JSON files sorting so I am doing a post sort which should match the order that TexturePacker uses. Eventually I might add some more UnitTests. It’s a bit more complex then I realized to get C# working on Travis-CI, and almost all of Unity needs to be mocked which is a lot of work. :smile:

I think the only problem right now with the Toolset is that is that ‘Process > Sprites’ does not support all the features that ‘Process > Meshes’ and ‘Process > Prefabs’ support, so it’s a bit confusing to users.

So I’m back again :wink: Thank you to put the code on github. Great idea!

.unitypackage

The best way would to put it on the asset store, but I am not sure if we can create an account where every contributor can update the package.
So I think it is better to put it on github so I don’t have to update my original post always…

Missing features

Which features are you missing exactly? Currently I have a little bit of time over, so I could add some features.

I just found this today (getting into Unity’s 3D stuff) and this gem was a Godsend. Thanks for sharing it. Wonderful bit of utility.

Ever in NYC…beers are on me.
B.

Great work guys. I am new to unity and this helps a ton. Any chance of getting trim to work?

Thank you, everyone, especially OP.

I’m trying to use the importer, but i can’t import my sprites with the pivot point centered, it’s always “Top Left”.

I found this line of code:

smd.pivot = this.frame.center;

But for me it’s not working, or im doing something wrong :slight_smile:

How i can import my sprites with pivot centered??

Thanks

@Vitor_r
Hi, I wrote a pivot parser based on @Hatsch 's awesome share.
You can find it at the link GitHub - f15gdsy/TexturePackerImporter: The TexturePacker importer for Unity3d 4.3 or higher .
Hope it helps :slight_smile:

Ty man :slight_smile:
I will take a look…

I’m using the script from post#1, its working fine in editor but I’m getting errors on build:

Assets/TexturePacker/Plugins/TexturePacker.cs(15,7): error CS0246: The type or namespace name UnityEditor' could not be found. Are you missing a using directive or an assembly reference? Assets/TexturePacker/Plugins/TexturePacker.cs(217,28): error CS0246: The type or namespace name SpriteMetaData’ could not be found. Are you missing a using directive or an assembly reference?
Assets/TexturePacker/Plugins/TexturePacker.cs(162,24): error CS0246: The type or namespace name SpriteMetaData' could not be found. Are you missing a using directive or an assembly reference? Assets/TexturePacker/Plugins/TexturePacker.cs(15,7): error CS0246: The type or namespace name UnityEditor’ could not be found. Are you missing a using directive or an assembly reference?

Edit: ok, figured it out. I put it all inside another “Editor” Folder and all is good.

Building an unitypackage?
Also I downloaded the current github repository but I frankly do not know how to make an unitypackage out of that?

Sorry for being away for so long, I forgot to subscribe to this thread. I hope to do some investigation for both StrangeIoC and my version of the TexturePacker Importer and integrating them into the Unity Store. I don’t like the idea of filling up my repo with binary files like ‘unitypackage’; however, what might work well is exporting the binary to a GitHub Gist which user can easily download.

-h

hey guys, i used the texture packer import workflow and really liked the result until my graphic artist decided to delete some graphics from the atlas which were not used. The result, all SpriteRenderers lost the correct reference to the matching sprite because the Sprites are referenced by the id (order) in the multiatlas instead the name (what would make much more sense)

This makes the whole workflow somehow useless because you can never delete an graphic again and also adding new requires an distinct Sprite order with number prefix (01_, 02_, …)

Or did i missed something and there is a solution?