namespace VRTK.Prefabs.CameraRig.UnityXRCameraRig { using UnityEngine; using UnityEngine.XR; using Malimbe.PropertySerializationAttribute; using Malimbe.XmlDocumentationAttribute; using Malimbe.MemberChangeMethod; /// /// Provides configuration for the Unity Engine in XR. /// public class UnityXRConfigurator : MonoBehaviour { /// /// Represents the type of physical space available for XR. /// [Serialized] [field: DocumentedByXml] public TrackingSpaceType TrackingSpaceType { get; set; } = TrackingSpaceType.RoomScale; /// /// Automatically set the Unity Physics Fixed Timestep value based on the headset render frequency. /// [Serialized] [field: DocumentedByXml] public bool LockPhysicsUpdateRateToRenderFrequency { get; set; } = true; protected virtual void OnEnable() { UpdateTrackingSpaceType(); } protected virtual void Update() { UpdateFixedDeltaTime(); } /// /// Updates the tracking space type. /// protected virtual void UpdateTrackingSpaceType() { XRDevice.SetTrackingSpaceType(TrackingSpaceType); } /// /// Updates the fixed delta time to the appropriate value. /// protected virtual void UpdateFixedDeltaTime() { if (LockPhysicsUpdateRateToRenderFrequency && Time.timeScale > 0.0f && !string.IsNullOrEmpty(XRSettings.loadedDeviceName)) { Time.fixedDeltaTime = Time.timeScale / XRDevice.refreshRate; } } /// /// Called after has been changed. /// [CalledAfterChangeOf(nameof(TrackingSpaceType))] protected virtual void OnAfterTrackingSpaceTypeChange() { UpdateTrackingSpaceType(); } /// /// Called after has been changed. /// [CalledAfterChangeOf(nameof(LockPhysicsUpdateRateToRenderFrequency))] protected virtual void OnAfterLockPhysicsUpdateRateToRenderFrequencyChange() { UpdateFixedDeltaTime(); } } }