/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ using System.Collections.Generic; using UnityEngine; namespace NRKernal.NRExamples { /// A tracking infomation tips. [HelpURL("https://developer.nreal.ai/develop/discover/introduction-nrsdk")] public class TrackingInfomationTips : SingletonBehaviour { /// Dictionary of tips. private Dictionary m_TipsDict = new Dictionary(); /// Values that represent tip types. public enum TipType { /// An enum constant representing the un initialized option. UnInitialized, /// An enum constant representing the lost tracking option. LostTracking, /// An enum constant representing the none option. None } /// The tips layer. [Tooltip("Camera would only show the layer of TipsLayer when lost tracking.\n" + "Select the layer which you want to show when lost tracking.")] [SerializeField] private LayerMask m_TipsLayer; /// The current tip. private GameObject m_CurrentTip; /// The center camera. private Camera centerCamera; /// The origin layer l camera. private int originLayerLCam; /// The origin layer camera. private int originLayerRCam; /// Starts this object. void Start() { originLayerLCam = NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.cullingMask; originLayerRCam = NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.cullingMask; centerCamera = NRSessionManager.Instance.NRHMDPoseTracker.centerCamera; ShowTips(TipType.UnInitialized); } /// Executes the 'enable' action. private void OnEnable() { NRHMDPoseTracker.OnHMDLostTracking += OnHMDLostTracking; NRHMDPoseTracker.OnHMDPoseReady += OnHMDPoseReady; } /// Executes the 'disable' action. private void OnDisable() { NRHMDPoseTracker.OnHMDLostTracking -= OnHMDLostTracking; NRHMDPoseTracker.OnHMDPoseReady -= OnHMDPoseReady; } /// Executes the 'hmd pose ready' action. private void OnHMDPoseReady() { NRDebugger.Info("[NRHMDPoseTracker] OnHMDPoseReady"); ShowTips(TipType.None); } /// Executes the 'hmd lost tracking' action. private void OnHMDLostTracking() { NRDebugger.Info("[NRHMDPoseTracker] OnHMDLostTracking:" + NRFrame.LostTrackingReason); ShowTips(TipType.LostTracking); } /// Shows the tips. /// The type. public void ShowTips(TipType type) { switch (type) { case TipType.UnInitialized: case TipType.LostTracking: GameObject go; m_TipsDict.TryGetValue(type, out go); int layer = LayerMaskToLayer(m_TipsLayer); if (go == null) { go = Instantiate(Resources.Load(type.ToString() + "Tip"), centerCamera.transform) as GameObject; m_TipsDict.Add(type, go); go.layer = layer; foreach (Transform child in go.transform) { child.gameObject.layer = layer; } } if (go != m_CurrentTip) { m_CurrentTip?.SetActive(false); go.SetActive(true); m_CurrentTip = go; } NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.cullingMask = 1 << layer; NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.cullingMask = 1 << layer; break; case TipType.None: if (m_CurrentTip != null) { m_CurrentTip.SetActive(false); } m_CurrentTip = null; NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.cullingMask = originLayerLCam; NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.cullingMask = originLayerRCam; break; default: break; } } /// Layer mask to layer. /// . /// The last layer of the layer mask. public static int LayerMaskToLayer(LayerMask layerMask) { int layerNumber = 0; int layer = layerMask.value; while (layer > 0) { layer = layer >> 1; layerNumber++; } return layerNumber - 1; } /// /// Base OnDestroy method that destroys the Singleton's unique instance. Called by Unity when /// destroying a MonoBehaviour. Scripts that extend Singleton should be sure to call /// base.OnDestroy() to ensure the underlying static Instance reference is properly cleaned up. new void OnDestroy() { if (isDirty) return; if (m_TipsDict != null) { foreach (var item in m_TipsDict) { if (item.Value != null) { GameObject.Destroy(item.Value); } } } } } }