NGUI中按钮连按响应 是需要提前设置的,接下来我们就来看看NGUI中按钮连按响应的实现过程。

Unity对于按钮手势操作有第三方插件FingerGestures。但是考虑到我们项目本身就是用的NGUI,它里面已经封装了按钮响应的处理,虽然没有长按的处理,但似乎是用FingerGestures有点浪费,所以就自己写了以下代码,分享给大家,仅供参考。

这里面定义了两个按钮的长按事件,分别对鼠标和触摸进行了处理。代码中红色标注了以增加按钮为例的主要代码。

  1. private enum ButtonState
  2. {
  3. OnPress,
  4. OnRelease,
  5. OnLongPress,
  6. NONE,
  7. }
  8. private bool canLongPress = false;
  9. private float duration = 0.5f;  //长按认证时间,当点击按钮超过此时间时,认为长按//
  10. private float changeTime = 0.2f;  //长按后,数据改变时间间隔//
  11. private float startTime = 0;  //记录开始点击的时间//
  12. private ButtonState addBtnState = ButtonState.NONE;
  13. /// <summary>
  14. /// Determines whether this instance is button hover the specified btn.判断现在是否经过此物体,辅助获取当前点击物体//
  15. /// </summary>
  16. /// <returns>
  17. /// <c>true</c> if this instance is button hover the specified btn; otherwise, <c>false</c>.
  18. /// </returns>
  19. /// <param name='btn'>
  20. /// If set to <c>true</c> button.
  21. /// </param>
  22. bool IsButtonHover(GameObject btn)
  23. {
  24. if(UICamera.hoveredObject != null && UICamera.hoveredObject.Equals(btn))
  25. {
  26. return true;
  27. }
  28. return false;
  29. }
  30. /// <summary>
  31. /// Raises the touch event.  触摸长按处理函数//
  32. /// </summary>
  33. void OnTouch(){
  34. if(Input.touchCount > 0)
  35. {//有触摸//
  36. Touch touch = Input.GetTouch(0);   //单点触摸//
  37. if(touch.phase == TouchPhase.Began)
  38. {  //按下//
  39. if(IsButtonHover(addBtn)){//点击增加按钮//
  40. addBtnState = ButtonState.OnPress;//按下状态//
  41. startTime = Time.time;//记录按下时间,用来判断是否长按//
  42. }
  43. else if(IsButtonHover(reduceBtn))
  44. {  //点击减少按钮//
  45. reduceState = ButtonState.OnPress;
  46. startTime = Time.time;
  47. }
  48. else{
  49. }
  50. }
  51. else if(touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended){//抬起//
  52. addBtnState = ButtonState.NONE;
  53. reduceState = ButtonState.NONE;
  54. }
  55. else
  56. {//保持按下状态//
  57. if(IsButtonHover(addBtn))
  58. {//点击增加按钮//
  59. reduceState = ButtonState.NONE;
  60. if(addBtnState == ButtonState.OnPress)
  61. {  //检测是否为长按事件//
  62. float deltaTime = Time.time - startTime;
  63. if(deltaTime >= duration)
  64. {//当按下的时间超过长按确认时间,表示为长按//
  65. addBtnState = ButtonState.OnLongPress;
  66. startTime = Time.time;
  67. }
  68. }
  69. else if(addBtnState == ButtonState.OnLongPress)
  70. {  //长按事件的处理//
  71. float deltaTime = Time.time - startTime;  //事件间隔//
  72. if(deltaTime >= changeTime)
  73. { //当按下时间到达处理的时间//
  74. int curNum = int.Parse(wnd.GetInputText());
  75. if(curNum < maxNum){
  76. preNum = curNum + 1;
  77. wnd.SetInputText(preNum.ToString());
  78. wnd.SetPriceLabelOnDialog(preNum * price);
  79. }
  80. startTime = Time.time;
  81. }
  82. }
  83. }
  84. else if(IsButtonHover(reduceBtn))
  85. {  //点击减少按钮//
  86. addBtnState = ButtonState.NONE;
  87. if(reduceState == ButtonState.OnPress)
  88. {
  89. float deltaTime = Time.time - startTime;
  90. if(deltaTime >= duration){
  91. reduceState = ButtonState.OnLongPress;
  92. startTime = Time.time;
  93. }
  94. }
  95. else if(reduceState == ButtonState.OnLongPress)
  96. {
  97. float deltaTime = Time.time - startTime;
  98. if(deltaTime >= changeTime)
  99. {
  100. int curNum = int.Parse(wnd.GetInputText());
  101. if(curNum > 0)
  102. {
  103. preNum = curNum - 1;
  104. wnd.SetInputText(preNum.ToString());
  105. wnd.SetPriceLabelOnDialog(preNum * price);
  106. }
  107. startTime = Time.time;
  108. }
  109. }
  110. }
  111. else
  112. {
  113. addBtnState = ButtonState.NONE;
  114. reduceState = ButtonState.NONE;
  115. }
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Raises the mouse event.鼠标点击长按函数//
  121. /// </summary>
  122. void OnMouse(){
  123. //鼠标//
  124. if(Input.GetMouseButtonDown(0))
  125. {   //鼠标按下//
  126. if(IsButtonHover(addBtn))
  127. {//点击增加按钮//
  128. addBtnState = ButtonState.OnPress;
  129. startTime = Time.time;
  130. }
  131. else if(IsButtonHover(reduceBtn))
  132. {  //点击减少按钮//
  133. reduceState = ButtonState.OnPress;
  134. startTime = Time.time;
  135. }
  136. else
  137. {
  138. }
  139. }
  140. else if(Input.GetMouseButtonUp(0))
  141. {
  142. addBtnState = ButtonState.NONE;
  143. reduceState = ButtonState.NONE;
  144. }
  145. else
  146. {
  147. if(IsButtonHover(addBtn))
  148. {//点击增加按钮//
  149. reduceState = ButtonState.NONE;
  150. if(addBtnState == ButtonState.OnPress)
  151. {
  152. float deltaTime = Time.time - startTime;
  153. if(deltaTime >= duration)
  154. {
  155. addBtnState = ButtonState.OnLongPress;
  156. startTime = Time.time;
  157. }
  158. }
  159. else if(addBtnState == ButtonState.OnLongPress)
  160. {
  161. float deltaTime = Time.time - startTime;
  162. if(deltaTime >= changeTime)
  163. {
  164. int curNum = int.Parse(wnd.GetInputText());
  165. if(curNum < maxNum)
  166. {
  167. preNum = curNum + 1;
  168. wnd.SetInputText(preNum.ToString());
  169. wnd.SetPriceLabelOnDialog(preNum * price);
  170. }
  171. startTime = Time.time;
  172. }
  173. }
  174. }
  175. else if(IsButtonHover(reduceBtn))
  176. {  //点击减少按钮//
  177. addBtnState = ButtonState.NONE;
  178. if(reduceState == ButtonState.OnPress)
  179. {
  180. float deltaTime = Time.time - startTime;
  181. if(deltaTime >= duration)
  182. {
  183. reduceState = ButtonState.OnLongPress;
  184. startTime = Time.time;
  185. }
  186. }
  187. else if(reduceState == ButtonState.OnLongPress)
  188. {
  189. float deltaTime = Time.time - startTime;
  190. if(deltaTime >= changeTime){
  191. int curNum = int.Parse(wnd.GetInputText());
  192. if(curNum > 0){
  193. preNum = curNum - 1;
  194. wnd.SetInputText(preNum.ToString());
  195. wnd.SetPriceLabelOnDialog(preNum * price);
  196. }
  197. startTime = Time.time;
  198. }
  199. }
  200. }
  201. else
  202. {
  203. addBtnState = ButtonState.NONE;
  204. reduceState = ButtonState.NONE;
  205. }
  206. }
  207. }
  208. void Update()
  209. {
  210. //增加,减少按钮长按监听//
  211. if(canLongPress)
  212. {
  213. if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  214. {
  215. OnTouch();
  216. }
  217. else
  218. {
  219. OnMouse();
  220. }
  221. }
  222. }