Add XR sample assets and update settings

This commit is contained in:
2026-01-21 14:18:30 +01:00
parent da213b4475
commit 0ae28bf32d
1990 changed files with 506411 additions and 76 deletions

View File

@@ -0,0 +1,16 @@
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToAxisXRInput : ActionToControlXRInput<float>
{
[SerializeField]
Slider m_Slider;
void Update()
{
if (m_Slider != null && device.isValid && device.TryGetFeatureValue(usage, out var axisValue))
m_Slider.value = axisValue;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4e640c17930de1148826b2c4e579eec3

View File

@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToAxisXRInputHand : ActionToControlXRInputHand<float>
{
[SerializeField]
Slider m_Slider;
void Update()
{
if (m_Slider != null && device.isValid && device.TryGetFeatureValue(usage, out var axisValue))
m_Slider.value = axisValue;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 073f5e0a3f381764cbe5dd23b5451af0

View File

@@ -0,0 +1,28 @@
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToButtonXRInput : ActionToControlXRInput<bool>
{
[SerializeField]
Image m_Image;
[SerializeField]
Color m_NormalColor = Color.red;
[SerializeField]
Color m_PressedColor = Color.green;
void Awake()
{
if (m_Image != null)
m_Image.color = m_NormalColor;
}
void Update()
{
if (m_Image != null && device.isValid && device.TryGetFeatureValue(usage, out var isPressed))
m_Image.color = isPressed ? m_PressedColor : m_NormalColor;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ead821ee897c52149b04afd633204ab8

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToButtonXRInputHand : ActionToControlXRInputHand<bool>
{
[SerializeField]
Image m_Image;
[SerializeField]
Color m_NormalColor = Color.red;
[SerializeField]
Color m_PressedColor = Color.green;
void Awake()
{
if (m_Image != null)
m_Image.color = m_NormalColor;
}
void Update()
{
if (m_Image != null && device.isValid && device.TryGetFeatureValue(usage, out var isPressed))
m_Image.color = isPressed ? m_PressedColor : m_NormalColor;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b6f0f767d7a434d43896e37e8c3dbd3a

View File

@@ -0,0 +1,43 @@
using System.Collections;
using UnityEngine.UI;
using UnityEngine.XR.OpenXR.Input;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public abstract class ActionToControlXRInput<TUsageType> : MonoBehaviour
{
[SerializeField]
XRNode m_XRNode;
[SerializeField]
string m_UsageString;
[SerializeField]
[Tooltip("Optional text element that will be set to the name of the action")]
Text m_Text;
protected InputDevice device { get; private set; }
protected InputFeatureUsage<TUsageType> usage { get; private set; }
protected virtual void OnEnable()
{
usage = new InputFeatureUsage<TUsageType>(m_UsageString);
StartCoroutine(SearchForDevice());
}
IEnumerator SearchForDevice()
{
if (m_Text != null)
m_Text.text = m_UsageString;
while (isActiveAndEnabled)
{
device = InputDevices.GetDeviceAtXRNode(m_XRNode);
if (device.isValid)
break;
yield return new WaitForSeconds(1.0f);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6d2b96f7a468fda4f96199bb038bb6b5

View File

@@ -0,0 +1,63 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToControlXRInputHand<TUsageType> : MonoBehaviour
{
[SerializeField]
bool m_IsRightHand;
[SerializeField]
string m_HandDeviceName;
[SerializeField]
string m_UsageString;
[SerializeField]
[Tooltip("Optional text element that will be set to the name of the action")]
Text m_Text = null;
protected InputDevice device { get; private set; }
protected InputFeatureUsage<TUsageType> usage { get; private set; }
List<InputDevice> m_DevicesReuse;
void OnEnable()
{
usage = new InputFeatureUsage<TUsageType>(m_UsageString);
m_DevicesReuse = new List<InputDevice>();
StartCoroutine(SearchForDevice());
}
IEnumerator SearchForDevice()
{
if (m_Text != null)
m_Text.text = m_UsageString;
while (isActiveAndEnabled)
{
InputDevices.GetDevicesWithCharacteristics(
m_IsRightHand
? InputDeviceCharacteristics.Right
: InputDeviceCharacteristics.Left,
m_DevicesReuse);
for (int deviceIndex = 0; deviceIndex < m_DevicesReuse.Count; ++deviceIndex)
{
if (m_DevicesReuse[deviceIndex].name != m_HandDeviceName)
continue;
device = m_DevicesReuse[deviceIndex];
break;
}
if (device.isValid)
break;
yield return new WaitForSeconds(1.0f);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 087fc5e342f6a5a4793c6c4d741743aa

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
using UnityEngine.XR;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToDeviceInfoXRInput : MonoBehaviour
{
[SerializeField]
XRNode m_XRNode;
[SerializeField]
Text m_Text = null;
InputDevice m_Device;
List<InputFeatureUsage> m_UsagesReuse;
void OnEnable() => m_UsagesReuse = new List<InputFeatureUsage>();
void Update()
{
if (!m_Device.isValid)
{
m_Device = InputDevices.GetDeviceAtXRNode(m_XRNode);
if (!m_Device.isValid)
return;
}
m_Device.TryGetFeatureUsages(m_UsagesReuse);
m_Text.text = $"{m_Device.name}\n{string.Join(",", m_UsagesReuse.Select(u => u.name))}";
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2a4fe26eb8a9c3941855bf9b20afc372

View File

@@ -0,0 +1,53 @@
using UnityEngine.XR.OpenXR.Input;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToHapticsXRInput : ActionToControlXRInput<bool>
{
public float amplitude
{
get => m_Amplitude;
set => m_Amplitude = value;
}
public float duration
{
get => m_Duration;
set => m_Duration = value;
}
public float frequency
{
get => m_Frequency;
set => m_Frequency = value;
}
[SerializeField]
float m_Amplitude = 1.0f;
[SerializeField]
float m_Duration = 0.1f;
[SerializeField]
float m_Frequency = 0.0f;
bool m_WasPressedLastFrame;
void Update()
{
if (!device.isValid)
return;
if (!device.TryGetFeatureValue(usage, out var isPressed))
{
m_WasPressedLastFrame = false;
return;
}
if (isPressed && !m_WasPressedLastFrame)
OpenXRInput.SendHapticImpulse(device, m_Amplitude, m_Duration, m_Frequency);
m_WasPressedLastFrame = isPressed;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0dfcd11b560b4f74bab8688f3ebbea28

View File

@@ -0,0 +1,48 @@
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToSliderXRInput : ActionToControlXRInput<float>
{
[SerializeField]
Slider m_Slider;
Graphic m_Graphic;
Graphic[] m_Graphics;
void Start()
{
if (m_Slider == null)
Debug.LogWarning("ActionToSliderXRInput MonoBehaviour started without any associated slider. This input will not be reported.", this);
m_Graphic = gameObject.GetComponent<Graphic>();
m_Graphics = gameObject.GetComponentsInChildren<Graphic>();
}
void Update()
{
if (m_Slider != null && device.isValid && device.TryGetFeatureValue(usage, out var axisValue))
{
SetVisible(true);
m_Slider.value = axisValue;
}
else
{
SetVisible(false);
m_Slider.value = 0f;
}
}
void SetVisible(bool visible)
{
if (m_Graphic != null)
m_Graphic.enabled = visible;
if (m_Graphics != null)
{
for (int graphicIndex = 0; graphicIndex < m_Graphics.Length; ++graphicIndex)
m_Graphics[graphicIndex].enabled = visible;
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3dde9c14ae9dc1a469da2028638f26da

View File

@@ -0,0 +1,62 @@
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToVector2SliderXRInput : ActionToControlXRInput<Vector2>
{
public Slider xAxisSlider => m_XAxisSlider;
public Slider yAxisSlider => m_YAxisSlider;
[SerializeField]
Slider m_XAxisSlider = null;
[SerializeField]
Slider m_YAxisSlider = null;
Graphic m_Graphic;
Graphic[] m_Graphics;
void Start()
{
if (m_XAxisSlider == null)
Debug.LogWarning("ActionToSlider Monobehaviour started without any associated X-axis slider. This input won't be reported.", this);
if (m_YAxisSlider == null)
Debug.LogWarning("ActionToSlider Monobehaviour started without any associated Y-axis slider. This input won't be reported.", this);
m_Graphic = gameObject.GetComponent<Graphic>();
m_Graphics = gameObject.GetComponentsInChildren<Graphic>();
}
void Update()
{
if (m_XAxisSlider != null && m_YAxisSlider != null && device.isValid)
{
bool retrieved = device.TryGetFeatureValue(usage, out var vec);
SetVisible(retrieved);
if (!retrieved)
vec = Vector2.zero;
xAxisSlider.value = vec.x;
yAxisSlider.value = vec.y;
}
else
{
SetVisible(false);
}
}
void SetVisible(bool visible)
{
if (m_Graphic != null)
m_Graphic.enabled = visible;
if (m_Graphics != null)
{
for (int graphicIndex = 0; graphicIndex < m_Graphics.Length; ++graphicIndex)
m_Graphics[graphicIndex].enabled = visible;
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 18c96d791af05894187e1c5c9fd77c4a

View File

@@ -0,0 +1,16 @@
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToVector2XRInput : ActionToControlXRInput<Vector2>
{
[SerializeField]
RectTransform m_Handle;
void Update()
{
if (m_Handle != null && device.isValid && device.TryGetFeatureValue(usage, out var vec))
m_Handle.anchorMin = m_Handle.anchorMax = (vec + Vector2.one) * 0.5f;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ebd3aeec634b2634ba76bcad8633fb2e

View File

@@ -0,0 +1,54 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine.XR.OpenXR.Input;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToVisibilityXRInput : MonoBehaviour
{
[SerializeField]
XRNode m_XRNode;
[SerializeField]
GameObject m_Target;
[SerializeField]
string m_UsageString;
List<InputFeatureUsage> m_UsagesReuse;
void OnEnable()
{
m_UsagesReuse = new List<InputFeatureUsage>();
if (m_Target == null)
m_Target = gameObject;
m_Target.SetActive(false);
StartCoroutine(UpdateVisibility());
}
IEnumerator UpdateVisibility()
{
while (isActiveAndEnabled)
{
var device = InputDevices.GetDeviceAtXRNode(m_XRNode);
if (device.isValid && device.TryGetFeatureUsages(m_UsagesReuse) && m_UsagesReuse.Count > 0)
{
for (int usageIndex = 0; usageIndex < m_UsagesReuse.Count; ++usageIndex)
{
if (m_UsagesReuse[usageIndex].name == m_UsageString)
{
m_Target.SetActive(true);
break;
}
}
break;
}
yield return new WaitForSeconds(1.0f);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 83bb0a5009c7d6a41b30d907f9e30183

View File

@@ -0,0 +1,74 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine.XR.OpenXR.Input;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class ActionToVisibilityXRInputHand : MonoBehaviour
{
[SerializeField]
bool m_IsRightHand;
[SerializeField]
GameObject m_Target;
[SerializeField]
string m_HandDeviceName;
[SerializeField]
string m_UsageString;
List<InputDevice> m_DevicesReuse;
List<InputFeatureUsage> m_UsagesReuse;
void OnEnable()
{
m_DevicesReuse = new List<InputDevice>();
m_UsagesReuse = new List<InputFeatureUsage>();
if (m_Target == null)
m_Target = gameObject;
m_Target.SetActive(false);
StartCoroutine(UpdateVisibility());
}
IEnumerator UpdateVisibility()
{
while (isActiveAndEnabled)
{
InputDevices.GetDevicesWithCharacteristics(
m_IsRightHand
? InputDeviceCharacteristics.Right
: InputDeviceCharacteristics.Left,
m_DevicesReuse);
var device = new InputDevice();
for (int deviceIndex = 0; deviceIndex < m_DevicesReuse.Count; ++deviceIndex)
{
if (m_DevicesReuse[deviceIndex].name != m_HandDeviceName)
continue;
device = m_DevicesReuse[deviceIndex];
break;
}
if (device.isValid && device.TryGetFeatureUsages(m_UsagesReuse) && m_UsagesReuse.Count > 0)
{
for (int usageIndex = 0; usageIndex < m_UsagesReuse.Count; ++usageIndex)
{
if (m_UsagesReuse[usageIndex].name == m_UsageString)
{
m_Target.SetActive(true);
break;
}
}
break;
}
yield return new WaitForSeconds(1.0f);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2fb19af5cfb8b954f9ee92b5845f1655

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class AutomaticTrackingModeChangerXRInput : MonoBehaviour
{
[SerializeField]
float m_ChangeInterval = 5.0f;
private float m_TimeRemainingTillChange;
static List<XRInputSubsystem> s_InputSubsystems = new List<XRInputSubsystem>();
static List<TrackingOriginModeFlags> s_SupportedTrackingOriginModes = new List<TrackingOriginModeFlags>();
void OnEnable()
{
m_TimeRemainingTillChange = m_ChangeInterval;
}
void Update()
{
m_TimeRemainingTillChange -= Time.deltaTime;
if (m_TimeRemainingTillChange <= 0.0f)
{
List<XRInputSubsystem> inputSubsystems = new List<XRInputSubsystem>();
SubsystemManager.GetSubsystems(inputSubsystems);
XRInputSubsystem subsystem = inputSubsystems?[0];
if (subsystem != null)
{
UpdateSupportedTrackingOriginModes(subsystem);
SetToNextMode(subsystem);
}
m_TimeRemainingTillChange += m_ChangeInterval;
}
}
void UpdateSupportedTrackingOriginModes(XRInputSubsystem subsystem)
{
TrackingOriginModeFlags supportedOriginModes = subsystem.GetSupportedTrackingOriginModes();
s_SupportedTrackingOriginModes.Clear();
for (int i = 0; i < 31; i++)
{
uint modeToCheck = 1u << i;
if ((modeToCheck & ((UInt32)supportedOriginModes)) != 0)
{
s_SupportedTrackingOriginModes.Add((TrackingOriginModeFlags)modeToCheck);
}
}
}
void SetToNextMode(XRInputSubsystem subsystem)
{
TrackingOriginModeFlags currentOriginMode = subsystem.GetTrackingOriginMode();
for (int i = 0; i < s_SupportedTrackingOriginModes.Count; i++)
{
if (currentOriginMode == s_SupportedTrackingOriginModes[i])
{
int nextModeIndex = (i + 1) % s_SupportedTrackingOriginModes.Count;
subsystem.TrySetTrackingOriginMode(s_SupportedTrackingOriginModes[nextModeIndex]);
break;
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6e6c265ca1781794c9749b99898b3ad3

View File

@@ -0,0 +1,73 @@
using System.Collections.Generic;
using UnityEngine.UI;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class DisplayDeviceInfoFromActionXRInput : MonoBehaviour
{
[SerializeField]
XRNode m_XRNode;
[SerializeField]
GameObject m_RootObject = null;
[SerializeField]
Text m_TargetText;
InputDevice m_Device;
List<InputFeatureUsage> m_UsagesReuse;
void OnEnable()
{
if (m_UsagesReuse == null)
m_UsagesReuse = new List<InputFeatureUsage>();
if (m_TargetText == null)
Debug.LogWarning("DisplayDeviceInfo Monobehaviour has no Target Text set. No information will be displayed.");
}
void Update()
{
if (!m_Device.isValid)
{
m_Device = InputDevices.GetDeviceAtXRNode(m_XRNode);
if (!m_Device.isValid)
{
if (m_RootObject != null)
m_RootObject.SetActive(false);
if (m_TargetText != null)
m_TargetText.text = "<No Device Connected>";
return;
}
}
if (m_RootObject != null)
m_RootObject.SetActive(true);
if (m_TargetText == null)
return;
m_TargetText.text = $"{m_Device.name}\n";
bool useComma = false;
m_Device.TryGetFeatureUsages(m_UsagesReuse);
foreach (var usage in m_UsagesReuse)
{
if (!useComma)
{
useComma = true;
m_TargetText.text += $"{usage}";
}
else
{
m_TargetText.text += $"{usage},";
}
}
if (m_TargetText.text.Length > 30)
m_TargetText.text = m_TargetText.text.Substring(0, 30);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 782f2d8c9ea0932448e57e84770efaf2

View File

@@ -0,0 +1,75 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.OpenXR.Input;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class MarkLateLatchNodeXRInput : MonoBehaviour
{
[SerializeField]
XRDisplaySubsystem.LateLatchNode m_LateLatchNode;
// Set one pose type usage for each hand to be late latched - e.g.: Aim, AimPosition, Grip, or GripPosition. For head node, leave it unset.
[SerializeField]
string m_UsageName;
InputDevice m_Device;
XRDisplaySubsystem m_DisplaySubsystem;
bool TryEnsureLateLatchCalled()
{
if (m_Device.isValid)
return true;
m_Device = InputDevices.GetDeviceAtXRNode(ConvertLateLatchNodeToXRNode(m_LateLatchNode));
if (!m_Device.isValid)
return false;
if (!string.IsNullOrEmpty(m_UsageName))
OpenXRInput.TrySetControllerLateLatchAction(m_Device, m_UsageName);
return true;
}
XRNode ConvertLateLatchNodeToXRNode(XRDisplaySubsystem.LateLatchNode node)
{
switch (node)
{
case XRDisplaySubsystem.LateLatchNode.Head:
return XRNode.Head;
case XRDisplaySubsystem.LateLatchNode.LeftHand:
return XRNode.LeftHand;
case XRDisplaySubsystem.LateLatchNode.RightHand:
return XRNode.RightHand;
default:
return XRNode.Head;
}
}
void Start()
{
List<XRDisplaySubsystem> subsys = new List<XRDisplaySubsystem>();
SubsystemManager.GetSubsystems<XRDisplaySubsystem>(subsys);
if (subsys.Count >= 1)
m_DisplaySubsystem = subsys[0];
}
void Update()
{
if (!TryEnsureLateLatchCalled() || m_DisplaySubsystem == null)
return;
transform.position += new Vector3(0.00001f, 0, 0);
Quaternion rot = transform.rotation;
rot.x += 0.00001f;
transform.rotation = rot;
m_DisplaySubsystem.MarkTransformLateLatched(transform, m_LateLatchNode);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e90535f5a20f6f845a750e19ce005b6e

View File

@@ -0,0 +1,63 @@
using UnityEngine;
using UnityEngine.Experimental.XR.Interaction;
using UnityEngine.SpatialTracking;
using UnityEngine.XR;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
[RequireComponent(typeof(TrackedPoseDriver))]
public class PoseProvider : BasePoseProvider
{
public override PoseDataFlags GetPoseFromProvider(out Pose output)
{
output = Pose.identity;
if (!m_Device.isValid)
{
m_Device = InputDevices.GetDeviceAtXRNode(m_XRNode);
if (!m_Device.isValid)
return PoseDataFlags.NoData;
}
var successFlags = PoseDataFlags.NoData;
if (m_Device.TryGetFeatureValue(m_PositionUsage, out var position))
{
output.position = position;
successFlags |= PoseDataFlags.Position;
}
if (m_Device.TryGetFeatureValue(m_RotationUsage, out var rotation))
{
output.rotation = rotation;
successFlags |= PoseDataFlags.Rotation;
}
return successFlags;
}
void Start()
{
m_LastPose = Pose.identity;
m_Driver = GetComponent<TrackedPoseDriver>();
m_PositionUsage = new InputFeatureUsage<Vector3>(m_PositionUsageString);
m_RotationUsage = new InputFeatureUsage<Quaternion>(m_RotationUsageString);
}
[SerializeField]
XRNode m_XRNode;
[SerializeField]
string m_PositionUsageString;
[SerializeField]
string m_RotationUsageString;
InputDevice m_Device;
InputFeatureUsage<Vector3> m_PositionUsage;
InputFeatureUsage<Quaternion> m_RotationUsage;
Pose m_LastPose;
TrackedPoseDriver m_Driver;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 34bcf8ef78760c74ca19937538d62e73

View File

@@ -0,0 +1,77 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.XR.Interaction;
using UnityEngine.SpatialTracking;
using UnityEngine.XR;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
[RequireComponent(typeof(TrackedPoseDriver))]
public class PoseProvider_NameDevice : BasePoseProvider
{
public override PoseDataFlags GetPoseFromProvider(out Pose output)
{
output = Pose.identity;
if (!m_Device.isValid)
{
InputDevices.GetDevices(m_DevicesReuse);
for (int deviceIndex = 0; deviceIndex < m_DevicesReuse.Count; ++deviceIndex)
{
var device = m_DevicesReuse[deviceIndex];
if (device.name == m_DeviceName)
{
m_Device = device;
break;
}
}
if (!m_Device.isValid)
return PoseDataFlags.NoData;
}
var successFlags = PoseDataFlags.NoData;
if (m_Device.TryGetFeatureValue(m_PositionUsage, out var position))
{
output.position = position;
successFlags |= PoseDataFlags.Position;
}
if (m_Device.TryGetFeatureValue(m_RotationUsage, out var rotation))
{
output.rotation = rotation;
successFlags |= PoseDataFlags.Rotation;
}
return successFlags;
}
void Start()
{
m_LastPose = Pose.identity;
m_Driver = GetComponent<TrackedPoseDriver>();
m_PositionUsage = new InputFeatureUsage<Vector3>(m_PositionUsageString);
m_RotationUsage = new InputFeatureUsage<Quaternion>(m_RotationUsageString);
m_DevicesReuse = new List<InputDevice>();
}
[SerializeField]
string m_DeviceName;
[SerializeField]
string m_PositionUsageString;
[SerializeField]
string m_RotationUsageString;
InputDevice m_Device;
InputFeatureUsage<Vector3> m_PositionUsage;
InputFeatureUsage<Quaternion> m_RotationUsage;
Pose m_LastPose;
TrackedPoseDriver m_Driver;
List<InputDevice> m_DevicesReuse;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d69ebe92d49928c44848ed51b0c8a8b8

View File

@@ -0,0 +1,98 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR;
namespace UnityEngine.XR.OpenXR.Samples.ControllerSampleXRInput
{
public class TrackingModeOriginXRInput : MonoBehaviour
{
[SerializeField]
Image m_RecenteredImage;
[SerializeField]
Color m_RecenteredOffColor = Color.red;
[SerializeField]
Color m_RecenteredColor = Color.green;
[SerializeField]
float m_RecenteredColorResetTime = 1.0f;
float m_LastRecenteredTime;
[SerializeField]
TrackingOriginModeFlags m_CurrentTrackingOriginMode;
public TrackingOriginModeFlags currentTrackingOriginMode { get { return m_CurrentTrackingOriginMode; } }
[SerializeField]
Text m_CurrentTrackingOriginModeDisplay;
[SerializeField]
TrackingOriginModeFlags m_DesiredTrackingOriginMode;
public TrackingOriginModeFlags desiredTrackingOriginMode { get { return m_DesiredTrackingOriginMode; } set { m_DesiredTrackingOriginMode = value; } }
[SerializeField]
TrackingOriginModeFlags m_SupportedTrackingOriginModes;
public TrackingOriginModeFlags supportedTrackingOriginModes { get { return m_SupportedTrackingOriginModes; } }
static List<XRInputSubsystem> s_InputSubsystems = new List<XRInputSubsystem>();
private void OnEnable()
{
SubsystemManager.GetSubsystems(s_InputSubsystems);
for (int i = 0; i < s_InputSubsystems.Count; i++)
{
s_InputSubsystems[i].trackingOriginUpdated += TrackingOriginUpdated;
}
}
private void OnDisable()
{
SubsystemManager.GetSubsystems(s_InputSubsystems);
for (int i = 0; i < s_InputSubsystems.Count; i++)
{
s_InputSubsystems[i].trackingOriginUpdated -= TrackingOriginUpdated;
}
}
public void OnDesiredSelectionChanged(int newValue)
{
desiredTrackingOriginMode = (TrackingOriginModeFlags)(newValue == 0 ? 0 : (1 << (newValue - 1)));
}
private void TrackingOriginUpdated(XRInputSubsystem obj)
{
m_LastRecenteredTime = Time.time;
}
void Update()
{
XRInputSubsystem subsystem = null;
SubsystemManager.GetSubsystems(s_InputSubsystems);
if (s_InputSubsystems.Count > 0)
{
subsystem = s_InputSubsystems[0];
}
m_SupportedTrackingOriginModes = subsystem?.GetSupportedTrackingOriginModes() ?? TrackingOriginModeFlags.Unknown;
if (m_CurrentTrackingOriginMode != m_DesiredTrackingOriginMode & m_DesiredTrackingOriginMode != TrackingOriginModeFlags.Unknown)
{
subsystem?.TrySetTrackingOriginMode(m_DesiredTrackingOriginMode);
}
m_CurrentTrackingOriginMode = subsystem?.GetTrackingOriginMode() ?? TrackingOriginModeFlags.Unknown;
if (m_CurrentTrackingOriginModeDisplay != null)
m_CurrentTrackingOriginModeDisplay.text = m_CurrentTrackingOriginMode.ToString();
if (m_RecenteredImage != null)
{
float lerp = (Time.time - m_LastRecenteredTime) / m_RecenteredColorResetTime;
lerp = Mathf.Clamp(lerp, 0.0f, 1.0f);
m_RecenteredImage.color = Color.Lerp(m_RecenteredColor, m_RecenteredOffColor, lerp);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8680514b207a89d4c91a7b5a6b89b462