Hello Guest

Author Topic: One SpriteAnimation as template for multiple sprites  (Read 3824 times)

astrobread

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
One SpriteAnimation as template for multiple sprites
« on: August 04, 2014, 05:19:22 pm »
I have several items the player can wear, and each have a spritesheet so they can shift around properly during the walk animation. I loaded all the spritesheets into one SpriteCollection, and then was faced with the task of making a walk animation for each and every one.

Instead, I made a single animation prefab and then wrote a script to repurpose it for every item.

So my question based on researching similar posts: is there a reason I should be using Resources.Load instead of tk2dSpriteAnimation.Instantiate? Are there any other potential issues using this method? Thanks!

Code: [Select]
tk2dSpriteAnimator theAnimator = gameObject.AddComponent<tk2dSpriteAnimator>();
theAnimator.Library = (tk2dSpriteAnimation)tk2dSpriteAnimation.Instantiate(genericAnimationPrefab);
for (int i = 0; i < theAnimator.Library.clips.Length; i++)
{
   for (int j = 0; j < theAnimator.Library.clips[i].frames.Length; j++)
   {
      int oldId = theAnimator.Library.clips[i].frames[j].spriteId;
      int newId = TransformOldIdToNewId(oldId); // pseudo-code placeholder function
      theAnimator.Library.clips[i].frames[j].spriteId = newId;
   }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: One SpriteAnimation as template for multiple sprites
« Reply #1 on: August 05, 2014, 10:38:26 pm »
No Instantiate will work fine. Don't forget to destroy the object if you're doing this at rntime and not changing scene after that...