RKNativeInput.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using UnityEngine;
  3. namespace Rokid.UXR.Module
  4. {
  5. /// <summary>
  6. /// Android Native Input
  7. /// </summary>
  8. public class RKNativeInput : MonoSingleton<RKNativeInput>
  9. {
  10. bool isDebug = false;
  11. private bool m_Init = false;
  12. public RKKeyEvent keyState;
  13. public RKKeyEvent keyDownState;
  14. public RKKeyEvent keyUpState;
  15. public float lrHorizontalAxis = 0;
  16. public float lrHVerticalAxis = 0;
  17. public float rrHorizontalAxis = 0;
  18. public float rrHVerticalAxis = 0;
  19. private void LateUpdate()
  20. {
  21. keyDownState = 0;
  22. keyUpState = 0;
  23. }
  24. protected override void OnSingletonInit()
  25. {
  26. DontDestroyOnLoad(this.gameObject);
  27. Initialized();
  28. this.gameObject.name = "RKInput";
  29. this.gameObject.hideFlags = HideFlags.HideInHierarchy;
  30. }
  31. /// <summary>
  32. /// 初始化
  33. /// </summary>
  34. public void Initialized()
  35. {
  36. if (m_Init) return;
  37. m_Init = true;
  38. }
  39. public bool GetKey(RKKeyEvent key)
  40. {
  41. return (keyState & key) != 0;
  42. }
  43. public bool GetKeyDown(RKKeyEvent key)
  44. {
  45. return (keyDownState & key) != 0;
  46. }
  47. public bool GetKeyUp(RKKeyEvent key)
  48. {
  49. return (keyUpState & key) != 0;
  50. }
  51. /**
  52. * return axis value from -1 to 1;
  53. */
  54. public float GetAxis(AxisEvent axisName)
  55. {
  56. switch (axisName)
  57. {
  58. case AxisEvent.Horizontal_Left:
  59. return lrHorizontalAxis;
  60. case AxisEvent.Vertical_Left:
  61. return lrHVerticalAxis;
  62. case AxisEvent.Horizontal_Right:
  63. return rrHorizontalAxis;
  64. case AxisEvent.Vertical_Right:
  65. return rrHVerticalAxis;
  66. default:
  67. return 0f;
  68. }
  69. }
  70. //Android Key Event Callback
  71. void OnKeyEvent(string keyEvent)
  72. {
  73. RKLog.Debug("UXR-PLUGIN::OnKeyEvent = " + keyState);
  74. keyState = (RKKeyEvent)int.Parse(keyEvent);
  75. }
  76. void OnKeyDownEvent(string keyDownEvent)
  77. {
  78. RKLog.Debug("UXR-PLUGIN::keyDownEvent = " + keyDownState);
  79. keyDownState |= (RKKeyEvent)int.Parse(keyDownEvent);
  80. }
  81. void OnKeyUpEvent(string keyUpEvent)
  82. {
  83. RKLog.Debug("UXR-PLUGIN::keyUpEvent = " + keyUpState);
  84. keyUpState |= (RKKeyEvent)int.Parse(keyUpEvent);
  85. }
  86. void OnAxisEvent(string axisEvent)
  87. {
  88. RKLog.Debug("UXR-PLUGIN::axisEvent = " + axisEvent);
  89. string[] strAxis = axisEvent.Split('|');
  90. if ("LR".Equals(strAxis[0]))
  91. {
  92. lrHorizontalAxis = float.Parse(strAxis[1]);
  93. lrHVerticalAxis = float.Parse(strAxis[2]);
  94. }
  95. else if ("RR".Equals(strAxis[0]))
  96. {
  97. rrHorizontalAxis = float.Parse(strAxis[1]);
  98. rrHVerticalAxis = float.Parse(strAxis[2]);
  99. }
  100. }
  101. }
  102. public enum RKKeyEvent
  103. {
  104. KEY_LEFT = 1 << 0,
  105. KEY_RIGHT = 1 << 1,
  106. KEY_UP = 1 << 2,
  107. KEY_DOWN = 1 << 3,
  108. KEY_BACK = 1 << 4,
  109. KEY_HOME = 1 << 5,
  110. KEY_OK = 1 << 6,
  111. KEY_X = 1 << 7,
  112. KEY_Y = 1 << 8,
  113. KEY_B = 1 << 9,
  114. KEY_A = 1 << 10,
  115. KEY_SELECT = 1 << 11,
  116. KEY_START = 1 << 12,
  117. KEY_RR_LEFT = 1 << 13,
  118. KEY_RR_RIGHT = 1 << 14,
  119. KEY_RR_UP = 1 << 15,
  120. KEY_RR_DOWN = 1 << 16,
  121. KEY_RESET = 1 << 17,
  122. KEY_TOOLS = 1 << 18,
  123. KEY_POWER = 1 << 19,
  124. KEY_RESET_RAY = 1 << 20,
  125. KEY_MENU_BTN1_LOCK = 1 << 21,
  126. KEY_MENU_BTN1_UNLOCK = 1 << 22,
  127. KEY_MENU_BTN2 = 1 << 23,
  128. KEY_MENU_BTN3 = 1 << 24,
  129. KEY_MOUSE_SECONDARY = 1 << 25,
  130. KEY_MOUSE_THERIARY = 1 << 26,
  131. }
  132. public enum AxisEvent
  133. {
  134. Horizontal_Left,
  135. Vertical_Left,
  136. Horizontal_Right,
  137. Vertical_Right
  138. }
  139. }