OVRProfiler.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. using Assets.OVR.Scripts;
  6. public class OVRProfiler : EditorWindow
  7. {
  8. enum TargetPlatform
  9. {
  10. OculusGo,
  11. GearVR,
  12. SantaCruz,
  13. OculusRift
  14. };
  15. private static List<RangedRecord> mRecords = new List<RangedRecord>();
  16. private Vector2 mScrollPosition;
  17. static private TargetPlatform mTargetPlatform;
  18. [MenuItem("Tools/Oculus/OVR Profiler")]
  19. static void Init()
  20. {
  21. // Get existing open window or if none, make a new one:
  22. EditorWindow.GetWindow(typeof(OVRProfiler));
  23. #if UNITY_ANDROID
  24. mTargetPlatform = TargetPlatform.OculusGo;
  25. #else
  26. mTargetPlatform = TargetPlatform.OculusRift;
  27. #endif
  28. }
  29. void OnGUI()
  30. {
  31. GUILayout.Label("OVR Profiler", EditorStyles.boldLabel);
  32. string[] options = new string[]
  33. {
  34. "Oculus Go", "Gear VR", "Santa Cruz", "Oculus Rift",
  35. };
  36. mTargetPlatform = (TargetPlatform)EditorGUILayout.Popup("Target Oculus Platform", (int)mTargetPlatform, options);
  37. if (EditorApplication.isPlaying)
  38. {
  39. UpdateRecords();
  40. DrawResults();
  41. }
  42. else
  43. {
  44. ShowCenterAlignedMessageLabel("Click Run in Unity to view stats.");
  45. }
  46. }
  47. void OnInspectorUpdate()
  48. {
  49. Repaint();
  50. }
  51. void DrawResults()
  52. {
  53. string lastCategory = "";
  54. mScrollPosition = EditorGUILayout.BeginScrollView(mScrollPosition);
  55. foreach (RangedRecord record in mRecords)
  56. {
  57. // Add separator and label for new category
  58. if (!record.category.Equals(lastCategory))
  59. {
  60. lastCategory = record.category;
  61. EditorGUILayout.Separator();
  62. EditorGUILayout.BeginHorizontal();
  63. GUILayout.Label(lastCategory, EditorStyles.label, GUILayout.Width(200));
  64. EditorGUILayout.EndHorizontal();
  65. }
  66. // Draw records
  67. EditorGUILayout.BeginHorizontal();
  68. Rect r = EditorGUILayout.BeginVertical();
  69. EditorGUI.ProgressBar(r, record.value / (record.max * 2), record.category + " " + record.value.ToString());
  70. GUILayout.Space(16);
  71. EditorGUILayout.EndVertical();
  72. EditorGUILayout.EndHorizontal();
  73. EditorGUILayout.BeginHorizontal();
  74. GUILayout.Label(record.message);
  75. EditorGUILayout.EndHorizontal();
  76. GUI.enabled = true;
  77. }
  78. EditorGUILayout.EndScrollView();
  79. }
  80. private void UpdateRecords()
  81. {
  82. mRecords.Clear();
  83. if (mTargetPlatform == TargetPlatform.OculusRift)
  84. {
  85. AddRecord("Client Frame CPU Time (ms)", "", UnityStats.frameTime * 1000, 0, 11);
  86. AddRecord("Render Frame CPU Time (ms)", "", UnityStats.renderTime * 1000, 0, 11);
  87. }
  88. else
  89. {
  90. // Graphics memory
  91. long memSizeByte = UnityStats.usedTextureMemorySize + UnityStats.vboTotalBytes;
  92. AddRecord("Graphics Memory (MB)", "Please use less than 1024 MB of vertex and texture memory.", ConvertBytes(memSizeByte, "MB"), 0, 1024);
  93. }
  94. float triVertRec = mTargetPlatform == TargetPlatform.OculusRift ? 1000000 : 100000;
  95. // Triangle count
  96. AddRecord("Triangles", "Please use less than 100000 triangles.", UnityStats.triangles, 0, triVertRec);
  97. // Vertices count
  98. AddRecord("Vertices", "Please use less than 100000 vertices.", UnityStats.vertices, 0, triVertRec);
  99. float dcRec = mTargetPlatform == TargetPlatform.OculusRift ? 1000 : 100;
  100. // Draw call count
  101. AddRecord("Draw Call", "Please use less than 100 draw calls.", UnityStats.drawCalls, 0, dcRec);
  102. }
  103. private string FormatBytes(long bytes, string target)
  104. {
  105. return System.String.Format("{0:0.##} {1}", ConvertBytes(bytes, target), target);
  106. }
  107. private float ConvertBytes(long bytes, string target)
  108. {
  109. string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
  110. int i;
  111. double dblSByte = bytes;
  112. for (i = 0; i < Suffix.Length; i++, bytes /= 1024)
  113. {
  114. if (Suffix[i] == target)
  115. return (float)dblSByte;
  116. dblSByte = bytes / 1024.0;
  117. }
  118. return 0;
  119. }
  120. private void ShowCenterAlignedMessageLabel(string message)
  121. {
  122. GUILayout.BeginVertical();
  123. GUILayout.FlexibleSpace();
  124. GUILayout.BeginHorizontal();
  125. GUILayout.FlexibleSpace();
  126. GUILayout.Label(message, EditorStyles.boldLabel);
  127. GUILayout.FlexibleSpace();
  128. GUILayout.EndHorizontal();
  129. GUILayout.FlexibleSpace();
  130. GUILayout.EndVertical();
  131. }
  132. private void AddRecord(string category, string message, float value, float min, float max)
  133. {
  134. RangedRecord record = new RangedRecord(category, message, value, min, max);
  135. mRecords.Add(record);
  136. }
  137. }
  138. #endif