AR Foundation演示项目,使用 AR Foundation 4.1.7 并围绕某些功能演示更高级功能。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

175 行
4.4 KiB

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARUXReasonsManager : MonoBehaviour
{
[SerializeField]
bool m_ShowNotTrackingReasons = true;
public bool showNotTrackingReasons
{
get => m_ShowNotTrackingReasons;
set => m_ShowNotTrackingReasons = value;
}
[SerializeField]
TMP_Text m_ReasonDisplayText;
public TMP_Text reasonDisplayText
{
get => m_ReasonDisplayText;
set => m_ReasonDisplayText = value;
}
[SerializeField]
GameObject m_ReasonParent;
[SerializeField]
Image m_ReasonIcon;
[SerializeField]
Sprite m_InitRelocalSprite;
public Sprite initRelocalSprite
{
get => m_InitRelocalSprite;
set => m_InitRelocalSprite = value;
}
[SerializeField]
Sprite m_MotionSprite;
public Sprite motionSprite
{
get => m_MotionSprite;
set => m_MotionSprite = value;
}
[SerializeField]
Sprite m_LightSprite;
public Sprite lightSprite
{
get => m_LightSprite;
set => m_LightSprite = value;
}
[SerializeField]
Sprite m_FeaturesSprite;
public Sprite featuresSprite
{
get => m_FeaturesSprite;
set => m_FeaturesSprite = value;
}
[SerializeField]
Sprite m_UnsupportedSprite;
public Sprite unsupportedSprite
{
get => m_UnsupportedSprite;
set => m_UnsupportedSprite = value;
}
[SerializeField]
Sprite m_NoneSprite;
public Sprite noneSprite
{
get => m_NoneSprite;
set => m_NoneSprite = value;
}
NotTrackingReason m_CurrentReason;
bool m_SessionTracking;
const string k_InitRelocalText = "Initializing augmented reality";
const string k_MotionText = "Try moving at a slower pace";
const string k_LightText = "It’s too dark. Try going to a more well lit area.";
const string k_FeaturesText = "Look for more textures or details in the area.";
const string k_UnsupportedText = "AR content is not supported.";
const string k_NoneText = "Wait for tracking to begin.";
void OnEnable()
{
ARSession.stateChanged += ARSessionOnstateChanged;
if (!m_ShowNotTrackingReasons)
{
m_ReasonParent.SetActive(false);
}
}
void OnDisable()
{
ARSession.stateChanged -= ARSessionOnstateChanged;
}
void Update()
{
if (m_ShowNotTrackingReasons)
{
if (!m_SessionTracking)
{
m_CurrentReason = ARSession.notTrackingReason;
ShowReason();
}
else
{
if (m_ReasonDisplayText.gameObject.activeSelf)
{
m_ReasonParent.SetActive(false);
}
}
}
}
void ARSessionOnstateChanged(ARSessionStateChangedEventArgs obj)
{
m_SessionTracking = obj.state == ARSessionState.SessionTracking ? true : false;
}
void ShowReason()
{
m_ReasonParent.SetActive(true);
SetReason();
}
void SetReason()
{
switch (m_CurrentReason)
{
case NotTrackingReason.Initializing:
case NotTrackingReason.Relocalizing:
m_ReasonDisplayText.text = k_InitRelocalText;
m_ReasonIcon.sprite = m_InitRelocalSprite;
break;
case NotTrackingReason.ExcessiveMotion:
m_ReasonDisplayText.text = k_MotionText;
m_ReasonIcon.sprite = m_MotionSprite;
break;
case NotTrackingReason.InsufficientLight:
m_ReasonDisplayText.text = k_LightText;
m_ReasonIcon.sprite = m_LightSprite;
break;
case NotTrackingReason.InsufficientFeatures:
m_ReasonDisplayText.text = k_FeaturesText;
m_ReasonIcon.sprite = m_FeaturesSprite;
break;
case NotTrackingReason.Unsupported:
m_ReasonDisplayText.text = k_UnsupportedText;
m_ReasonIcon.sprite = m_UnsupportedSprite;
break;
case NotTrackingReason.None:
m_ReasonDisplayText.text = k_NoneText;
m_ReasonIcon.sprite = m_NoneSprite;
break;
}
}
}