12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class StarsIndicator : MonoBehaviour
- {
- public Slider slider;
- public StarTarget star1, star2, star3;
- int _maxScore;
- float _maxWidth;
- void Awake()
- {
- //Get the width from the slider's sprites
- _maxWidth = slider.GetComponent<RectTransform>().sizeDelta.x;
- slider.value = 0;
- }
- void OnEnable()
- {
- NotificationCenter.AddListener(OnScoreChanged, NotificationType.UpdateScoreLabel);
- var level = LevelsManager.Instance.CurrentLevel;
- SetStarPositions(level.star1Threshold, level.star2Threshold, level.star3Threshold);
- }
- void OnDisable()
- {
- NotificationCenter.RemoveListener(OnScoreChanged, NotificationType.UpdateScoreLabel);
- }
- void SetStarPositions(int star1Threshold, int star2Threshold, int star3Threshold)
- {
- _maxScore = star3Threshold;
- star1.SetPosition(star1Threshold, _maxScore, _maxWidth);
- star2.SetPosition(star2Threshold, _maxScore, _maxWidth);
- star3.SetPosition(star3Threshold, _maxScore, _maxWidth);
- }
- void OnScoreChanged(Notification note)
- {
- int score = GameDataManager.Instance.Score;
- float newSliderValue = score / (float)_maxScore;
- iTween.Stop(gameObject);
- iTween.ValueTo(gameObject, iTween.Hash(
- "from", slider.value,
- "to", newSliderValue,
- "time", 0.3f,
- "onupdate", "OniTweenUpdateSlider",
- "onupdatetarget", gameObject));
- }
- void OniTweenUpdateSlider(float newValue)
- {
- slider.value = newValue;
- }
- }
|