79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.UI;
|
|
|
|
public class MyARPoseDriver : MonoBehaviour
|
|
{
|
|
|
|
public InputDevice? currentDevice;
|
|
private float savedHeading = 0f;
|
|
private bool headingSet = false;
|
|
|
|
public Text text;
|
|
|
|
private void OnEnable()
|
|
{
|
|
// 在启用时注册设备连接事件
|
|
InputDevices.deviceConnected += OnDeviceConnected;
|
|
text.text += "注冊 ";
|
|
// 查找已连接的设备
|
|
var devices = new List<InputDevice>();
|
|
text.text += "连接"+devices;
|
|
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.TrackedDevice, devices);
|
|
foreach (var device in devices)
|
|
{
|
|
text.text += " 进入循环";
|
|
TrySetupDevice(device);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
InputDevices.deviceConnected -= OnDeviceConnected;
|
|
}
|
|
|
|
private void OnDeviceConnected(InputDevice device)
|
|
{
|
|
TrySetupDevice(device);
|
|
}
|
|
|
|
private void TrySetupDevice(InputDevice device)
|
|
{
|
|
// 检查设备是否支持位置和旋转追踪
|
|
if (device.characteristics.HasFlag(InputDeviceCharacteristics.TrackedDevice))
|
|
{
|
|
text.text +="检查设备";
|
|
if (!currentDevice.HasValue)
|
|
{
|
|
currentDevice = device;
|
|
Debug.Log($"AR追踪设备已连接: {device.name}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
text.text += "AR追踪设备未连接";
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!currentDevice.HasValue)
|
|
return;
|
|
Quaternion rotation = Quaternion.identity;
|
|
|
|
bool hasRotation = false;
|
|
|
|
|
|
|
|
if (!(hasRotation = currentDevice.Value.TryGetFeatureValue(CommonUsages.centerEyeRotation, out rotation)))
|
|
{
|
|
hasRotation = currentDevice.Value.TryGetFeatureValue(CommonUsages.colorCameraRotation, out rotation);
|
|
}
|
|
|
|
if (hasRotation)
|
|
{
|
|
transform.localRotation = rotation;
|
|
}
|
|
}
|
|
} |