DeviceEventHandler.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using Rokid.UXR.Native;
  4. namespace Rokid.UXR.Module
  5. {
  6. public class DeviceEventHandler : MonoBehaviour
  7. {
  8. /// <summary>
  9. /// 当usb断开的时候是否自动退出应用
  10. /// </summary>
  11. [SerializeField]
  12. private bool quitWhenUsbDisconnect = true;
  13. /// <summary>
  14. /// 触发系统返回键的时候退出
  15. /// </summary>
  16. [SerializeField]
  17. private bool responseToEscape = true;
  18. private void Start()
  19. {
  20. RKLog.Debug("====DeviceEventHandler==== Inited Usb Devices!!! ");
  21. NativeInterface.NativeAPI.RegisterUSBStatusCallback();
  22. NativeInterface.NativeAPI.OnUSBConnect += OnUSBConnect;
  23. NativeInterface.NativeAPI.OnUSBDisConnect += OnUSBDisConnect;
  24. }
  25. private void OnDestroy()
  26. {
  27. NativeInterface.NativeAPI.UnRegisterUSBStatusCallback();
  28. NativeInterface.NativeAPI.OnUSBConnect -= OnUSBConnect;
  29. NativeInterface.NativeAPI.OnUSBDisConnect -= OnUSBDisConnect;
  30. }
  31. private void OnUSBConnect()
  32. {
  33. RKLog.Debug("====UsbEventHandler==== USBConnect !!!");
  34. }
  35. private void OnUSBDisConnect()
  36. {
  37. RKLog.Debug("====UsbEventHandler==== USB Disconnect !!!");
  38. if (quitWhenUsbDisconnect)
  39. Quit();
  40. }
  41. public void Quit()
  42. {
  43. RKLog.Debug("====DeviceEventHandler==== Quit");
  44. Application.Quit();
  45. NativeInterface.NativeAPI.KillProcess();
  46. }
  47. private void Update()
  48. {
  49. if (Input.GetKeyDown(KeyCode.Escape) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_BACK) || Input.GetKeyDown(KeyCode.JoystickButton2))
  50. {
  51. if (SceneManager.GetActiveScene().buildIndex == 0 && responseToEscape)
  52. {
  53. Quit();
  54. }
  55. else
  56. {
  57. SceneManager.LoadScene(0);
  58. }
  59. }
  60. }
  61. }
  62. }