unity/유니티 초보

Unity - 아이템 쿨타임 UI

rimugiri 2024. 2. 20. 22:44
728x90

유니티에서는 버프나 아이템에 쿨타임 화면이 표기되는데 이는 Image의 Type을 이용하여 손쉽게 구현 할 수 있다

1. 아이템 쿨타임 조정 코드

using UnityEngine;
using UnityEngine.UI;

public class CoolDown : MonoBehaviour
{
    [SerializeField] private float _coolTime = 3f;

    [SerializeField] private Image image;

    private float _timer = 0;
    private void Awake()
    {
        image = GetComponent<Image>();
    }
    void Start()
    {
        // 코드로도 조정이 가능하다

        // 시계 방향으로 쿨타임 감소
        //image.type = Image.Type.Filled;
        //image.fillMethod = Image.FillMethod.Radial360;
        //image.fillClockwise = false;
        //image.fillOrigin = (int)Image.Origin360.Top;

        // 우 -> 좌 감소
        //image.type = Image.Type.Filled;
        //image.fillMethod = Image.FillMethod.Horizontal;
        //image.fillOrigin = (int)Image.OriginHorizontal.Left;

        // 아래 -> 위 감소
        //image.type = Image.Type.Filled;
        //image.fillMethod = Image.FillMethod.Vertical;
        //image.fillOrigin = (int)Image.OriginVertical.Top;
    }

    void Update()
    {
        _timer += Time.deltaTime;

        //0 ~1값으로 제어
        image.fillAmount = 1 - _timer / _coolTime;

        //반복적인 작동을 보여주기 위한 코드
        if (_timer >= _coolTime)
        {
            _timer = 0;
        }

        //// 왔다 갔다 코드 3.14초
        //image.fillAmount = Mathf.Sin(_timer) * 0.5f + 0.5f;
    }
}

 

2. 구현 결과

0

728x90