본문 바로가기
개발/C#

유니티 C# 이미지 크기 애니메이션 간단 구현 Image Scale Animation

by SPNK 2023. 8. 7.
반응형

코드 작성

유니티의 스프라이트 이미지가 자동으로 커졌다가 작아지는 애니메이션입니다

using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

public class ImageAnimation : MonoBehaviour
{
    Image image;

    float size = 0;

    bool check = false;

    WaitForSeconds WaitForSeconds = new WaitForSeconds(0.01f);


    private void Awake()
    {
        image = GetComponent<Image>();
    }

    [Button]
    public void SizeUp()
    {
        size = 0;

        check = false;

        transform.localScale = Vector3.zero;

        StopAllCoroutines();
        StartCoroutine(StarUpAnimation());
    }

    [Button]
    public void SizeDown()
    {
        size = 1;

        check = false;

        transform.localScale = Vector3.one;

        StopAllCoroutines();
        StartCoroutine(StarDownAnimation());
    }

    IEnumerator SizeUpAnimation()
    {
        if(!check)
        {
            if (size < 1.2f)
            {
                size += 0.04f;
            }
            else
            {
                check = true;
            }
        }
        else
        {
            if(size > 1f)
            {
                size -= 0.04f;
            }
            else
            {
                SoundManager.instance.PlaySFX(GameSfxType.GetStar);

                transform.localScale = Vector3.one;
                yield break;
            }
        }

        transform.localScale = new Vector3(size, size, size);

        yield return WaitForSeconds;
        StartCoroutine(StarUpAnimation());
    }

    IEnumerator SizeDownAnimation()
    {
        if(size > 0)
        {
            size -= 0.04f;
        }
        else
        {
            SoundManager.instance.PlaySFX(GameSfxType.LoseStar);

            transform.localScale = Vector3.zero;
            yield break;
        }

        transform.localScale = new Vector3(size, size, size);

        yield return WaitForSeconds;
        StartCoroutine(StarDownAnimation());
    }
}
반응형

댓글