RKVirtualController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. namespace Rokid.UXR.Module
  3. {
  4. public class RKVirtualController : MonoSingleton<RKVirtualController>
  5. {
  6. public ControllerType currentControllerType = ControllerType.NORMAL;
  7. public ControllerType oldControllerType = ControllerType.NORMAL;
  8. protected override void OnSingletonInit()
  9. {
  10. this.gameObject.name = "RKVirtualController";
  11. DontDestroyOnLoad(this.gameObject);
  12. this.gameObject.hideFlags = HideFlags.HideInHierarchy;
  13. }
  14. public void Change(ControllerType type)
  15. {
  16. RKLog.KeyInfo($"==== RKVirtualController ==== Change Type {type} { SystemInfo.deviceModel}");
  17. if (SystemInfo.deviceModel.Contains("RG-stationPro") || SystemInfo.deviceModel.Contains("RG-stationXR2"))
  18. {
  19. type = ControllerType.Mouse;
  20. }
  21. oldControllerType = currentControllerType;
  22. currentControllerType = type;
  23. CallBridge.CallAndroid(Request.Build()
  24. .Name("VirtualController.registerFrag")
  25. .Param("type", (int)type));
  26. RegisterListener();
  27. }
  28. //Use RKInput instead of UnityEngine.Input for KeyEvent(Key ABXY) and Axis event, true as default.
  29. public void UseCustomGamePadEvent(bool isHook)
  30. {
  31. CallBridge.CallAndroid(Request.Build()
  32. .Name("VirtualController.setHookGamePad")
  33. .Param("isHook", isHook));
  34. }
  35. private static void RegisterListener()
  36. {
  37. //regist key event listener
  38. CallBridge.CallAndroid(
  39. Request.Build()
  40. .Name("VirtualController.setOnKeyListener")
  41. .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnKeyEvent")));
  42. //regist key down event listener
  43. CallBridge.CallAndroid(
  44. Request.Build()
  45. .Name("VirtualController.setOnKeyDownListener")
  46. .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnKeyDownEvent"))
  47. );
  48. //regist key up event listener
  49. CallBridge.CallAndroid(
  50. Request.Build()
  51. .Name("VirtualController.setOnKeyUpListener")
  52. .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnKeyUpEvent"))
  53. );
  54. CallBridge.CallAndroid(
  55. Request.Build()
  56. .Name("VirtualController.setOnAxisListener")
  57. .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnAxisEvent"))
  58. );
  59. }
  60. public void DestroyVirtualController()
  61. {
  62. CallBridge.CallAndroid(
  63. Request.Build()
  64. .Name("VirtualController.dismiss"));
  65. }
  66. public void LoadWebView(string url)
  67. {
  68. if (currentControllerType == ControllerType.WEBVIEW)
  69. {
  70. CallBridge.CallAndroid(Request.Build().Name("VirtualController.loadWebViewUrl").Param("url", url));
  71. }
  72. else
  73. {
  74. RKLog.Info("RKVirtualController Please to set ControllerType to WebView");
  75. }
  76. }
  77. public void AutoLoadWebView(string url)
  78. {
  79. Change(ControllerType.WEBVIEW);
  80. LoadWebView(url);
  81. }
  82. /// <summary>
  83. /// 配置界面按钮的显示和隐藏
  84. /// </summary>
  85. /// <param name="menu"></param>
  86. /// <param name="menuBtn1"></param>
  87. /// <param name="menuBtn2"></param>
  88. /// <param name="menuBtn3"></param>
  89. public void ConfigMenuView(bool menu, bool menuBtn1, bool menuBtn2, bool menuBtn3)
  90. {
  91. CallBridge.CallAndroid(Request.Build()
  92. .Name("VirtualController.showMenuView")
  93. .Param("menu", menu)
  94. .Param("menuBtn1", menuBtn1)
  95. .Param("menuBtn2", menuBtn2)
  96. .Param("menuBtn3", menuBtn3));
  97. }
  98. }
  99. public enum ControllerType
  100. {
  101. NORMAL = 0,
  102. GAMEPAD = 1,
  103. IMUCTL = 2,
  104. PHONE3DOF = 3,
  105. WEBVIEW = 4,
  106. Mouse = 5
  107. }
  108. }