AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
UAudioProfiler.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEditor;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity
9 {
10  public class UAudioProfiler : EditorWindow
11  {
12  private int currentFrame = 0;
13  private List<ProfilerEvent[]> eventTimeline;
14  private Vector2 scrollOffset = new Vector2();
15  private const int MaxFrames = 300;
16 
17  private class ProfilerEvent
18  {
19  public string EventName = "";
20  public string EmitterName = "";
21  public string BusName = "";
22  }
23 
24  [MenuItem("Mixed Reality Toolkit/UAudioTools/Profiler", false, 200)]
25  static void ShowEditor()
26  {
27  UAudioProfiler profilerWindow = GetWindow<UAudioProfiler>();
28  if (profilerWindow.eventTimeline == null)
29  {
30  profilerWindow.currentFrame = 0;
31  profilerWindow.eventTimeline = new List<ProfilerEvent[]>();
32  }
33  profilerWindow.Show();
34  }
35 
36  // Only update the currently-playing events 10 times a second - we don't need millisecond-accurate profiling
37  private void OnInspectorUpdate()
38  {
39  if (!EditorApplication.isPlaying)
40  {
41  return;
42  }
43 
44  ProfilerEvent[] currentEvents = new ProfilerEvent[0];
45 
46  if (this.eventTimeline == null)
47  {
48  this.eventTimeline = new List<ProfilerEvent[]>();
49  }
50 
51  if (UAudioManager.Instance != null && !EditorApplication.isPaused)
52  {
53  CollectProfilerEvents(currentEvents);
54  }
55 
56  Repaint();
57  }
58 
59  // Populate an array of the active events, and add it to the timeline list of all captured audio frames.
60  private void CollectProfilerEvents(ProfilerEvent[] currentEvents)
61  {
62  List<ActiveEvent> activeEvents = UAudioManager.Instance.ProfilerEvents;
63  currentEvents = new ProfilerEvent[activeEvents.Count];
64  for (int i = 0; i < currentEvents.Length; i++)
65  {
66  ActiveEvent currentEvent = activeEvents[i];
67  ProfilerEvent tempEvent = new ProfilerEvent();
68  tempEvent.EventName = currentEvent.AudioEvent.Name;
69  tempEvent.EmitterName = currentEvent.AudioEmitter.name;
70 
71  // The bus might be null, Unity defaults to Editor-hidden master bus.
72  if (currentEvent.AudioEvent.AudioBus == null)
73  {
74  tempEvent.BusName = "-MasterBus-";
75  }
76  else
77  {
78  tempEvent.BusName = currentEvent.AudioEvent.AudioBus.name;
79  }
80 
81  currentEvents[i] = tempEvent;
82  }
83  this.eventTimeline.Add(currentEvents);
84 
85  // Trim the first event if we have exceeded the maximum stored frames.
86  if (this.eventTimeline.Count > MaxFrames)
87  {
88  this.eventTimeline.RemoveAt(0);
89  }
90  this.currentFrame = this.eventTimeline.Count - 1;
91  }
92 
93  // Draw the profiler window.
94  private void OnGUI()
95  {
96  if (!EditorApplication.isPlaying)
97  {
98  EditorGUILayoutExtensions.Label("Profiler only active in play mode!");
99  return;
100  }
101 
102  //Fix null reference exception when launching with profiler is open
103  if(eventTimeline!=null)
104  {
105  this.currentFrame = EditorGUILayout.IntSlider(this.currentFrame, 0, this.eventTimeline.Count - 1);
106  scrollOffset = EditorGUILayout.BeginScrollView(scrollOffset);
107 
108  if (this.eventTimeline.Count > this.currentFrame)
109  {
110  for (int i = 0; i < this.eventTimeline[this.currentFrame].Length; i++)
111  {
112  DrawEventButton(this.eventTimeline[this.currentFrame][i], i);
113  }
114  }
115 
116  EditorGUILayout.EndScrollView();
117  }
118 
119  }
120 
121  private void DrawEventButton(ProfilerEvent currentEvent, int id)
122  {
123  EditorGUILayout.SelectableLabel(currentEvent.EventName + "-->(" + currentEvent.EmitterName + ")-->(" + currentEvent.BusName + ")");
124  }
125  }
126 }
Extensions for the UnityEditor.EditorGUILayout class.
Currently active AudioEvents along with their AudioSource components for instance limiting events ...
Definition: ActiveEvent.cs:12
static UAudioManager Instance
AudioMixerGroup AudioBus
Definition: AudioEvent.cs:82
static void Label(string text, params GUILayoutOption[] options)
The UAudioManager class is a singleton that provides organization and control of an application&#39;s Aud...