Module_QuitApp.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SC.XR.Unity.Module_InputSystem;
  5. using SC.XR.Unity;
  6. using System;
  7. public class Module_QuitApp : MonoBehaviour {
  8. [SerializeField]
  9. bool UseSDKConfiguration = true;
  10. [SerializeField]
  11. bool UseLongPress = true;
  12. [Range(0, 10)]
  13. [SerializeField]
  14. int maxSupportKeyCodeAmount = 5;
  15. [SerializeField]
  16. private List<InputDeviceType> QuitByInputDeviceTypeList;
  17. [SerializeField]
  18. private List<InputKeyCode> QuitKeyCodeList;
  19. private bool UseQuitKeyCodeList = true;
  20. public static event Action<InputKeyCode, InputDevicePartBase> QuitAppCallBack;
  21. private static Module_QuitApp instance;
  22. void Awake() {
  23. if (instance) {
  24. DestroyImmediate(gameObject);
  25. return;
  26. }
  27. instance = this;
  28. DontDestroyOnLoad(gameObject);
  29. }
  30. // Start is called before the first frame update
  31. void Start() {
  32. if (QuitKeyCodeList == null) {
  33. QuitKeyCodeList = new List<InputKeyCode>();
  34. }
  35. if (UseSDKConfiguration) {
  36. if (API_Module_SDKConfiguration.HasKey("Module_QuitApp", "UseQuitKeyCodeList")) {
  37. UseQuitKeyCodeList = API_Module_SDKConfiguration.GetBool("Module_QuitApp", "UseQuitKeyCodeList", 0);
  38. }
  39. DebugMy.Log("Module_QuitApp SDKConfiguration UseQuitKeyCodeList:" + UseQuitKeyCodeList, this, true);
  40. if (UseQuitKeyCodeList == false) {
  41. QuitKeyCodeList.Clear();
  42. }
  43. if (API_Module_SDKConfiguration.HasKey("Module_QuitApp", "UseLongPress")) {
  44. UseLongPress = API_Module_SDKConfiguration.GetBool("Module_QuitApp", "UseLongPress", 1);
  45. }
  46. DebugMy.Log("Module_QuitApp SDKConfiguration UseLongPress:" + UseLongPress, this, true);
  47. int configMaxKeyAmount = 10 - QuitKeyCodeList.Count;
  48. InputKeyCode KeyCode = InputKeyCode.NULL;
  49. for (int i = configMaxKeyAmount; i >= 0; i--) {
  50. KeyCode = InputKeyCode.NULL;
  51. if (API_Module_SDKConfiguration.HasKey("Module_QuitApp", "QuitAppKey" + i)) {
  52. bool canParse = System.Enum.TryParse<InputKeyCode>(API_Module_SDKConfiguration.GetString("Module_QuitApp", "QuitAppKey" + i, ""), false, out KeyCode);
  53. if (canParse && QuitKeyCodeList.Contains(KeyCode) == false) {
  54. QuitKeyCodeList.Add(KeyCode);
  55. DebugMy.Log("Module_QuitApp SDKConfiguration Add SDKConfig QuitAppKey" + i + ": " + KeyCode, this, true);
  56. }
  57. }
  58. }
  59. }
  60. foreach (var keycode in QuitKeyCodeList) {
  61. DebugMy.Log("Module_QuitApp QuitKeyCodeList:" + keycode, this, true);
  62. }
  63. if (UseLongPress) {
  64. DispatcherBase.KeyLongDelegateRegister(AnyKeyEventDelegate);
  65. } else {
  66. DispatcherBase.KeyDownDelegateRegister(AnyKeyEventDelegate);
  67. }
  68. }
  69. // Update is called once per frame
  70. void Update() {
  71. }
  72. void Destroy() {
  73. if (instance != this)
  74. return;
  75. if (UseLongPress) {
  76. DispatcherBase.KeyLongDelegateUnRegister(AnyKeyEventDelegate);
  77. } else {
  78. DispatcherBase.KeyDownDelegateUnRegister(AnyKeyEventDelegate);
  79. }
  80. QuitKeyCodeList = null;
  81. }
  82. void AnyKeyEventDelegate(InputKeyCode keyCode, InputDevicePartBase part) {
  83. if (QuitByInputDeviceTypeList != null && QuitByInputDeviceTypeList.Count >0 && (QuitByInputDeviceTypeList.Contains(part.inputDeviceBase.inputDeviceType) == false)) {
  84. Debug.Log("Module_QuitApp QuitByInputDeviceTypeList not contain the inputDevice:" + part.inputDeviceBase.inputDeviceType);
  85. return;
  86. }
  87. if (QuitKeyCodeList != null && QuitKeyCodeList.Contains(keyCode)) {
  88. Debug.Log("Module_QuitApp: APP Quit !!! by " + keyCode + " " + part.PartType);
  89. if (QuitAppCallBack != null) {
  90. Debug.Log("Module_QuitApp Invoke QuitAppCallBack:" + keyCode + " " + part.PartType);
  91. QuitAppCallBack.Invoke(keyCode,part);
  92. }
  93. Application.Quit();
  94. }
  95. }
  96. }