JumpingNumberTextComponent.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ShotSimulator.UI
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using DG.Tweening;
  11. using DG.Tweening.Core;
  12. using DG.Tweening.Plugins.Options;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. public class JumpingNumberTextComponent : MonoBehaviour
  16. {
  17. private TweenerCore<int, int, NoOptions> m_Tweener;
  18. private int currentValue;
  19. private int endValue;
  20. public Text viewText;
  21. public Text incNumText;
  22. public void Init()
  23. {
  24. if (m_Tweener != null)
  25. {
  26. m_Tweener.Kill();
  27. }
  28. viewText.text = "0";
  29. incNumText.gameObject.SetActive(false);
  30. currentValue = 0;
  31. endValue = 0;
  32. }
  33. public void SetNum(int end)
  34. {
  35. //incNumText.gameObject.SetActive(true);
  36. int incNum = end - endValue;
  37. endValue = end;
  38. string incNumString = string.Empty;
  39. if (incNum > 0)
  40. {
  41. incNumString = "+";
  42. incNumText.color = Color.red;
  43. }
  44. else if (incNum < 0)
  45. {
  46. incNumText.color = Color.green;
  47. }
  48. else
  49. {
  50. incNumText.gameObject.SetActive(false);
  51. }
  52. incNumText.text = incNumString + incNum;
  53. if (m_Tweener != null)
  54. {
  55. m_Tweener.Kill();
  56. }
  57. m_Tweener = DOTween.To(GetCurrentValue, SetCurrentValue, end, 2);
  58. m_Tweener.onComplete = () =>
  59. {
  60. incNumText.gameObject.SetActive(false);
  61. };
  62. }
  63. private int GetCurrentValue()
  64. {
  65. return currentValue;
  66. }
  67. private void SetCurrentValue(int value)
  68. {
  69. currentValue = value;
  70. viewText.text = currentValue.ToString();
  71. }
  72. }
  73. }