uGUISliderMoveButton.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using UnityEngine.EventSystems;
  3. namespace UnityEngine.UI
  4. {
  5. [RequireComponent(typeof(RectTransform))]
  6. public class uGUISliderMoveButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler, IEventSystemHandler
  7. {
  8. public Slider targetSlider
  9. {
  10. get
  11. {
  12. return this.m_TargetSlider;
  13. }
  14. set
  15. {
  16. this.m_TargetSlider = value;
  17. }
  18. }
  19. public float moveSpeed
  20. {
  21. get
  22. {
  23. return this.m_MoveSpeed;
  24. }
  25. set
  26. {
  27. this.m_MoveSpeed = value;
  28. }
  29. }
  30. private bool isUpdateSlider { get; set; }
  31. private bool isAccel { get; set; }
  32. private float speed { get; set; }
  33. public float decelerationRate
  34. {
  35. get
  36. {
  37. return this.m_DecelerationRate;
  38. }
  39. set
  40. {
  41. this.m_DecelerationRate = value;
  42. }
  43. }
  44. protected override void Start()
  45. {
  46. base.Start();
  47. }
  48. protected virtual void LateUpdate()
  49. {
  50. if (this.m_TargetSlider == null)
  51. {
  52. return;
  53. }
  54. if (this.isAccel)
  55. {
  56. this.speed += (this.moveSpeed - this.speed) * Time.unscaledDeltaTime * Time.unscaledDeltaTime;
  57. }
  58. this.speed *= Mathf.Pow(this.decelerationRate, Time.unscaledDeltaTime);
  59. if (Mathf.Abs(this.speed) <= 0.01f * Time.unscaledDeltaTime)
  60. {
  61. this.isUpdateSlider = false;
  62. return;
  63. }
  64. if (!this.isUpdateSlider)
  65. {
  66. return;
  67. }
  68. this.m_TargetSlider.value += this.speed;
  69. }
  70. public void OnPointerDown(PointerEventData eventData)
  71. {
  72. this.isUpdateSlider = true;
  73. this.isAccel = true;
  74. }
  75. public void OnPointerUp(PointerEventData eventData)
  76. {
  77. this.isAccel = false;
  78. }
  79. public void OnDrag(PointerEventData eventData)
  80. {
  81. }
  82. protected void OnReset()
  83. {
  84. if (this.targetSlider == null)
  85. {
  86. this.targetSlider = base.GetComponentInParent<Slider>();
  87. }
  88. }
  89. [SerializeField]
  90. private Slider m_TargetSlider;
  91. [SerializeField]
  92. private float m_MoveSpeed;
  93. [SerializeField]
  94. private float m_DecelerationRate = 0.1f;
  95. }
  96. }