- Home /
Blend between sprites without animator
So, this question is pretty straight forward. I want to blend between a few sprites easily, and using the animator is a TON of work to create for each and every single piece of armor i will have in the game.
I have just a few sprites that would rotate along the player.
Answer by gernomino · May 15 at 01:28 PM
Are there any animations tied to the armor? If not, you could change the sprite via script.
In theory, you could also write an editor script to generate the needed animator controller. Might be a little complicated but would basically go like
- Select your up, left, right, and down sprites in the asset window in order
- In context menu, have menu item that generates the animator controller asset and blend tree for those sprites. I haven't done this before but it's possible. The editor has some contextual behavior already. For instance, if you have sprites selected and right-click -> Create Animation, it will auto add the sprites to a new animation asset. Could do something similar, maybe with
- ContextMenu
- Create menu items in project window with [ContextMenu("Assets/MenuName")]
- AssetDatabase
- For saving your animation controller to a file - AnimationController
- Of course you'll need to find a way to set the animations programmatically to save them to the controller.
I'm not certain how to get the selected objects. It looks like there's Selection.activeObject
. Not sure if it returns an array you can iterate on or if there's some other API you'd use.
This is an interesting approach, but very complex. I wanted to chime in though:
Selection.objects
will give you an array of selected objects to iterate over.
Answer by Thygrrr · May 15 at 06:25 PM
Here is a slightly different approach:
first create a prefab that blends between generic sprites (using animators)
then create prefab variants that then each have the sprites you want (they can keep the same animator and animations)
An alternative way is to just smack it with code and no animators:
using UnityEngine;
[RequireComponent(typeof(SpriteRenderer))]
public class DirectionalSprite : MonoBehaviour
{
//Put your directional variants here.
public Sprite[] directionSprites = new Sprite[4];
//You can quickly call this on the GameObject e.g.
//on the PlayerController to all objects that understand the Method
//BroadcastMessage("ChangeDirection", Direction.West);
//Or by getting all the DirectionalSpriteComponents via
//GetComponentsInChildren<DirectionalSprite>() and calling
//ChangeDirection for all of em when needed.
public void ChangeDirection(Direction dir)
{
GetComponent<SpriteRenderer>().sprite = directionSprites[(int)dir];
}
}
//The numbers are optional but serve as illustration.
//They will be consecutive as in the list by default,
//as needed by our array indexer in ChangeDirection
public enum Direction
{
North = 0,
East = 1,
South = 2,
West = 3
}
NB: you should probably cache the SpriteRenderer in Awake or make it a public field that you can assign in the inspector. The script goes on the piece of armor, e.g. the helm or the mail.
Your answer
![](https://koobas.hobune.stream/wayback/20220613060601im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use multiple bone rigged sprites for 2d character animations 0 Answers
Animating 2D Character made up of 2 or more swapable sprites 1 Answer
Is Unity capable of implementing Complex 2D Characters/Animation/Sprites 1 Answer
Alpha masking multiple 2D Sprite GameObjects with 1 Sprite GameObject 0 Answers