12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Rokid.UXR.Native;
- namespace Rokid.UXR.Module
- {
- public class DeviceEventHandler : MonoBehaviour
- {
- /// <summary>
- /// 当usb断开的时候是否自动退出应用
- /// </summary>
- [SerializeField]
- private bool quitWhenUsbDisconnect = true;
- /// <summary>
- /// 触发系统返回键的时候退出
- /// </summary>
- [SerializeField]
- private bool responseToEscape = true;
- private void Start()
- {
- RKLog.Debug("====DeviceEventHandler==== Inited Usb Devices!!! ");
- NativeInterface.NativeAPI.RegisterUSBStatusCallback();
- NativeInterface.NativeAPI.OnUSBConnect += OnUSBConnect;
- NativeInterface.NativeAPI.OnUSBDisConnect += OnUSBDisConnect;
- }
- private void OnDestroy()
- {
- NativeInterface.NativeAPI.UnRegisterUSBStatusCallback();
- NativeInterface.NativeAPI.OnUSBConnect -= OnUSBConnect;
- NativeInterface.NativeAPI.OnUSBDisConnect -= OnUSBDisConnect;
- }
- private void OnUSBConnect()
- {
- RKLog.Debug("====UsbEventHandler==== USBConnect !!!");
- }
- private void OnUSBDisConnect()
- {
- RKLog.Debug("====UsbEventHandler==== USB Disconnect !!!");
- if (quitWhenUsbDisconnect)
- Quit();
- }
- public void Quit()
- {
- RKLog.Debug("====DeviceEventHandler==== Quit");
- Application.Quit();
- NativeInterface.NativeAPI.KillProcess();
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_BACK) || Input.GetKeyDown(KeyCode.JoystickButton2))
- {
- if (SceneManager.GetActiveScene().buildIndex == 0 && responseToEscape)
- {
- Quit();
- }
- else
- {
- SceneManager.LoadScene(0);
- }
- }
- }
- }
- }
|