sliderProgressSc.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using DG.Tweening;
  4. using System.Collections.Generic;
  5. public class sliderProgressSc : MonoBehaviour
  6. {
  7. [Header("Slider Reference")]
  8. [SerializeField] private Slider targetSlider;
  9. [Header("Dots Configuration")]
  10. [SerializeField] private List<Transform> dots = new List<Transform>();
  11. [SerializeField] private List<Image> dotImages = new List<Image>();
  12. [Header("Slider Animation")]
  13. [SerializeField] private float sliderAnimationDuration = 0.8f;
  14. [SerializeField] private Ease sliderEase = Ease.OutCubic;
  15. [SerializeField] private bool animateSliderValue = true;
  16. [Header("Dot Animation Settings")]
  17. [SerializeField] private float scaleDuration = 0.3f;
  18. [SerializeField] private float colorDuration = 0.2f;
  19. [SerializeField] private Vector3 activeScale = Vector3.one * 1.2f;
  20. [SerializeField] private Vector3 inactiveScale = Vector3.one;
  21. [SerializeField] private Ease scaleEase = Ease.OutBack;
  22. [SerializeField] private float dotStaggerDelay = 0.05f; // Delay between dot activations
  23. [Header("Colors")]
  24. [SerializeField] private Color inactiveColor = new Color(0.3f, 0.3f, 0.3f, 0.6f);
  25. [SerializeField] private Color activeColor = new Color(0f, 1f, 0.4f, 1f); // Bright green
  26. [SerializeField] private Color pulseColor = new Color(1f, 1f, 0.4f, 1f); // Yellow pulse
  27. private float previousSliderValue;
  28. private int lastActivatedDot = -1;
  29. private Tween sliderTween;
  30. private bool isAnimating = false;
  31. void Start()
  32. {
  33. InitializeSlider();
  34. SetupInitialState();
  35. }
  36. void Update()
  37. {
  38. // Check if slider value changed
  39. if (Mathf.Abs(targetSlider.value - previousSliderValue) > 0.001f)
  40. {
  41. if (animateSliderValue && !isAnimating)
  42. {
  43. AnimateSliderToValue(targetSlider.value);
  44. }
  45. else
  46. {
  47. CheckAndAnimateDots(targetSlider.value);
  48. }
  49. previousSliderValue = targetSlider.value;
  50. }
  51. }
  52. void InitializeSlider()
  53. {
  54. if (targetSlider == null)
  55. targetSlider = GetComponent<Slider>();
  56. if (targetSlider == null)
  57. {
  58. Debug.LogError("No slider found! Please assign a slider reference.");
  59. return;
  60. }
  61. // Auto-populate dots if empty
  62. if (dots.Count == 0)
  63. {
  64. Debug.Log("No dots found! Please assign dot transforms manually.");
  65. }
  66. previousSliderValue = targetSlider.value;
  67. }
  68. void SetupInitialState()
  69. {
  70. // Set all dots to inactive state
  71. for (int i = 0; i < dots.Count; i++)
  72. {
  73. if (dots[i] != null)
  74. {
  75. dots[i].localScale = inactiveScale;
  76. if (i < dotImages.Count && dotImages[i] != null)
  77. {
  78. dotImages[i].color = inactiveColor;
  79. }
  80. }
  81. }
  82. lastActivatedDot = -1;
  83. }
  84. void AnimateSliderToValue(float targetValue)
  85. {
  86. if (sliderTween != null && sliderTween.IsActive())
  87. {
  88. sliderTween.Kill();
  89. }
  90. isAnimating = true;
  91. float startValue = targetSlider.value;
  92. sliderTween = DOTween.To(() => targetSlider.value, x => {
  93. targetSlider.value = x;
  94. CheckAndAnimateDots(x);
  95. }, targetValue, sliderAnimationDuration)
  96. .SetEase(sliderEase)
  97. .OnComplete(() => {
  98. isAnimating = false;
  99. });
  100. }
  101. void CheckAndAnimateDots(float sliderValue)
  102. {
  103. // Calculate how many dots should be active based on slider value
  104. // For 10 dots: dot 0 at 0.1, dot 1 at 0.2, ..., dot 9 at 1.0
  105. int dotsToActivate = Mathf.FloorToInt(sliderValue * dots.Count);
  106. // Clamp to valid range
  107. dotsToActivate = Mathf.Clamp(dotsToActivate, 0, dots.Count);
  108. // The actual active dot index (0-based, -1 means no dots active)
  109. int targetActiveDot = dotsToActivate - 1;
  110. // Check if we've moved forward to activate more dots
  111. if (targetActiveDot > lastActivatedDot)
  112. {
  113. // Activate dots with staggered delay for better visual effect
  114. for (int i = lastActivatedDot + 1; i <= targetActiveDot; i++)
  115. {
  116. if (i < dots.Count && i >= 0)
  117. {
  118. float delay = (i - (lastActivatedDot + 1)) * dotStaggerDelay;
  119. ActivateDotWithDelay(i, delay);
  120. }
  121. }
  122. lastActivatedDot = targetActiveDot;
  123. }
  124. // Check if we've moved backward (deactivate dots)
  125. else if (targetActiveDot < lastActivatedDot)
  126. {
  127. // Deactivate dots from lastActivatedDot down to targetActiveDot+1
  128. for (int i = lastActivatedDot; i > targetActiveDot; i--)
  129. {
  130. if (i < dots.Count && i >= 0)
  131. {
  132. DeactivateDot(i);
  133. }
  134. }
  135. lastActivatedDot = targetActiveDot;
  136. }
  137. }
  138. void ActivateDotWithDelay(int dotIndex, float delay)
  139. {
  140. if (dotIndex >= dots.Count || dotIndex < 0 || dots[dotIndex] == null) return;
  141. DOVirtual.DelayedCall(delay, () => ActivateDot(dotIndex));
  142. }
  143. void ActivateDot(int dotIndex)
  144. {
  145. if (dotIndex >= dots.Count || dotIndex < 0 || dots[dotIndex] == null) return;
  146. Transform dot = dots[dotIndex];
  147. // Scale up animation with bounce effect
  148. dot.DOScale(activeScale, scaleDuration).SetEase(scaleEase);
  149. // Color change to active with pulse effect
  150. if (dotIndex < dotImages.Count && dotImages[dotIndex] != null)
  151. {
  152. Image dotImage = dotImages[dotIndex];
  153. // Initial color change
  154. dotImage.DOColor(activeColor, colorDuration);
  155. // Add a subtle pulse effect
  156. dotImage.DOColor(pulseColor, colorDuration * 0.5f)
  157. .SetDelay(colorDuration)
  158. .SetLoops(2, LoopType.Yoyo)
  159. .SetEase(Ease.InOutSine);
  160. }
  161. }
  162. void DeactivateDot(int dotIndex)
  163. {
  164. if (dotIndex >= dots.Count || dotIndex < 0 || dots[dotIndex] == null) return;
  165. Transform dot = dots[dotIndex];
  166. // Scale down animation
  167. dot.DOScale(inactiveScale, scaleDuration * 0.7f).SetEase(Ease.OutQuad);
  168. // Color change to inactive
  169. if (dotIndex < dotImages.Count && dotImages[dotIndex] != null)
  170. {
  171. dotImages[dotIndex].DOColor(inactiveColor, colorDuration * 0.7f);
  172. }
  173. }
  174. // Public methods for external control
  175. public void SetSliderValue(float value)
  176. {
  177. if (targetSlider != null)
  178. {
  179. if (animateSliderValue)
  180. {
  181. AnimateSliderToValue(value);
  182. }
  183. else
  184. {
  185. targetSlider.value = value;
  186. }
  187. }
  188. }
  189. public void SetSliderValueInstant(float value)
  190. {
  191. if (targetSlider != null)
  192. {
  193. targetSlider.value = value;
  194. CheckAndAnimateDots(value);
  195. }
  196. }
  197. public void ResetDots()
  198. {
  199. // Kill any running animations
  200. if (sliderTween != null && sliderTween.IsActive())
  201. {
  202. sliderTween.Kill();
  203. }
  204. for (int i = 0; i < dots.Count; i++)
  205. {
  206. if (dots[i] != null)
  207. {
  208. dots[i].DOKill();
  209. }
  210. if (i < dotImages.Count && dotImages[i] != null)
  211. {
  212. dotImages[i].DOKill();
  213. }
  214. }
  215. // Reset state
  216. lastActivatedDot = -1;
  217. isAnimating = false;
  218. SetupInitialState();
  219. }
  220. // Get current progress (0-10)
  221. public int GetCurrentProgress()
  222. {
  223. return lastActivatedDot + 1;
  224. }
  225. // Check if specific dot is active
  226. public bool IsDotActive(int dotIndex)
  227. {
  228. return dotIndex <= lastActivatedDot;
  229. }
  230. void OnDestroy()
  231. {
  232. // Clean up any running tweens
  233. if (sliderTween != null && sliderTween.IsActive())
  234. {
  235. sliderTween.Kill();
  236. }
  237. for (int i = 0; i < dots.Count; i++)
  238. {
  239. if (dots[i] != null)
  240. {
  241. dots[i].DOKill();
  242. }
  243. if (i < dotImages.Count && dotImages[i] != null)
  244. {
  245. dotImages[i].DOKill();
  246. }
  247. }
  248. }
  249. // // Debug helper - shows current calculation values in inspector
  250. // [System.Serializable]
  251. // public class DebugInfo
  252. // {
  253. // [SerializeField] public float currentSliderValue;
  254. // [SerializeField] public int dotsToActivate;
  255. // [SerializeField] public int targetActiveDot;
  256. // [SerializeField] public int lastActivatedDot;
  257. // }
  258. // [Header("Debug Info (Runtime Only)")]
  259. // [SerializeField] private DebugInfo debugInfo = new DebugInfo();
  260. // void LateUpdate()
  261. // {
  262. // // Update debug info for inspector visibility
  263. // if (targetSlider != null)
  264. // {
  265. // debugInfo.currentSliderValue = targetSlider.value;
  266. // debugInfo.dotsToActivate = Mathf.FloorToInt(targetSlider.value * dots.Count);
  267. // debugInfo.targetActiveDot = debugInfo.dotsToActivate - 1;
  268. // debugInfo.lastActivatedDot = lastActivatedDot;
  269. // }
  270. // }
  271. }