Hello Guest

Author Topic: Problem with using sprite thumbnail code  (Read 3528 times)

TDippingSauce

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Problem with using sprite thumbnail code
« on: December 11, 2014, 10:52:56 am »
Hi, I'm currently making my own tool for editing character's attack moves. The Editor Looks like this :


(Yeah, I extended and copy-pasted some parts of 2d toolkit editor codes to make this editor)


I wanted to preview selected frame(In the image, it's frame 0) of tk2dSpriteAnimationClip instance, but as you see the thumbnail box is empty.

This is code to draw thumbnail box :

Code: [Select]
public void Draw()
{
GUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(" ");

int tileSize = 128;
Rect r = GUILayoutUtility.GetRect(tileSize, tileSize, GUILayout.ExpandWidth(false));
tk2dGrid.Draw(r);
tk2dSpriteThumbnailCache.DrawSpriteTextureInRect(r, spriteDef, Color.white);


GUILayout.EndHorizontal();
}

...which is copy&pasted from tk2dSpriteEditor.DrawSpriteEditorGUI().

I inserted some logs to find out why, and realized when tk2dSpriteThumbnailCache.DrawSpriteTextureInRect is called, spriteDef.materialInst is null which causes this problem(maybe).

So this is how i get reference of tk2dSpriteDefinition :

1)Before Open the editor, drag&drop SpriteAnimation prefab i created. In the image, you can see "PlayerAnimation" on the right.

2)When you click "New Attack Move" button and set clip from EditorWindow.popup() (in the image, the clip is 'neutral'.), AttackMove class is instantiated and this instance holds reference of tk2dSpriteAnimationClip

3)Create frame list from tk2dSpriteAnimationClip.frames.

4)When a user clicks frame, check the index of frame and get tk2dSpriteAnimationFrame reference

5)tk2dSpriteDefinition spriteDef = currentFrame.spriteCollection.spriteDefinitions [currentFrame.spriteId];

6)use spriteDef to draw thumbnail image



I wrote as much detail as possible. Any missing steps here? Thanks for your help in advance!
« Last Edit: December 11, 2014, 11:02:15 am by TDippingSauce »

TDippingSauce

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Problem with using sprite thumbnail code
« Reply #1 on: December 11, 2014, 02:16:57 pm »
OK that was not a complex problem. I got tk2dSpriteDefinition reference by this :

Code: [Select]
spriteDef = currentFrame.spriteCollection.spriteDefinitions [currentFrame.spriteId];      
But if you use GetSpriteDefinition(string) just like tk2dSpriteEditor does...

Code: [Select]
spriteDef = currentFrame.spriteCollection.GetSpriteDefinition (spriteDef.name);
then it works. I needed spriteDef.name to use GetSpriteDefinition so as a conclusion it became little bit hacky code like this :
Code: [Select]
spriteDef = currentFrame.spriteCollection.spriteDefinitions [currentFrame.spriteId];
spriteDef = currentFrame.spriteCollection.GetSpriteDefinition (spriteDef.name);

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Problem with using sprite thumbnail code
« Reply #2 on: December 12, 2014, 03:18:47 pm »
Hi,
Yeah this stuff is build on layers and layers of backwards compatible code, here be dragons :)

Looks like you've figured it out though!