Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Programmatically Swap All Sprites In An Animation

Discussion in 'Animation' started by cjcota136, Mar 2, 2021.

  1. cjcota136

    cjcota136

    Joined:
    Feb 16, 2021
    Posts:
    1
    Right now I am using the LateUpdate Method and swapping the sprite depending on what sprite it is. For example when the character is running the helmet object swaps its sprite from Helmet1_1 to Helmet1_6 over the course of the animation. Now what my code does is it looks for what sprite you would like it to show instead, for example Helmet2_1 instead of Helmet1_1 and over the course of the animation it'll swap the Helmet1_1 for Helmet 2_1 and one second later Helmet1_2 for Helmet2_2. Is there a way to set all sprites within an animation until changed programmtically?

    Code (CSharp):
    1.         foreach (var renderer in Breastplate.GetComponentsInChildren<SpriteRenderer>())
    2.         {
    3.             string spriteName = renderer.sprite.name;
    4.             var newSprite = Array.Find(subBreastplateSprites, item => item.name == spriteName);
    5.             if (newSprite)
    6.                 renderer.sprite = newSprite;
    7.         }
     
unityunity