티스토리 뷰

반응형

[unity/score] 스코어 점수 올라가는 로직 구현(with lerf)

 

 

1초동안 100점

1초동안 1000점 5초동안 10점 1초동안 10000점 등등 

 

정해진 시간과 점수를 기반으로 로직구현

 

 

 

#에디터 구조

 

#함수 구현

UI 적용 함수 - ShowScore

로직 구현 - ProgressScore

 

ProgressScore는 Time.deltaTime/duration 로 쪼개어 lerp를 적용하여 구현하였다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UI;

public class CounterManager : MonoBehaviour
{
    [SerializeField]
    float duration = 3f;

    [SerializeField]
    int score = 5000;

    /// <summary>
    /// 숫자 이미지 리소스를 위한 값
    /// </summary>
    [SerializeField]
    List<RawImage> numList;
    Texture2D[] sequences;

    /// <summary>
    /// 스코어 카운팅을 위한 값
    /// </summary>
    private int progressStart = 0;
    private int progressEnd = 5000;
    private int totalScore = 0;

    /// <summary>
    /// 델타타임 누적 값
    /// </summary>
    private float deltaTime = 0f;

    /// <summary>
    /// 초기화
    /// </summary>
    private void Awake()
    {
        InitScore();
        GetScoreResources();
    }

 
    public void Set(float duration_,int score_)
    {
        duration = duration_;
        score = score_;
    }
    public void Run()
    {
        InitScore();
        isRun = true;

    }

    public void Stop()
    {
        isRun = false;
    }


    bool isRun = false;
    void Update()
    {
        if (Input.GetKeyDown("1"))
        {
            Run();
        }

        if (isRun)
        {
            
            ProgressScore();
            ShowScore(totalScore);

            if(totalScore == progressEnd)
            {
                Debug.Log("걸린시간:" + deltaTime);
                Stop();
            }
        }
    }

    void GetScoreResources()
    {
        Object[] obj = Resources.LoadAll("Images", typeof(Texture2D));
        sequences = new Texture2D[obj.Length];

        if (obj.Length > 0)
        {
            sequences = new Texture2D[obj.Length];
            for (int i = 0; i < obj.Length; i++)
            {
                sequences[i] = (Texture2D)obj[i];
            }
        }
    }

    void InitScore()
    {
        isRun = false;

        progressStart = totalScore;
        lerp = 0;
        deltaTime = 0;
        progressEnd = totalScore + score;
    }

 
    void ShowScore(long num)
    {
        Debug.Log(num);
        string numString = num.ToString();
        int leng = num.ToString().Length;

        for (int i = leng; i >= 1; i--)
        {
            numList[leng - i].texture = sequences[int.Parse(numString.Substring(i - 1, 1))];

        }
    }

    float lerp = 0f;
    int ProgressScore()
    {
        deltaTime += Time.deltaTime;
        lerp += Time.deltaTime / duration;
        totalScore = (int)Mathf.Lerp(progressStart, progressEnd, lerp);
        return totalScore;
    }

}

#score #lerp #unity #시간 #duration #점수 #game #게임

반응형
댓글
반응형