您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

71 行
2.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
namespace UnityEngine.XR.ARFoundation.Samples
{
/// <summary>
/// This script enables face tracking with user facing camera and disables it otherwise.
/// It enables the world space object with world facing camera and disables it otherwise.
/// </summary>
[RequireComponent(typeof(ARSessionOrigin))]
[RequireComponent(typeof(ARFaceManager))]
public class FacingDirectionManager : MonoBehaviour
{
[SerializeField]
GameObject m_WorldSpaceObject;
public GameObject worldSpaceObject
{
get => m_WorldSpaceObject;
set => m_WorldSpaceObject = value;
}
CameraFacingDirection m_CurrentCameraFacingDirection;
ARCameraManager m_CameraManager;
void OnEnable()
{
m_CameraManager = GetComponentInChildren<ARCameraManager>();
m_CurrentCameraFacingDirection = m_CameraManager.currentFacingDirection;
}
void Update()
{
var updatedCameraFacingDirection = m_CameraManager.currentFacingDirection;
if (updatedCameraFacingDirection != CameraFacingDirection.None && updatedCameraFacingDirection != m_CurrentCameraFacingDirection)
{
if (updatedCameraFacingDirection == CameraFacingDirection.User)
{
m_CurrentCameraFacingDirection = updatedCameraFacingDirection;
GetComponent<ARFaceManager>().enabled = true;
worldSpaceObject.SetActive(false);
Application.onBeforeRender -= OnBeforeRender;
}
else if (updatedCameraFacingDirection == CameraFacingDirection.World)
{
m_CurrentCameraFacingDirection = updatedCameraFacingDirection;
GetComponent<ARFaceManager>().enabled = false;
worldSpaceObject.SetActive(true);
Application.onBeforeRender += OnBeforeRender;
}
}
}
void OnDisable()
{
GetComponent<ARFaceManager>().enabled = false;
Application.onBeforeRender -= OnBeforeRender;
}
void OnBeforeRender()
{
var camera = GetComponent<ARSessionOrigin>().camera;
if (camera && worldSpaceObject)
{
worldSpaceObject.transform.position = camera.transform.position + camera.transform.forward;
}
}
}
}