- Home /
Change Spritesheet Programatically
Can an animation's sprite sheet be changed programatically? I know that I can use mecanim to define different animation states and set bools/triggers, etc. etc., but that's not what I mean.
I have an enemy that uses sprite sheet animation. He has the ability to lose limbs, so I have sprite sheets for each permutation of lost limbs (no left arm, no right arm, no arms, no head and no left arm, no head and no right arm, etc.), and color variations of each of those permutations. In total, I have like 20 sprite sheets for this one enemy, and I really don't feel like defining a separate animation state for each. I can do it if I have to, but I was hoping I could do something like this (pseudocode) to save some time:
public SpriteSheet allLimbs;
public SpriteSheet noLeftArm;
public SpriteSheet noLeftArmNoHead;
public SpriteSheet noArmsNoHead;
...
public enum Limb{
Head, LeftArm, RightArm
}
public void LoseLimb(Limb limb){
if(limb == Limb.Head){
SwitchSpriteSheet();
...
} ...
...
}
Can this sort of thing be done natively in Unity? If not, it's probably quicker just to do the individual animation states for my purposes, rather than write a custom SpriteSheet class and parse image data.
Just a note,, in case it's not possible (I really hope it is though, since I ned it for my project as well): Consider using a shader that supports palettes, this way you can assign different palettes based on the damage your character receives (use transparent colors for holes in the body). Of course, the spritesheet bitmap has to be set up the right way so that each possible damage area has it's own color.
What is the SpriteSheet class you're using? Unity's own Sprite atlas, or a third party such as Texture packer? If your sprites are always arranged in the same layout on each sheet, is it not just a case of changing the renderer.shared$$anonymous$$aterial.mainTexture to the appropriate sheet?
@tanoshimi That's just pseudocode. I don't actually have a SpriteSheet class. That's just something like what I was hoping to accomplish. I'm using all stock Unity stuff in my project, and using frame animation from the spritesheet using animations in mecanim. All of my spritesheets are exactly the same in dimensions and number of frames. I'll look into the solution you've mentioned, but I have a feeling the animation will overwrite any sprites/textures I assign through code. I suspect I'll have to access the Animator in order to achieve what I'm looking for.
Answer by mikest · Jul 09, 2016 at 07:34 AM
this is a pretty typical 2D scenario. There is a trick to do what you want. If you watch this video starting at about 19:20 you can see the basic idea for how to do sprite swaps.
https://www.youtube.com/watch?v=rMCLWt1DuqI
I do something similar in my games, but rather than naming the sprites as in the video, I slice them and make sure that i am always referencing the same frame. I did a blog post on it but it's not very detailed but it might give you some ideas on how to approach the problem in your game.
http://blog.hofstudios.com/post/140743627622/cubicle-run-devlog-art-and-animations
Hopefully i have understood your issue.