using System.Collections; using System.Collections.Generic; using UnityEngine; using SC.XR.Unity.Module_InputSystem; using SC.XR.Unity; using System; public class Module_QuitApp : MonoBehaviour { [SerializeField] bool UseSDKConfiguration = true; [SerializeField] bool UseLongPress = true; [Range(0, 10)] [SerializeField] int maxSupportKeyCodeAmount = 5; [SerializeField] private List QuitByInputDeviceTypeList; [SerializeField] private List QuitKeyCodeList; private bool UseQuitKeyCodeList = true; public static event Action QuitAppCallBack; private static Module_QuitApp instance; void Awake() { if (instance) { DestroyImmediate(gameObject); return; } instance = this; DontDestroyOnLoad(gameObject); } // Start is called before the first frame update void Start() { if (QuitKeyCodeList == null) { QuitKeyCodeList = new List(); } if (UseSDKConfiguration) { if (API_Module_SDKConfiguration.HasKey("Module_QuitApp", "UseQuitKeyCodeList")) { UseQuitKeyCodeList = API_Module_SDKConfiguration.GetBool("Module_QuitApp", "UseQuitKeyCodeList", 0); } DebugMy.Log("Module_QuitApp SDKConfiguration UseQuitKeyCodeList:" + UseQuitKeyCodeList, this, true); if (UseQuitKeyCodeList == false) { QuitKeyCodeList.Clear(); } if (API_Module_SDKConfiguration.HasKey("Module_QuitApp", "UseLongPress")) { UseLongPress = API_Module_SDKConfiguration.GetBool("Module_QuitApp", "UseLongPress", 1); } DebugMy.Log("Module_QuitApp SDKConfiguration UseLongPress:" + UseLongPress, this, true); int configMaxKeyAmount = 10 - QuitKeyCodeList.Count; InputKeyCode KeyCode = InputKeyCode.NULL; for (int i = configMaxKeyAmount; i >= 0; i--) { KeyCode = InputKeyCode.NULL; if (API_Module_SDKConfiguration.HasKey("Module_QuitApp", "QuitAppKey" + i)) { bool canParse = System.Enum.TryParse(API_Module_SDKConfiguration.GetString("Module_QuitApp", "QuitAppKey" + i, ""), false, out KeyCode); if (canParse && QuitKeyCodeList.Contains(KeyCode) == false) { QuitKeyCodeList.Add(KeyCode); DebugMy.Log("Module_QuitApp SDKConfiguration Add SDKConfig QuitAppKey" + i + ": " + KeyCode, this, true); } } } } foreach (var keycode in QuitKeyCodeList) { DebugMy.Log("Module_QuitApp QuitKeyCodeList:" + keycode, this, true); } if (UseLongPress) { DispatcherBase.KeyLongDelegateRegister(AnyKeyEventDelegate); } else { DispatcherBase.KeyDownDelegateRegister(AnyKeyEventDelegate); } } // Update is called once per frame void Update() { } void Destroy() { if (instance != this) return; if (UseLongPress) { DispatcherBase.KeyLongDelegateUnRegister(AnyKeyEventDelegate); } else { DispatcherBase.KeyDownDelegateUnRegister(AnyKeyEventDelegate); } QuitKeyCodeList = null; } void AnyKeyEventDelegate(InputKeyCode keyCode, InputDevicePartBase part) { if (QuitByInputDeviceTypeList != null && QuitByInputDeviceTypeList.Count >0 && (QuitByInputDeviceTypeList.Contains(part.inputDeviceBase.inputDeviceType) == false)) { Debug.Log("Module_QuitApp QuitByInputDeviceTypeList not contain the inputDevice:" + part.inputDeviceBase.inputDeviceType); return; } if (QuitKeyCodeList != null && QuitKeyCodeList.Contains(keyCode)) { Debug.Log("Module_QuitApp: APP Quit !!! by " + keyCode + " " + part.PartType); if (QuitAppCallBack != null) { Debug.Log("Module_QuitApp Invoke QuitAppCallBack:" + keyCode + " " + part.PartType); QuitAppCallBack.Invoke(keyCode,part); } Application.Quit(); } } }