Search code examples
unity-game-engineanimationresizesizesprite

How to animate a 9 slice in Unity?


I want to animate a 9 slice and simply want for me to just put in a speed value with a value of how big I want for it to become on specific axes just in a parameter. But when I tried using SpriteRenderer.size += Vector2(something, something) its moves buggily, has an inconsistent speed growth, and sometimes goes far above the threshold and then snaps back. Any idea how to fix this?

Keep in mind I'm pretty new to IEnumerators and coding and unity in general so be nice lel

 public IEnumerator BarSize(float speed , float XToSize, float YToSize)
    {
        while(true)
        {
            for(float i = Sprite.size.x; 1 != XToSize; i = Sprite.size.x)
            {
                Sprite.size += new Vector2(speed, 0);

                if(i >= XToSize)
                {
                    Sprite.size = new Vector2(XToSize, Sprite.size.y);

                    yield break;
                }

                yield return new WaitForFixedUpdate();
            }
            
            for (float i = Sprite.size.x; 1 != XToSize; i = Sprite.size.x)
            {
                Sprite.size += new Vector2(0, speed);

                if (Sprite.size.y >= YToSize)
                {
                    Sprite.size = new Vector2(transform.localScale.x, YToSize);

                    yield break;
                }

                yield return new WaitForEndOfFrame();
            }

            if (Sprite.size == new Vector2(XToSize, YToSize))
            {
                yield break;
            }

            yield return new WaitForEndOfFrame();
        }
    }

Solution

  • I am an IDIOT!!! I FORGOT to use WaitForFixedUpdate() and used the situationally worse WaitForEndOfFrame(). Lmao EVERYTHING works now.