TouchPadEventInput.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. using System;
  3. using UnityEngine.EventSystems;
  4. using Rokid.UXR.Module;
  5. namespace Rokid.UXR.Interaction
  6. {
  7. public class TouchPadEventInput : MonoSingleton<TouchPadEventInput>, IEventInput
  8. {
  9. public static Action<Vector2> OnMouseMove;
  10. public static Action OnActiveTouchPadModule;
  11. public static Action OnReleaseTouchPadModule;
  12. private Vector2 mouseDelta = Vector2.zero;
  13. public float X_MOVE_SCALE = 0.14f;
  14. public float Y_MOVE_SCALE = 0.7F;
  15. public Transform Interactor { get; set; }
  16. private bool initialize = false;
  17. public void Initialize(Transform parent)
  18. {
  19. if (Interactor == null)
  20. {
  21. GameObject go = GameObject.Find("TouchPadInteractor");
  22. if (go == null)
  23. {
  24. go = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Interactor/TouchPadInteractor"));
  25. }
  26. Interactor = go.transform;
  27. Interactor.name = "TouchPadInteractor";
  28. Interactor.SetParent(transform);
  29. }
  30. Interactor.SetParent(transform);
  31. this.transform.SetParent(parent);
  32. initialize = true;
  33. }
  34. public void Release()
  35. {
  36. OnReleaseTouchPadModule?.Invoke();
  37. Destroy(this.gameObject);
  38. }
  39. private void Start()
  40. {
  41. RKLog.Info("====TouchPadEventInput==== 注册android回调用");
  42. }
  43. private void Update()
  44. {
  45. // if (InputModuleManager.Instance.GetTouchPadActive() == false)
  46. // {
  47. // if (Input.GetKeyDown(KeyCode.Mouse0))
  48. // {
  49. // ActiveModule();
  50. // }
  51. // }
  52. if (!initialize)
  53. return;
  54. if (InputModuleManager.Instance.GetTouchPadActive())
  55. {
  56. if (Input.touchCount > 0)
  57. {
  58. mouseDelta = Input.GetTouch(0).deltaPosition;
  59. mouseDelta.x = mouseDelta.x * X_MOVE_SCALE;
  60. mouseDelta.y = -mouseDelta.y * Y_MOVE_SCALE;
  61. OnMouseMove?.Invoke(mouseDelta);
  62. }
  63. }
  64. }
  65. public void ActiveModule()
  66. {
  67. RKLog.Debug("====TouchPadEventInput==== : ActiveModule");
  68. OnActiveTouchPadModule?.Invoke();
  69. Input.ResetInputAxes();
  70. RKVirtualController.Instance.Change(ControllerType.Mouse);
  71. EventSystem.current.pixelDragThreshold = 10;
  72. }
  73. protected override void OnDestroy()
  74. {
  75. base.OnDestroy();
  76. OnMouseMove = null;
  77. }
  78. }
  79. }