SteamVR_Ears.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. namespace Valve.VR
  9. {
  10. [RequireComponent(typeof(AudioListener))]
  11. public class SteamVR_Ears : MonoBehaviour
  12. {
  13. public SteamVR_Camera vrcam;
  14. bool usingSpeakers;
  15. Quaternion offset;
  16. private void OnNewPosesApplied()
  17. {
  18. var origin = vrcam.origin;
  19. var baseRotation = origin != null ? origin.rotation : Quaternion.identity;
  20. transform.rotation = baseRotation * offset;
  21. }
  22. void OnEnable()
  23. {
  24. usingSpeakers = false;
  25. var settings = OpenVR.Settings;
  26. if (settings != null)
  27. {
  28. var error = EVRSettingsError.None;
  29. if (settings.GetBool(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_UsingSpeakers_Bool, ref error))
  30. {
  31. usingSpeakers = true;
  32. var yawOffset = settings.GetFloat(OpenVR.k_pch_SteamVR_Section, OpenVR.k_pch_SteamVR_SpeakersForwardYawOffsetDegrees_Float, ref error);
  33. offset = Quaternion.Euler(0.0f, yawOffset, 0.0f);
  34. }
  35. }
  36. if (usingSpeakers)
  37. SteamVR_Events.NewPosesApplied.Listen(OnNewPosesApplied);
  38. }
  39. void OnDisable()
  40. {
  41. if (usingSpeakers)
  42. SteamVR_Events.NewPosesApplied.Remove(OnNewPosesApplied);
  43. }
  44. }
  45. }