123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using UnityEngine;
- namespace Rokid.UXR.Module
- {
- public class RKVirtualController : MonoSingleton<RKVirtualController>
- {
- public ControllerType currentControllerType = ControllerType.NORMAL;
- public ControllerType oldControllerType = ControllerType.NORMAL;
- protected override void OnSingletonInit()
- {
- this.gameObject.name = "RKVirtualController";
- DontDestroyOnLoad(this.gameObject);
- this.gameObject.hideFlags = HideFlags.HideInHierarchy;
- }
- public void Change(ControllerType type)
- {
- RKLog.KeyInfo($"==== RKVirtualController ==== Change Type {type} { SystemInfo.deviceModel}");
- if (SystemInfo.deviceModel.Contains("RG-stationPro") || SystemInfo.deviceModel.Contains("RG-stationXR2"))
- {
- type = ControllerType.Mouse;
- }
- oldControllerType = currentControllerType;
- currentControllerType = type;
- CallBridge.CallAndroid(Request.Build()
- .Name("VirtualController.registerFrag")
- .Param("type", (int)type));
- RegisterListener();
- }
- //Use RKInput instead of UnityEngine.Input for KeyEvent(Key ABXY) and Axis event, true as default.
- public void UseCustomGamePadEvent(bool isHook)
- {
- CallBridge.CallAndroid(Request.Build()
- .Name("VirtualController.setHookGamePad")
- .Param("isHook", isHook));
- }
- private static void RegisterListener()
- {
- //regist key event listener
- CallBridge.CallAndroid(
- Request.Build()
- .Name("VirtualController.setOnKeyListener")
- .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnKeyEvent")));
- //regist key down event listener
- CallBridge.CallAndroid(
- Request.Build()
- .Name("VirtualController.setOnKeyDownListener")
- .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnKeyDownEvent"))
- );
- //regist key up event listener
- CallBridge.CallAndroid(
- Request.Build()
- .Name("VirtualController.setOnKeyUpListener")
- .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnKeyUpEvent"))
- );
- CallBridge.CallAndroid(
- Request.Build()
- .Name("VirtualController.setOnAxisListener")
- .AndroidCallback(CallBridge.CreateCallback("RKInput", "OnAxisEvent"))
- );
- }
- public void DestroyVirtualController()
- {
- CallBridge.CallAndroid(
- Request.Build()
- .Name("VirtualController.dismiss"));
- }
- public void LoadWebView(string url)
- {
- if (currentControllerType == ControllerType.WEBVIEW)
- {
- CallBridge.CallAndroid(Request.Build().Name("VirtualController.loadWebViewUrl").Param("url", url));
- }
- else
- {
- RKLog.Info("RKVirtualController Please to set ControllerType to WebView");
- }
- }
- public void AutoLoadWebView(string url)
- {
- Change(ControllerType.WEBVIEW);
- LoadWebView(url);
- }
- /// <summary>
- /// 配置界面按钮的显示和隐藏
- /// </summary>
- /// <param name="menu"></param>
- /// <param name="menuBtn1"></param>
- /// <param name="menuBtn2"></param>
- /// <param name="menuBtn3"></param>
- public void ConfigMenuView(bool menu, bool menuBtn1, bool menuBtn2, bool menuBtn3)
- {
- CallBridge.CallAndroid(Request.Build()
- .Name("VirtualController.showMenuView")
- .Param("menu", menu)
- .Param("menuBtn1", menuBtn1)
- .Param("menuBtn2", menuBtn2)
- .Param("menuBtn3", menuBtn3));
- }
- }
- public enum ControllerType
- {
- NORMAL = 0,
- GAMEPAD = 1,
- IMUCTL = 2,
- PHONE3DOF = 3,
- WEBVIEW = 4,
- Mouse = 5
- }
- }
|