본문 바로가기
유니티 공부

게이지 만들기와 어제의 문제점 해결(feat.TIL)

by g-builder 2024. 9. 11.

오늘도 이것저것 ui들과  코드들을 조금씩 손봤지만 확실히 기억해둬야겠다고 생각한건 게이지 만들기이다

 

먼저 게이지바로 만들 이미지를 변수선언해준뒤 변수의 fillAmount값을 조정할수 있는 함수를 만들어준다.

그밑에 스프라이트는 fillAmount 값의 변동에따라 게이지바의 색깔을 변화시키기 위해 스프라이트를 변환하는 코드다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    public Image timeGaugeBar;
 
    public void SetGauge(float min, float max, float time)
    {
        timeGaugeBar.fillAmount = time/max;
        if (0.67f< timeGaugeBar.fillAmount && timeGaugeBar.fillAmount <= 1f)
        {
            timeGaugeBar.sprite = Resources.Load<Sprite>("curved_slider_fill_02");
        }else if(0.34f < timeGaugeBar.fillAmount && timeGaugeBar.fillAmount <= 0.67f)
        {
            timeGaugeBar.sprite = Resources.Load<Sprite>("curved_slider_fill_03");
        }
        else if(timeGaugeBar.fillAmount <= 0.34f)
        {
            timeGaugeBar.sprite = Resources.Load<Sprite>("curved_slider_fill_01");
        }
    }
cs

 

이때 fillAmount는 Image컴포넌트의 Image Type을 Filled로 변경해줘야 Fill Amount가 등장해준다

그런다음 업데이트 에서 함수를 호출한뒤 작동시켰다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
    void Update()
    {
        time -= Time.deltaTime;
 
        timeBar.SetGauge(030, time);
 
        timeTxt.text = time.ToString("N2");
 
    }
cs

 

 

미고토다! 훌륭하다!

 

그리고 어제 배경의 이미지가 스케일이 0이되면 멈춘채 다시 1이 되도 움직이지 않는 현상을 오늘아침 컴퓨터를 키자마자 찾아봤다.

생각보다 아주 쉬운문제여서 찾고 해결하는데 10분도 안걸린것같다

 

애니메이터가 들어있는 오브젝트의 애니메이터 컴포넌트의 Update Mode를 Unscaled Time로 바꿔주면된다.이러면 Scale값의 영향을 안받는다.

 

 

오늘도 고생많으셨습니다~