9
\$\begingroup\$

I'm working in Unity, and I'm finding that the animator isn't working for what I'm trying to do, in my 2D game. I want to build a specific animation, using sprite sheets, and be able to reuse the same animation with other sprites that are the same size and dimensions by cells. After some research, I find that it is impossible.

Instead, I'm trying to create my own animation script, but I don't understand how to tell Unity to draw only a section of the sprite. Something like in XNA, where you could render only a specific rectangle of a sprite using start and end coordinates.

How do I manually draw a part of a sprite sheet?

\$\endgroup\$
  • 3
    \$\begingroup\$ This is possible in Unity without duplicating the animation data, with a little hack. Effectively, you play animation A using spritesheetA, then in LateUpdate you swap each SpriteRenderer's Sprite to its analogue from spritesheetB. There was a Unity video on this technique; I'll post it as an answer if I can find it later, and elaborate on the details. This will let you use the full power of Unity's native animation system without having to write your own from scratch. \$\endgroup\$ – DMGregory Jul 22 '16 at 12:05
  • \$\begingroup\$ You should try Spine or some other skeletal animation software. It has a skins capability that allows you to maintain the same bone animations, but swap and change skins as needed. I personally use Spine because it is simple, there are tutorials and it has a unity runtime that makes everything super simple. The downside is it does cost money, but the basic version gives you everything you need to make animations. The pro version just makes it easier and quicker. \$\endgroup\$ – Savlon Feb 26 '18 at 10:25
  • \$\begingroup\$ use masks if you work on UI \$\endgroup\$ – virtouso Aug 5 '18 at 8:54
1
\$\begingroup\$

I think there can be a simpler approach in which you can do this , and still use Unity Mecanim for the animation.

Steps:-

1) You can programatically assign texture 2D sprites from the image, and as it is a grid it wont be much difficult to do so. In the following answer the user has created his own sprite slicer and saved into the directory (optional else just save it in a variable). You don't need the whole code,just check the GetSprites() function and you will get the idea :)

http://answers.unity3d.com/questions/576799/coding-my-own-auto-slicer-getting-islands-of-pixel.html

2) Once you are able to do that, then maintain an Array of sprites, which you got from step 1.

3) Now you need to create an anim file of these sprites which can be plugged into mecanim. For this you need Unity's Set Curve function(check the link provided below). Once you have the clip its upto you whether to save it for future use or just have it in a variable for the current game instance only.

http://docs.unity3d.com/ScriptReference/AnimationClip.SetCurve.html

Hope this resolves your doubt :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.