Untitled

 avatar
unknown
plain_text
21 days ago
1.1 kB
3
Indexable
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using deVoid.Utils;
using DG.Tweening; // Make sure DOTween is imported

public class StarCounterUI : MonoBehaviour
{
    public Image starImage; // Single star image
    public FloatField score;
    public int starsThreshold = 15;
    public TextMeshProUGUI starCountText;

    private int currentStars = 0;

    private void OnEnable()
    {
        score.onChange.AddListener(UpdateStars);
    }

    private void OnDisable()
    {
        score.onChange.RemoveListener(UpdateStars);
    }

    private void UpdateStars()
    {
        int newStars = Mathf.FloorToInt(score.Value / starsThreshold);

        if (newStars > currentStars)
        {
            //Optional: Add a little animation to the star or the text when it updates
            starImage.transform.DOPunchScale(Vector3.one * 0.2f, 0.3f);
        }

        currentStars = newStars;
        starCountText.text = currentStars.ToString();
    }



    private void Start()
    {
        UpdateStars(); // Initialize on start
    }
}
Leave a Comment