Hello Guest

Author Topic: Swapping sprites in a Hierarchy from another sprite?  (Read 7220 times)

4ror

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Swapping sprites in a Hierarchy from another sprite?
« on: October 02, 2012, 02:34:57 pm »
Swapping textures on a Character in a hierarchy
I?ve been working on a game and all has been going well. I am new to unity but I have much of it working.  I bought the 2dtk plugin sometime ago but never got round to implementing it. I am now looking at optimising my textures using the texture atlasing feature as this has lowered my draw calls down.

I?ve managed to create an atlas for various level graphics. For example each sprite 3 textures, 2 of them show various levels of visual damage. These spirtes get swap out when a damage is applied. 

My question. I want to use 2dtk for the player textures. It?s slightly more complicated because the player is in a hierarchy. In this hierarchy are eyes, arms, body, and a mouth. They all share the same textures atlas. What I had working originally is when the player falls a distance the character mouth and arms swap textures over to a falling texture, not animated. Also the character eyes blink, hence my reason for separate layered graphics.  I had a javascripted attached to the parent and it would find the gameObject on startup, and swap the textures of the attached using unity?s built in features.  All works great however, I?m not sure how by using one javascript I can swap sprite id?s of another sprite using 2dtk...any tips?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Swapping sprites in a Hierarchy from another sprite?
« Reply #1 on: October 02, 2012, 04:30:06 pm »
You can easily swap spriteIds from javascript.

1. Make sure you've run the Setup for Javascript option
2. In your JS, do something like this:

Code: [Select]
var sprite : tk2dSprite;
sprite = GetComponent(tk2dSprite);

sprite.spriteId = sprite.GetSpriteIdByName("body_hurt");

Later to optimize this, you can cache the spriteIds on startup.

4ror

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Swapping sprites in a Hierarchy from another sprite?
« Reply #2 on: October 02, 2012, 05:01:41 pm »
Hi unikron,

thanks for the reply. This is what I've done with individual sprites that share the same script and using an int array to store the ID's of each id in the atlas. But how would a single script know which sprite to swap textures of other sprites? It doesn't work because I think its getting tk2dscript of itself not the other sprites even if I drag the sprites into the var slots arms, mouth in the inspector.

Code: [Select]
var player : tk2dSprite;
var mouth : tk2dSprite;
var arms : tk2dSprite;

player = GetComponent(tk2dSprite);
mouth = GetComponent(tk2dSprite);
arms = GetComponent(tk2dSprite);

// If something happens swap some sprites round
arms.spriteId = spriteIdentity[3];
mouth.spriteId = spriteIdentity[4];

// else swap them back
arms.spriteId = spriteIdentity[1];
mouth.spriteId = spriteIdentity[2];

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Swapping sprites in a Hierarchy from another sprite?
« Reply #3 on: October 02, 2012, 05:06:59 pm »
If you need to change the whole hierarchy in one go, create an array for each of the parts, and use GetComponentsInChildren(tk2dSprite) to get all of them. You'll still need to swap them manually after that

4ror

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Swapping sprites in a Hierarchy from another sprite?
« Reply #4 on: October 02, 2012, 07:43:58 pm »
Thanks. When you said array that's what I needed. I created another array of sprites > assigned them in the inspector > in script, pick the element I want to change + elements from another array of sprites and swap on condition... now I've reduced my draw calls a little more.

Thanks again.