Create a complex object meaning instead of a single GameObject for your sprite use multiple GameObjects. These “props” would be children of the main sprite.
Can’t be done using bones. Using layers, will lead to making new animations each time a new item is added, and tons of layers. Unfortunately unity has no good solution to this issue. The best I could make is, I animate my own Sprite property, then I change each child item sprite using the current sprite, in LateUpdate.
public class SpriteSheetAnimator : MonoBehaviour
{
public Sprite m_Sprite; // property that is animated using Animator
public Vector3 m_Scale = new Vector3(1f, 1f, 1f); // handles flip; Animator too.
internal Dictionary<string, Sprite> m_BaseSpriteReference;
SpriteSheet[] m_Sheets;
void Awake ()
{
m_Sheets = GetComponentsInChildren<SpriteSheet>();
m_BaseSpriteReference = new Dictionary<string, Sprite>();
foreach (var sprite in Resources.LoadAll<Sprite>("avatar1"))
m_BaseSpriteReference.Add(sprite.name, sprite);
}
void LateUpdate ()
{
foreach (var sheet in m_Sheets)
{
sheet.m_Renderer.sprite = sheet.m_Sprites[m_Sprite];
sheet.transform.localScale = m_Scale;
}
}
}
public class SpriteSheet : MonoBehaviour
{
public string m_SheetName;
internal SpriteRenderer m_Renderer;
internal Dictionary<Sprite, Sprite> m_Sprites;
void Start ()
{
m_Renderer = GetComponent<SpriteRenderer>();
m_Sprites = new Dictionary<Sprite, Sprite>();
var m_SheetAnimator = GetComponentInParent<SpriteSheetAnimator>();
foreach (var sprite in Resources.LoadAll<Sprite>(m_SheetName))
m_Sprites.Add(m_SheetAnimator.m_BaseSpriteReference[sprite.name], sprite);
}
}
You are saying it can’t be done with the sprites you are currently using right? Because it can be done with bones, though not easily accomplished with how your sprites have been created.
I agree using layers will result in a heavy lift - but that is one solution that works (though I’d hate it)
Is this child item sprite drawn on top of the base sprite in LateUpdate? Does this work as desired? Underdeveloped left brain restricts me from completely understanding your code.
What I did was just synchronize itens&avatar animations based on the current sprite being played, each item sprite is updated according to current sprite defined at the animation, using its own sprite though. I can attach an example project tomorrow if you wish to take a closer look.