SteamVR_Stats.cs 1.7 KB

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