当前位置:   article > 正文

Unity 点击、双击、连击、长按 事件检测(支持PC和移动端)_鼠标连击检测

鼠标连击检测

InputDefine 类:(公共变量、常量、枚举、事件)定义

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class InputDefine
  5. {
  6. /// <summary>两次点击的时间间隔</summary>
  7. public const float DOUBLE_CLICK_RATE = 0.3f;
  8. /// <summary>连续按住0.5s后判定为长按</summary>
  9. public const float LONG_PRESS_SCALE = 0.5f;
  10. /// <summary>判断是否点击在UI上面</summary>
  11. public static bool IsTouchUI()
  12. {
  13. if (UnityEngine.EventSystems.EventSystem.current == null) return false;
  14. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  15. {
  16. if (Input.touchCount < 1) return false;
  17. if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) return true;
  18. }
  19. else
  20. {
  21. if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return true;
  22. }
  23. return false;
  24. }
  25. }
  26. /// <summary>输入事件类型</summary>
  27. public enum EnumInputEventType
  28. {
  29. /// <summary>没有事件</summary>
  30. None,
  31. /// <summary>单击</summary>
  32. Click,
  33. /// <summary>双击</summary>
  34. DoubleClick,
  35. /// <summary>滑动</summary>
  36. //Slide,
  37. /// <summary>长按</summary>
  38. LongPress,
  39. }
  40. /// <summary>
  41. /// 输入事件
  42. /// </summary>
  43. /// <param name="isTouchUI">是否触摸到UI</param>
  44. /// <param name="keyCode">(PC端)0表示左键,1表示右键,2表示中键</param>
  45. /// <param name="enumInputEventType">输入事件类型</param>
  46. public delegate void DelInputEvent(bool isTouchUI, int keyCode, EnumInputEventType enumInputEventType);
  47. /// <summary>
  48. /// 鼠标点击
  49. /// </summary>
  50. /// <param name="isTouchUI">是否触摸到UI</param>
  51. /// <param name="keyCode">(PC端)0表示左键,1表示右键,2表示中键</param>
  52. /// <param name="clickCount">连续点次数</param>
  53. public delegate void DelClickEvent(bool isTouchUI, int keyCode, Vector2 clickPs, int clickCount);

MouseEvent 类:鼠标事件(PC端)

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. /// <summary>鼠标事件</summary>
  6. public class MouseEvent : MonoBehaviour
  7. {
  8. public DelInputEvent onInputEventChange;
  9. public DelClickEvent onClickEvent;
  10. private MouseParam[] mouseParams = new MouseParam[] { new MouseParam(), new MouseParam(), new MouseParam() };
  11. private void Update()
  12. {
  13. addMouseInputEventTypeJudge();
  14. }
  15. /// <summary>添加</summary>
  16. private void addMouseInputEventTypeJudge()
  17. {
  18. for(int i=0;i< mouseParams.Length;i++)
  19. {
  20. mouseInputEventTypeJudge(i);
  21. }
  22. }
  23. /// <summary>鼠标左键输入事件判定</summary>
  24. private void mouseInputEventTypeJudge(int mouseKeyCode)
  25. {
  26. //鼠标按下
  27. if (Input.GetMouseButtonDown(mouseKeyCode))
  28. {
  29. mouseParams[mouseKeyCode].startTime = Time.realtimeSinceStartup;
  30. }
  31. //鼠标按住
  32. if (Input.GetMouseButton(mouseKeyCode))
  33. {
  34. mouseParams[mouseKeyCode].holdTime = Time.realtimeSinceStartup - mouseParams[mouseKeyCode].startTime;
  35. if (mouseParams[mouseKeyCode].holdTime >= InputDefine.LONG_PRESS_SCALE)
  36. {
  37. if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(),mouseKeyCode,EnumInputEventType.LongPress);
  38. }
  39. }
  40. //鼠标抬起
  41. if (Input.GetMouseButtonUp(mouseKeyCode))
  42. {
  43. if (mouseParams[mouseKeyCode].holdTime >= InputDefine.LONG_PRESS_SCALE) return;
  44. if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(), mouseKeyCode,EnumInputEventType.Click);
  45. mouseParams[mouseKeyCode].t2 = Time.realtimeSinceStartup;
  46. if (mouseParams[mouseKeyCode].t2 - mouseParams[mouseKeyCode].t1 < InputDefine.DOUBLE_CLICK_RATE)
  47. {
  48. //Debug.Log(mouseKeyCode + ":双击!");
  49. mouseParams[mouseKeyCode].clickCount++;
  50. if (onInputEventChange != null) onInputEventChange(InputDefine.IsTouchUI(), mouseKeyCode,EnumInputEventType.DoubleClick);
  51. }
  52. else
  53. {
  54. mouseParams[mouseKeyCode].clickCount = 1;
  55. }
  56. mouseParams[mouseKeyCode].t1 = mouseParams[mouseKeyCode].t2;
  57. if (onClickEvent != null) onClickEvent(InputDefine.IsTouchUI(), mouseKeyCode,Input.mousePosition, mouseParams[mouseKeyCode].clickCount);
  58. }
  59. }
  60. public struct MouseParam
  61. {
  62. public float startTime;
  63. public float holdTime;
  64. public float t1;
  65. public float t2;
  66. public int clickCount;
  67. }
  68. }

TouchEvent 类:触摸事件(移动端)

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>触摸事件</summary>
  5. public class TouchEvent : MonoBehaviour
  6. {
  7. public DelClickEvent onClickEvent;
  8. private void Update()
  9. {
  10. if (Input.touchCount > 0)
  11. {
  12. for (int i = 0; i < Input.touchCount; i++)
  13. {
  14. Touch touch = Input.GetTouch(i);
  15. if (touch.phase == TouchPhase.Began)
  16. {
  17. if (onClickEvent != null) onClickEvent(InputDefine.IsTouchUI(), i, touch.position, touch.tapCount);
  18. //Debug.Log(i + "---" + "touch.tapCount:" + touch.tapCount+ "---touch.position:"+ touch.position);
  19. }
  20. }
  21. }
  22. }
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/97858
推荐阅读
相关标签
  

闽ICP备14008679号