Hello Guest

Author Topic: Import from TexturePacker spritesheet  (Read 22970 times)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Import from TexturePacker spritesheet
« Reply #15 on: May 11, 2013, 03:26:15 pm »
I can't remember off hand how you replace the contents of a prefab without breaking the connection, but... you can work around this quite nicely by simply loading the prefab in, replacing the contents manually, and then SetDirty & commit.

Code: [Select]
if (System.IO.File.Exists(prefabPath)) {
tk2dSpriteCollectionData prefabObject = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;

prefabObject.spriteDefinitions = scd.spriteDefinitions;
prefabObject.materials = scd.materials;
prefabObject.spriteCollectionName = scd.spriteCollectionName;

EditorUtility.SetDirty(prefabObject);
AssetDatabase.SaveAssets();
}

Don't forget that you want to handle the material in the same way, i.e. don't keep overwriting it but rather if it exists, load it in, fix up, setdirty.

bitsofbas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
    • bitsofbas
Re: Import from TexturePacker spritesheet
« Reply #16 on: May 11, 2013, 07:12:21 pm »
Yup, much better now! I've updated the file from a couple of posts back.

Thanks!

bitsofbas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
    • bitsofbas
Re: Import from TexturePacker spritesheet
« Reply #17 on: May 12, 2013, 02:30:29 pm »
Oh noes! I thought everything was working just fine now, but I'm still getting errors when changing my TexturePacker atlas.

Most of the animations keep working, but their sprites have been changed. Others simply crash with this error:

Code: [Select]
IndexOutOfRangeException: Array index is out of range.
tk2dEditor.SpriteAnimationEditor.TimelineEditor.DrawFrameGroupEx (Rect r, .tk2dSpriteAnimationClip clip, tk2dEditor.SpriteAnimationEditor.FrameGroup fg, Boolean highlighted, Boolean showTime, Boolean playHighlight) (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteAnimationEditor/tk2dSpriteAnimationClipTimelineEditor.cs:525)
tk2dEditor.SpriteAnimationEditor.TimelineEditor.DrawFrameGroups (Int32 controlId, Rect frameGroupRect, .tk2dSpriteAnimationClip clip, System.Collections.Generic.List`1 frameGroups, Single clipTimeMarker) (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteAnimationEditor/tk2dSpriteAnimationClipTimelineEditor.cs:211)
tk2dEditor.SpriteAnimationEditor.TimelineEditor.Draw (Int32 windowWidth, .tk2dSpriteAnimationClip clip, System.Collections.Generic.List`1 frameGroups, Single clipTimeMarker) (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteAnimationEditor/tk2dSpriteAnimationClipTimelineEditor.cs:158)
tk2dEditor.SpriteAnimationEditor.ClipEditor.Draw (Int32 windowWidth) (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteAnimationEditor/tk2dSpriteAnimationClipEditor.cs:564)
tk2dSpriteAnimationEditorPopup.OnGUI () (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteAnimationEditor/tk2dSpriteAnimationEditorPopup.cs:480)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)

The changed atlas sprites are not part of any animation, but they obviously do change the order of the sprite atlas data ( looks like TexturePacker exports the sprites alphabetically, so adding a new sprite starting with "a" changes the positions of all other sprites ) It's as if the animation is keeping track of the sprites by sequential index instead of by name. If that makes sense.

Any ideas what could be happening here? Or what to do about it?

( Worst case scenario, I could use an additional file describing the contents of each animation and use that to build the Animation object from scratch every time I have to refresh. Skipping your Animation editor. )

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Import from TexturePacker spritesheet
« Reply #18 on: May 12, 2013, 03:08:17 pm »
2D Toolkit internally maintains indices for sprites. So, instead of referring to sprites by name, it refers to them by sprite collection - index. Works fine internally as we have full control over it but in your case, texture packer is reordering everything as it sorts stuff by name.

An easy solution to this, assuming you're already doing that replace prefab contents thing I suggested before -
Instead of replacing the spriteDefinitions array directly, fix it up by using name. So lets say you have A (old) and B (new).
1. Foreach B, find one with the same name in A, overwrite the index in A. (i.e. spritecollection.spriteDefinitions[indexA] = newColl.spriteDefinitions[indexB];
2. Foreach B, find ones which have been deleted in A, and clear them, setting name = ""; (that is sufficient).
3. Foreach B which doesn't exist in A, find an empty slot (name = ""), or insert a new one to the end of the list.

Keep the indicies constant, and everything should work correctly. You shouldn't need to fix up animations, but rather as I explained, keep the indicies intact.

Finnegan

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Import from TexturePacker spritesheet
« Reply #19 on: May 19, 2013, 08:45:21 pm »
Hi!

I'm moving to Unity and 2D Toolkit from cocos2d-iPhone, so I have a TexturePacker license.

Will the use of textures increase performance of my games? Or should I just import individual sprites?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Import from TexturePacker spritesheet
« Reply #20 on: May 19, 2013, 08:54:57 pm »
You can import individual sprites if you want, that is no problem at all - 2D Toolkit will atlas your sprites. Importing texturepacker atlases in as assets, as opposed to at runtime, isn't officially supported at the moment.

pxlweaver

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Import from TexturePacker spritesheet
« Reply #21 on: May 27, 2013, 06:56:00 pm »
Bitsofbas' script works great - the only thing I'm stuck on is trying to see if I can generate colliders for a Texture Packer atlas. Any ideas?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Import from TexturePacker spritesheet
« Reply #22 on: May 27, 2013, 06:57:37 pm »
You'll have to create them manually, its not supported at this point. The current sprite collection editor doesn't support editing data on already laid out atlases. Its planned for a future update, together with some more advanced atlasing schemes, but its not as high priority as other features.