IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
ChaperoneInfo.cs
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 
7 using UnityEngine;
8 using UnityEngine.Events;
9 using System.Collections;
10 
11 namespace Valve.VR.InteractionSystem
12 {
13  //-------------------------------------------------------------------------
14  public class ChaperoneInfo : MonoBehaviour
15  {
16  public bool initialized { get; private set; }
17  public float playAreaSizeX { get; private set; }
18  public float playAreaSizeZ { get; private set; }
19  public bool roomscale { get; private set; }
20 
21  public static SteamVR_Events.Event Initialized = new SteamVR_Events.Event();
22  public static SteamVR_Events.Action InitializedAction( UnityAction action ) { return new SteamVR_Events.ActionNoArgs( Initialized, action ); }
23 
24  //-------------------------------------------------
25  private static ChaperoneInfo _instance;
26  public static ChaperoneInfo instance
27  {
28  get
29  {
30  if ( _instance == null )
31  {
32  _instance = new GameObject( "[ChaperoneInfo]" ).AddComponent<ChaperoneInfo>();
33  _instance.initialized = false;
34  _instance.playAreaSizeX = 1.0f;
35  _instance.playAreaSizeZ = 1.0f;
36  _instance.roomscale = false;
37 
38  DontDestroyOnLoad( _instance.gameObject );
39  }
40  return _instance;
41  }
42  }
43 
44 
45  //-------------------------------------------------
46  IEnumerator Start()
47  {
48  // Uncomment for roomscale testing
49  //_instance.initialized = true;
50  //_instance.playAreaSizeX = UnityEngine.Random.Range( 1.0f, 4.0f );
51  //_instance.playAreaSizeZ = UnityEngine.Random.Range( 1.0f, _instance.playAreaSizeX );
52  //_instance.roomscale = true;
53  //ChaperoneInfo.Initialized.Send();
54  //yield break;
55 
56  // Get interface pointer
57  var chaperone = OpenVR.Chaperone;
58  if ( chaperone == null )
59  {
60  Debug.LogWarning( "Failed to get IVRChaperone interface." );
61  initialized = true;
62  yield break;
63  }
64 
65  // Get play area size
66  while ( true )
67  {
68  float px = 0.0f, pz = 0.0f;
69  if ( chaperone.GetPlayAreaSize( ref px, ref pz ) )
70  {
71  initialized = true;
72  playAreaSizeX = px;
73  playAreaSizeZ = pz;
74  roomscale = Mathf.Max( px, pz ) > 1.01f;
75 
76  Debug.LogFormat( "ChaperoneInfo initialized. {2} play area {0:0.00}m x {1:0.00}m", px, pz, roomscale ? "Roomscale" : "Standing" );
77 
78  ChaperoneInfo.Initialized.Send();
79 
80  yield break;
81  }
82 
83  yield return null;
84  }
85  }
86  }
87 }