IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_Stats.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Helper to display various hmd stats via GUIText
4 //
5 //=============================================================================
6 
7 using UnityEngine;
8 
9 #if UNITY_2017_2_OR_NEWER
10 
11 [ExecuteInEditMode]
12 public class SteamVR_Stats : MonoBehaviour
13 {
14  void Awake()
15  {
16  Debug.Log("SteamVR_Stats is deprecated in Unity 2017.2 - REMOVING");
17  DestroyImmediate(this);
18  }
19 }
20 
21 #else
22 
23 using Valve.VR;
24 
25 public class SteamVR_Stats : MonoBehaviour
26 {
27  public GUIText text;
28 
29  public Color fadeColor = Color.black;
30  public float fadeDuration = 1.0f;
31 
32  void Awake()
33  {
34  if (text == null)
35  {
36  text = GetComponent<GUIText>();
37  text.enabled = false;
38  }
39 
40  if (fadeDuration > 0)
41  {
42  SteamVR_Fade.Start(fadeColor, 0);
43  SteamVR_Fade.Start(Color.clear, fadeDuration);
44  }
45  }
46 
47  double lastUpdate = 0.0f;
48 
49  void Update()
50  {
51  if (text != null)
52  {
53  if (Input.GetKeyDown(KeyCode.I))
54  {
55  text.enabled = !text.enabled;
56  }
57 
58  if (text.enabled)
59  {
60  var compositor = OpenVR.Compositor;
61  if (compositor != null)
62  {
63  var timing = new Compositor_FrameTiming();
64  timing.m_nSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Compositor_FrameTiming));
65  compositor.GetFrameTiming(ref timing, 0);
66 
67  var update = timing.m_flSystemTimeInSeconds;
68  if (update > lastUpdate)
69  {
70  var framerate = (lastUpdate > 0.0f) ? 1.0f / (update - lastUpdate) : 0.0f;
71  lastUpdate = update;
72  text.text = string.Format("framerate: {0:N0}\ndropped frames: {1}", framerate, (int)timing.m_nNumDroppedFrames);
73  }
74  else
75  {
76  lastUpdate = update;
77  }
78  }
79  }
80  }
81  }
82 }
83 
84 #endif
85