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
'unity > 유니티 초보' 카테고리의 다른 글
Unity - rootMotion 설정 창 열리지 않을때 <root contain root motion curve> (0) | 2024.04.24 |
---|---|
Unity - UI 자동화 (0) | 2024.04.04 |
Unity - new Input System 사용법 (1) | 2024.01.23 |
Unity2D - 간단한 바운스볼 로직 구현 (0) | 2024.01.18 |
Unity3D - Custom Editor 제작 (0) | 2024.01.12 |