RepeatButton.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using LuaInterface;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace UnityEngine.UI
  6. {
  7. [RequireComponent(typeof(Button))]
  8. public class RepeatButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
  9. {
  10. public int[] stage;
  11. public float[] delay;
  12. public int[] effTimes;
  13. [HideInInspector]
  14. public Button button;
  15. private float curEffTime;
  16. private bool isDown = false;
  17. private float startTime;
  18. private float lastIsDownTime;
  19. LuaTable table;
  20. LuaFunction effFunc;
  21. [NoToLua]
  22. void Start()
  23. {
  24. button = GetComponent<Button>();
  25. //button.onClick.AddListener(OnButtonClick);
  26. }
  27. public void AddRepeatClickEventListener(LuaFunction func)
  28. {
  29. effFunc = func;
  30. }
  31. public void AddRepeatClickEventListener(LuaTable table, LuaFunction func)
  32. {
  33. this.table = table;
  34. effFunc = func;
  35. }
  36. void OnButtonClick()
  37. {
  38. }
  39. [NoToLua]
  40. void Update()
  41. {
  42. if (isDown)
  43. {
  44. int index = 0;
  45. if (stage.Length > 0)
  46. {
  47. for (int i = stage.Length - 1; i >= 0; --i)
  48. {
  49. float time = Time.time - startTime;
  50. if (time >= stage[i])
  51. {
  52. index = i;
  53. curEffTime = effTimes[index];
  54. break;
  55. }
  56. }
  57. }
  58. if (Time.time - lastIsDownTime > delay[index])
  59. {
  60. Effecive(index);
  61. }
  62. }
  63. }
  64. void Effecive(int index)
  65. {
  66. lastIsDownTime = Time.time;
  67. if (curEffTime > 0)
  68. {
  69. curEffTime -= 1;
  70. if (curEffTime == 0)
  71. {
  72. if (index >= stage.Length - 1)
  73. {
  74. isDown = false;
  75. }
  76. }
  77. }
  78. if (effFunc != null)
  79. {
  80. if (table == null)
  81. effFunc.Call(index, !isDown);
  82. else
  83. effFunc.Call(table, index, !isDown);
  84. }
  85. }
  86. [NoToLua]
  87. public void OnPointerDown(PointerEventData eventData)
  88. {
  89. isDown = true;
  90. startTime = Time.time;
  91. lastIsDownTime = startTime;
  92. int index = 0;
  93. curEffTime = effTimes[index];
  94. Effecive(0);
  95. }
  96. [NoToLua]
  97. public void OnPointerUp(PointerEventData eventData)
  98. {
  99. isDown = false;
  100. }
  101. [NoToLua]
  102. public void OnPointerExit(PointerEventData eventData)
  103. {
  104. isDown = false;
  105. }
  106. void OnDisable()
  107. {
  108. isDown = false;
  109. }
  110. public void ImmediatelyEffect()
  111. {
  112. if (effFunc != null)
  113. {
  114. if (table == null)
  115. effFunc.Call(0, !isDown);
  116. else
  117. effFunc.Call(table, 0, !isDown);
  118. }
  119. }
  120. }
  121. }