QuitButtonBehaviour.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // /******************************************************************************
  2. // * File: QuitButtonBehaviour.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using System.Collections;
  8. using UnityEngine.UI;
  9. using UnityEngine;
  10. namespace QCHT.Samples.Menu
  11. {
  12. public class QuitButtonBehaviour : MonoBehaviour
  13. {
  14. [SerializeField] private float waitDuration = 3f;
  15. [SerializeField] private Text text;
  16. [SerializeField] private string quitButtonText;
  17. [SerializeField] private string quitConfirmPopUpText;
  18. private bool _isWaitingForConfirm;
  19. public void Start() => ActiveQuit();
  20. private void Update()
  21. {
  22. #if ENABLE_LEGACY_INPUT_MANAGER
  23. if (Input.GetKeyDown(KeyCode.Escape))
  24. {
  25. Quit();
  26. }
  27. #endif
  28. }
  29. public void OnButtonClick()
  30. {
  31. if (_isWaitingForConfirm)
  32. {
  33. Quit();
  34. return;
  35. }
  36. _isWaitingForConfirm = true;
  37. text.text = quitConfirmPopUpText;
  38. StartCoroutine(ResetQuitButton());
  39. }
  40. private void ActiveQuit()
  41. {
  42. text.text = quitButtonText;
  43. _isWaitingForConfirm = false;
  44. }
  45. private IEnumerator ResetQuitButton()
  46. {
  47. yield return new WaitForSeconds(waitDuration);
  48. ActiveQuit();
  49. }
  50. private void Quit()
  51. {
  52. #if UNITY_EDITOR
  53. UnityEditor.EditorApplication.isPlaying = false;
  54. #else
  55. Application.Quit();
  56. #endif
  57. }
  58. }
  59. }