SteamVR_Ears.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Handles aligning audio listener when using speakers.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using Valve.VR;
  8. [RequireComponent(typeof(AudioListener))]
  9. public class SteamVR_Ears : MonoBehaviour
  10. {
  11. public SteamVR_Camera vrcam;
  12. bool usingSpeakers;
  13. Quaternion offset;
  14. private void OnNewPosesApplied()
  15. {
  16. var origin = vrcam.origin;
  17. var baseRotation = origin != null ? origin.rotation : Quaternion.identity;
  18. transform.rotation = baseRotation * offset;
  19. }
  20. void OnEnable()
  21. {
  22. usingSpeakers = false;
  23. var settings = OpenVR.Settings;
  24. if (settings != null)
  25. {
  26. var error = EVRSettingsError.None;
  27. if (settings.GetBool(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_UsingSpeakers_Bool, ref error))
  28. {
  29. usingSpeakers = true;
  30. var yawOffset = settings.GetFloat(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_SpeakersForwardYawOffsetDegrees_Float, ref error);
  31. offset = Quaternion.Euler(0.0f, yawOffset, 0.0f);
  32. }
  33. }
  34. if (usingSpeakers)
  35. SteamVR_Events.NewPosesApplied.Listen(OnNewPosesApplied);
  36. }
  37. void OnDisable()
  38. {
  39. if (usingSpeakers)
  40. SteamVR_Events.NewPosesApplied.Remove(OnNewPosesApplied);
  41. }
  42. }