using UnityEngine; using UnityEngine.SceneManagement; using Rokid.UXR.Native; namespace Rokid.UXR.Module { public class DeviceEventHandler : MonoBehaviour { /// /// 当usb断开的时候是否自动退出应用 /// [SerializeField] private bool quitWhenUsbDisconnect = true; /// /// 触发系统返回键的时候退出 /// [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); } } } } }