BackKeyDialog.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using SC.InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. public class BackKeyDialog : PointerDelegate
  7. {
  8. public BackKeyDialogUI UI;
  9. static BackKeyDialog instant;
  10. void Awake() {
  11. if(instant) {
  12. DestroyImmediate(gameObject);
  13. return;
  14. }
  15. instant = this;
  16. DontDestroyOnLoad(gameObject);
  17. }
  18. void Start() {
  19. Input.backButtonLeavesApp = false;
  20. SceneManager.activeSceneChanged += ChangedActiveScene;
  21. }
  22. void OnDestroy() {
  23. SceneManager.activeSceneChanged -= ChangedActiveScene;
  24. }
  25. protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part) {
  26. base.partAnyKeyDownDelegate(keyCode, part);
  27. if(keyCode != InputKeyCode.Back)
  28. return;
  29. if(Input.backButtonLeavesApp == true)
  30. return;
  31. if(!UI)
  32. return;
  33. if(UI.gameObject.activeSelf == true) {
  34. Application.Quit();
  35. } else {
  36. UI.gameObject.SetActive(true);
  37. }
  38. }
  39. void Update() {
  40. #if UNITY_EDITOR
  41. if(Input.GetKeyDown(KeyCode.Escape)) {
  42. partAnyKeyDownDelegate(InputKeyCode.Back, null);
  43. }
  44. #endif
  45. }
  46. private void ChangedActiveScene( Scene current, Scene next ) {
  47. Input.backButtonLeavesApp = false;
  48. }
  49. }