ChaperoneInfo.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Stores the play area size info from the players chaperone data
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using System.Collections;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class ChaperoneInfo : MonoBehaviour
  13. {
  14. public bool initialized { get; private set; }
  15. public float playAreaSizeX { get; private set; }
  16. public float playAreaSizeZ { get; private set; }
  17. public bool roomscale { get; private set; }
  18. public static SteamVR_Events.Event Initialized = new SteamVR_Events.Event();
  19. public static SteamVR_Events.Action InitializedAction( UnityAction action ) { return new SteamVR_Events.ActionNoArgs( Initialized, action ); }
  20. //-------------------------------------------------
  21. private static ChaperoneInfo _instance;
  22. public static ChaperoneInfo instance
  23. {
  24. get
  25. {
  26. if ( _instance == null )
  27. {
  28. _instance = new GameObject( "[ChaperoneInfo]" ).AddComponent<ChaperoneInfo>();
  29. _instance.initialized = false;
  30. _instance.playAreaSizeX = 1.0f;
  31. _instance.playAreaSizeZ = 1.0f;
  32. _instance.roomscale = false;
  33. DontDestroyOnLoad( _instance.gameObject );
  34. }
  35. return _instance;
  36. }
  37. }
  38. //-------------------------------------------------
  39. IEnumerator Start()
  40. {
  41. // Uncomment for roomscale testing
  42. //_instance.initialized = true;
  43. //_instance.playAreaSizeX = UnityEngine.Random.Range( 1.0f, 4.0f );
  44. //_instance.playAreaSizeZ = UnityEngine.Random.Range( 1.0f, _instance.playAreaSizeX );
  45. //_instance.roomscale = true;
  46. //ChaperoneInfo.Initialized.Send();
  47. //yield break;
  48. // Get interface pointer
  49. var chaperone = OpenVR.Chaperone;
  50. if ( chaperone == null )
  51. {
  52. Debug.LogWarning( "Failed to get IVRChaperone interface." );
  53. initialized = true;
  54. yield break;
  55. }
  56. // Get play area size
  57. while ( true )
  58. {
  59. float px = 0.0f, pz = 0.0f;
  60. if ( chaperone.GetPlayAreaSize( ref px, ref pz ) )
  61. {
  62. initialized = true;
  63. playAreaSizeX = px;
  64. playAreaSizeZ = pz;
  65. roomscale = Mathf.Max( px, pz ) > 1.01f;
  66. Debug.LogFormat( "ChaperoneInfo initialized. {2} play area {0:0.00}m x {1:0.00}m", px, pz, roomscale ? "Roomscale" : "Standing" );
  67. ChaperoneInfo.Initialized.Send();
  68. yield break;
  69. }
  70. yield return null;
  71. }
  72. }
  73. }
  74. }