- Home /
Dynamic sprite mask
I have a GameObject with the following components: SpriteRenderer, Animator, SpriteMask. The Animator animation exchanges the sprite of the SpriteRenderer. I want the SpriteMask to change according to the current sprite of the SpriteRenderer. I tried doing spriteMask.sprite = spriteRenderer.sprite;
inside the Update method. This exchanges the SpriteMask sprite in the inspector, but the mask itself doesn't change. Is it even possible to do this in Unity (yet)? Am I missing some kind of "RefreshMask" method on the SpriteMask? (could not find anything like this in the manual / scripting API)
Answer by calpolican · Jan 17, 2018 at 10:54 AM
I'm almost sure that for sprites you should use "overrideSprite()" instead. Give it a try: https://docs.unity3d.com/ScriptReference/UI.Image-overrideSprite.html
This question is not aimed at UI images, but at the mask for SpriteRenderers and there is no such thing as an overrideSprite() method: https://docs.unity3d.com/ScriptReference/Sprite$$anonymous$$ask.html
Well, I think sprites can't be changed just by using "sprite = myOtherSprite". I believe you need the override method. Perhaps try to extend the class by mimiking the method that Image uses. Before doing that, I would try something like this, is a long shot but I think it could work (perhaps):
void Start(){
sprite$$anonymous$$ask.sprite = Image.sprite;
}
//And then on Update():
Image.overrideSprite("newSprite");