Hello Guest

Author Topic: How to display a sprite within a sprite collection in an editor window?  (Read 5097 times)

TinyTim

  • Newbie
  • *
  • Posts: 2
    • View Profile
I have been trying to figure out how I would go about displaying each sprite kept within a sprite collection on a custom editor window. I've tried using the tk2dSpriteCollectionData.textures route but that just shows the one texture for all the sprites as one image. I want to display each sprite by themselves and was wanting to figure out how to do so.

I could think of how I would need to get the location within the atlas but I wouldn't know how to go about doing so. So if any of you could send me in the right direction that would be absolutely amazing and I would greatly appreciate that!

**EDIT**

So I've done a little bit of looking around both on the forum and in the SpriteCollection Data object and found out how I can possibly do this. I would need to get the x and y cord for the sprite based on the texture and then turn that into its separate texture2D with GetPixels and use that. The only problem is I can't figure out where the x/y cord is stored. I read something about using the UVs but I don't understand how I would go about using those as they're some, seemingly, unusable float that doesn't point to anything I need.

So I guess the new question is how I would go about finding the x/y cord of the top left most corner of the sprite using the data stored withing tk2dSpriteCollectionData.spriteCollection.
« Last Edit: February 02, 2016, 10:36:34 pm by TinyTim »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to display a sprite within a sprite collection in an editor window?
« Reply #1 on: February 02, 2016, 10:41:59 pm »
If you want to display in a custom editor window, I strongly recommend looking at how tk2d does it in the sprite editor.
tk2dSpriteEditorr.cs, DrawSpriteEditorGUI look for the line below and the surrounding lines.

tk2dSpriteThumbnailCache.DrawSpriteTextureInRect

TinyTim

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to display a sprite within a sprite collection in an editor window?
« Reply #2 on: February 02, 2016, 11:26:14 pm »
So I went through and looked at the code. I guess I'm just not understanding because every variable used to create the texture I see in the tk2dSpriteThumbnailCache (I was taken there by following the code from tk2dSpriteEditor) are the same between all the sprite definitions. The only things I've found to really be different are within the UVs and the RegionX/Y/W/H. And when I look at those variables I just don't understand how I can make them connect to the atlas texture. As the UVs are extremely small floats and the regionX/Y point to completely different locations on the texture.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to display a sprite within a sprite collection in an editor window?
« Reply #3 on: February 02, 2016, 11:30:54 pm »
The UVs contain the UVs used to draw the geometry that defines the sprite. Follow that function through to DrawSpriteTextureInRect and you can see how its actually drawn - its a mesh thats drawn using the positions / uvs. This isn't necessarily a quad in tk2d, so you can't make that assumption. The texture itself can be obtained by def.materialInst.mainTexture.

If you need to draw the "sprite" out of the atlas, this is the way to do it.