NRAppManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. namespace NRKernal
  11. {
  12. /// <summary> Manager for applications. </summary>
  13. [DisallowMultipleComponent]
  14. [HelpURL("https://developer.nreal.ai/develop/discover/introduction-nrsdk")]
  15. public class NRAppManager : MonoBehaviour
  16. {
  17. /// <summary>
  18. /// If enable this, quick click app button for three times, a profiler bar would show. </summary>
  19. public bool enableTriggerProfiler;
  20. /// <summary> The last click time. </summary>
  21. private float m_LastClickTime = 0f;
  22. /// <summary> The cumulative click number. </summary>
  23. private int m_CumulativeClickNum = 0;
  24. /// <summary> True if is profiler opened, false if not. </summary>
  25. private bool m_IsProfilerOpened = false;
  26. /// <summary> System gesture duration timer. </summary>
  27. private float m_SystemGestureTimer;
  28. /// <summary> Number of trigger profiler clicks. </summary>
  29. private const int TRIGGER_PROFILER_CLICK_COUNT = 3;
  30. /// <summary> Duration of system gesture to trigger function. </summary>
  31. private const float SYSTEM_GESTURE_KEEP_DURATION = 1.2f;
  32. private static AndroidJavaObject currentActivity;
  33. public static AndroidJavaObject CurrentActivity
  34. {
  35. get
  36. {
  37. if (currentActivity == null)
  38. {
  39. currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
  40. }
  41. return currentActivity;
  42. }
  43. }
  44. /// <summary> Executes the 'enable' action. </summary>
  45. private void OnEnable()
  46. {
  47. NRInput.AddClickListener(ControllerHandEnum.Right, ControllerButton.HOME, OnHomeButtonClick);
  48. NRInput.AddClickListener(ControllerHandEnum.Left, ControllerButton.HOME, OnHomeButtonClick);
  49. NRInput.AddClickListener(ControllerHandEnum.Right, ControllerButton.APP, OnAppButtonClick);
  50. NRInput.AddClickListener(ControllerHandEnum.Left, ControllerButton.APP, OnAppButtonClick);
  51. }
  52. /// <summary> Executes the 'disable' action. </summary>
  53. private void OnDisable()
  54. {
  55. NRInput.RemoveClickListener(ControllerHandEnum.Right, ControllerButton.HOME, OnHomeButtonClick);
  56. NRInput.RemoveClickListener(ControllerHandEnum.Left, ControllerButton.HOME, OnHomeButtonClick);
  57. NRInput.RemoveClickListener(ControllerHandEnum.Right, ControllerButton.APP, OnAppButtonClick);
  58. NRInput.RemoveClickListener(ControllerHandEnum.Left, ControllerButton.APP, OnAppButtonClick);
  59. }
  60. /// <summary> Updates this object. </summary>
  61. private void Update()
  62. {
  63. #if UNITY_EDITOR
  64. if (Input.GetKeyDown(KeyCode.Escape))
  65. {
  66. QuitApplication();
  67. }
  68. #endif
  69. CheckSystemGesture();
  70. }
  71. /// <summary> Executes the 'home button click' action. </summary>
  72. private void OnHomeButtonClick()
  73. {
  74. NRHomeMenu.Toggle();
  75. }
  76. /// <summary> Executes the 'application button click' action. </summary>
  77. private void OnAppButtonClick()
  78. {
  79. if (enableTriggerProfiler)
  80. {
  81. CollectClickEvent();
  82. }
  83. }
  84. /// <summary> Collect click event. </summary>
  85. private void CollectClickEvent()
  86. {
  87. if (Time.unscaledTime - m_LastClickTime < 0.2f)
  88. {
  89. m_CumulativeClickNum++;
  90. if (m_CumulativeClickNum == (TRIGGER_PROFILER_CLICK_COUNT - 1))
  91. {
  92. // Show the VisualProfiler
  93. NRVisualProfiler.Instance.Switch(!m_IsProfilerOpened);
  94. m_IsProfilerOpened = !m_IsProfilerOpened;
  95. m_CumulativeClickNum = 0;
  96. }
  97. }
  98. else
  99. {
  100. m_CumulativeClickNum = 0;
  101. }
  102. m_LastClickTime = Time.unscaledTime;
  103. }
  104. private void CheckSystemGesture()
  105. {
  106. if (NRInput.Hands.IsPerformingSystemGesture())
  107. {
  108. m_SystemGestureTimer += Time.deltaTime;
  109. if(m_SystemGestureTimer > SYSTEM_GESTURE_KEEP_DURATION)
  110. {
  111. m_SystemGestureTimer = float.MinValue;
  112. NRHomeMenu.Show();
  113. }
  114. }
  115. else
  116. {
  117. m_SystemGestureTimer = 0f;
  118. }
  119. }
  120. /// <summary> Quit application. </summary>
  121. public static void QuitApplication(bool backToMRSpace = false)
  122. {
  123. if (backToMRSpace)
  124. {
  125. NRDebugger.Info("QuitApplication backToMRSpace");
  126. if (Application.platform == RuntimePlatform.Android)
  127. {
  128. // 这个Action会启动Nebula Space
  129. string action = "ai.nreal.nebula.start.MRAPP";
  130. AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
  131. intent.Call<AndroidJavaObject>("setAction", action);
  132. intent.Call<AndroidJavaObject>("addFlags", 0x10000000);
  133. // 优先级最高
  134. intent.Call<AndroidJavaObject>("putExtra", "backToMRSpace", true);
  135. CurrentActivity.Call("startActivity", intent);
  136. }
  137. }
  138. NRDebugger.Info("QuitApplication QuitApp");
  139. NRDevice.QuitApp();
  140. }
  141. }
  142. }