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.
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.
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
}