/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ using UnityEngine; namespace NRKernal { /// Manager for applications. [DisallowMultipleComponent] [HelpURL("https://developer.nreal.ai/develop/discover/introduction-nrsdk")] public class NRAppManager : MonoBehaviour { /// /// If enable this, quick click app button for three times, a profiler bar would show. public bool enableTriggerProfiler; /// The last click time. private float m_LastClickTime = 0f; /// The cumulative click number. private int m_CumulativeClickNum = 0; /// True if is profiler opened, false if not. private bool m_IsProfilerOpened = false; /// System gesture duration timer. private float m_SystemGestureTimer; /// Number of trigger profiler clicks. private const int TRIGGER_PROFILER_CLICK_COUNT = 3; /// Duration of system gesture to trigger function. private const float SYSTEM_GESTURE_KEEP_DURATION = 1.2f; private static AndroidJavaObject currentActivity; public static AndroidJavaObject CurrentActivity { get { if (currentActivity == null) { currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"); } return currentActivity; } } /// Executes the 'enable' action. private void OnEnable() { NRInput.AddClickListener(ControllerHandEnum.Right, ControllerButton.HOME, OnHomeButtonClick); NRInput.AddClickListener(ControllerHandEnum.Left, ControllerButton.HOME, OnHomeButtonClick); NRInput.AddClickListener(ControllerHandEnum.Right, ControllerButton.APP, OnAppButtonClick); NRInput.AddClickListener(ControllerHandEnum.Left, ControllerButton.APP, OnAppButtonClick); } /// Executes the 'disable' action. private void OnDisable() { NRInput.RemoveClickListener(ControllerHandEnum.Right, ControllerButton.HOME, OnHomeButtonClick); NRInput.RemoveClickListener(ControllerHandEnum.Left, ControllerButton.HOME, OnHomeButtonClick); NRInput.RemoveClickListener(ControllerHandEnum.Right, ControllerButton.APP, OnAppButtonClick); NRInput.RemoveClickListener(ControllerHandEnum.Left, ControllerButton.APP, OnAppButtonClick); } /// Updates this object. private void Update() { #if UNITY_EDITOR if (Input.GetKeyDown(KeyCode.Escape)) { QuitApplication(); } #endif CheckSystemGesture(); } /// Executes the 'home button click' action. private void OnHomeButtonClick() { NRHomeMenu.Toggle(); } /// Executes the 'application button click' action. private void OnAppButtonClick() { if (enableTriggerProfiler) { CollectClickEvent(); } } /// Collect click event. private void CollectClickEvent() { if (Time.unscaledTime - m_LastClickTime < 0.2f) { m_CumulativeClickNum++; if (m_CumulativeClickNum == (TRIGGER_PROFILER_CLICK_COUNT - 1)) { // Show the VisualProfiler NRVisualProfiler.Instance.Switch(!m_IsProfilerOpened); m_IsProfilerOpened = !m_IsProfilerOpened; m_CumulativeClickNum = 0; } } else { m_CumulativeClickNum = 0; } m_LastClickTime = Time.unscaledTime; } private void CheckSystemGesture() { if (NRInput.Hands.IsPerformingSystemGesture()) { m_SystemGestureTimer += Time.deltaTime; if(m_SystemGestureTimer > SYSTEM_GESTURE_KEEP_DURATION) { m_SystemGestureTimer = float.MinValue; NRHomeMenu.Show(); } } else { m_SystemGestureTimer = 0f; } } /// Quit application. public static void QuitApplication(bool backToMRSpace = false) { if (backToMRSpace) { NRDebugger.Info("QuitApplication backToMRSpace"); if (Application.platform == RuntimePlatform.Android) { // 这个Action会启动Nebula Space string action = "ai.nreal.nebula.start.MRAPP"; AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent"); intent.Call("setAction", action); intent.Call("addFlags", 0x10000000); // 优先级最高 intent.Call("putExtra", "backToMRSpace", true); CurrentActivity.Call("startActivity", intent); } } NRDebugger.Info("QuitApplication QuitApp"); NRDevice.QuitApp(); } } }