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

85 行
2.7 KiB

using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class HumanBodyTracker : MonoBehaviour
{
[SerializeField]
[Tooltip("The Skeleton prefab to be controlled.")]
GameObject m_SkeletonPrefab;
[SerializeField]
[Tooltip("The ARHumanBodyManager which will produce body tracking events.")]
ARHumanBodyManager m_HumanBodyManager;
/// <summary>
/// Get/Set the <c>ARHumanBodyManager</c>.
/// </summary>
public ARHumanBodyManager humanBodyManager
{
get { return m_HumanBodyManager; }
set { m_HumanBodyManager = value; }
}
/// <summary>
/// Get/Set the skeleton prefab.
/// </summary>
public GameObject skeletonPrefab
{
get { return m_SkeletonPrefab; }
set { m_SkeletonPrefab = value; }
}
Dictionary<TrackableId, BoneController> m_SkeletonTracker = new Dictionary<TrackableId, BoneController>();
void OnEnable()
{
Debug.Assert(m_HumanBodyManager != null, "Human body manager is required.");
m_HumanBodyManager.humanBodiesChanged += OnHumanBodiesChanged;
}
void OnDisable()
{
if (m_HumanBodyManager != null)
m_HumanBodyManager.humanBodiesChanged -= OnHumanBodiesChanged;
}
void OnHumanBodiesChanged(ARHumanBodiesChangedEventArgs eventArgs)
{
BoneController boneController;
foreach (var humanBody in eventArgs.added)
{
if (!m_SkeletonTracker.TryGetValue(humanBody.trackableId, out boneController))
{
Debug.Log($"Adding a new skeleton [{humanBody.trackableId}].");
var newSkeletonGO = Instantiate(m_SkeletonPrefab, humanBody.transform);
boneController = newSkeletonGO.GetComponent<BoneController>();
m_SkeletonTracker.Add(humanBody.trackableId, boneController);
}
boneController.InitializeSkeletonJoints();
boneController.ApplyBodyPose(humanBody);
}
foreach (var humanBody in eventArgs.updated)
{
if (m_SkeletonTracker.TryGetValue(humanBody.trackableId, out boneController))
{
boneController.ApplyBodyPose(humanBody);
}
}
foreach (var humanBody in eventArgs.removed)
{
Debug.Log($"Removing a skeleton [{humanBody.trackableId}].");
if (m_SkeletonTracker.TryGetValue(humanBody.trackableId, out boneController))
{
Destroy(boneController.gameObject);
m_SkeletonTracker.Remove(humanBody.trackableId);
}
}
}
}