@@ -29,38 +29,57 @@ GUIStyle wrapStyle
2929 int captureCount = 100 ;
3030 float showMax = 0 ;
3131 float showMin = 0 ;
32- bool record = false ;
3332 AnimationCurve curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
3433 readonly List < ProfilerTick > currentTicks = new List < ProfilerTick > ( ) ;
3534 float lastDrawn = 0 ;
3635 struct ProfilerContainer
3736 {
3837 public ProfilerTick [ ] ticks ;
3938 }
40- private void OnGUI ( )
39+
40+ private void StopRecording ( )
4141 {
42- if ( ! NetworkProfiler . IsRunning && record )
43- {
44- if ( NetworkProfiler . Ticks != null && NetworkProfiler . Ticks . Count >= 2 )
45- curve = AnimationCurve . Constant ( NetworkProfiler . Ticks . ElementAt ( 0 ) . Frame , NetworkProfiler . Ticks . ElementAt ( NetworkProfiler . Ticks . Count - 1 ) . Frame , 0 ) ;
46- else
47- curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
42+ NetworkProfiler . Stop ( ) ;
43+ }
4844
49- lastDrawn = 0 ;
50- NetworkProfiler . Start ( captureCount ) ;
51- }
45+ private void StartRecording ( )
46+ {
47+ if ( NetworkProfiler . IsRunning )
48+ StopRecording ( ) ;
49+
50+ if ( NetworkProfiler . Ticks != null && NetworkProfiler . Ticks . Count >= 2 )
51+ curve = AnimationCurve . Constant ( NetworkProfiler . Ticks . ElementAt ( 0 ) . Frame , NetworkProfiler . Ticks . ElementAt ( NetworkProfiler . Ticks . Count - 1 ) . Frame , 0 ) ;
52+ else
53+ curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
54+
55+ lastDrawn = 0 ;
56+ NetworkProfiler . Start ( captureCount ) ;
57+ }
58+
59+ private void ChangeRecordState ( )
60+ {
61+ if ( NetworkProfiler . IsRunning ) StopRecording ( ) ;
62+ else StartRecording ( ) ;
63+ }
64+
65+ private void OnGUI ( )
66+ {
67+ bool recording = NetworkProfiler . IsRunning ;
5268
5369 //Draw top bar
5470 EditorGUILayout . BeginVertical ( ) ;
5571 EditorGUILayout . BeginHorizontal ( ) ;
56- bool prevRec = record ;
57- record = EditorGUILayout . Toggle ( "Record" , record ) ;
72+ if ( GUILayout . Button ( recording ? "Stop" : "Capture" ) ) ChangeRecordState ( ) ;
5873
5974 if ( GUILayout . Button ( "Import datafile" ) )
6075 {
6176 ProfilerTick [ ] ticks = BinarySerializer . Deserialize < ProfilerContainer > ( File . ReadAllBytes ( EditorUtility . OpenFilePanel ( "Choose a NetworkProfiler file" , "" , "" ) ) ) . ticks ;
6277 if ( ticks . Length >= 2 )
78+ {
6379 curve = AnimationCurve . Constant ( ticks [ 0 ] . EventId , ticks [ ( ticks . Length - 1 ) ] . EventId , 0 ) ;
80+ showMax = ticks . Length ;
81+ showMin = ticks . Length - Mathf . Clamp ( 100 , 0 , ticks . Length ) ;
82+ }
6483 else
6584 curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
6685 currentTicks . Clear ( ) ;
@@ -83,7 +102,11 @@ record = EditorGUILayout.Toggle("Record", record);
83102
84103 if ( GUILayout . Button ( "Export datafile" ) )
85104 {
86- File . WriteAllBytes ( EditorUtility . SaveFilePanel ( "Save NetworkProfiler data" , "" , "networkProfilerData" , "" ) , BinarySerializer . Serialize ( new ProfilerContainer ( ) { ticks = currentTicks . ToArray ( ) } ) ) ;
105+ int ticksInRange = 0 ;
106+ for ( int i = 0 ; i < currentTicks . Count ; i ++ ) if ( currentTicks [ i ] . EventId >= showMin && currentTicks [ i ] . EventId <= showMin ) ticksInRange ++ ;
107+ ProfilerTick [ ] ticks = new ProfilerTick [ ticksInRange ] ;
108+ for ( int i = 0 ; i < currentTicks . Count ; i ++ ) if ( currentTicks [ i ] . EventId >= showMin && currentTicks [ i ] . EventId <= showMin ) ticks [ i ] = currentTicks [ i ] ;
109+ File . WriteAllBytes ( EditorUtility . SaveFilePanel ( "Save NetworkProfiler data" , "" , "networkProfilerData" , "" ) , BinarySerializer . Serialize ( new ProfilerContainer ( ) { ticks = ticks } ) ) ;
87110 }
88111
89112 EditorGUILayout . EndHorizontal ( ) ;
@@ -94,34 +117,7 @@ record = EditorGUILayout.Toggle("Record", record);
94117 updateDelay = EditorGUILayout . Slider ( "Refresh delay" , updateDelay , 0.1f , 10f ) ;
95118 EditorGUILayout . EndVertical ( ) ;
96119
97- if ( prevRec != record )
98- {
99- if ( prevRec )
100- {
101- NetworkProfiler . Stop ( ) ;
102- }
103- else
104- {
105- if ( NetworkProfiler . Ticks != null && NetworkProfiler . Ticks . Count >= 2 )
106- curve = AnimationCurve . Constant ( NetworkProfiler . Ticks . ElementAt ( 0 ) . EventId , NetworkProfiler . Ticks . ElementAt ( NetworkProfiler . Ticks . Count - 1 ) . EventId , 0 ) ;
107- else
108- curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
109- lastDrawn = 0 ;
110- NetworkProfiler . Start ( captureCount ) ;
111- }
112- }
113- if ( prevHis != captureCount )
114- {
115- NetworkProfiler . Stop ( ) ;
116-
117- if ( NetworkProfiler . Ticks != null && NetworkProfiler . Ticks . Count >= 2 )
118- curve = AnimationCurve . Constant ( NetworkProfiler . Ticks . ElementAt ( 0 ) . EventId , NetworkProfiler . Ticks . ElementAt ( NetworkProfiler . Ticks . Count - 1 ) . EventId , 0 ) ;
119- else
120- curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
121-
122- lastDrawn = 0 ;
123- NetworkProfiler . Start ( captureCount ) ;
124- }
120+ if ( prevHis != captureCount ) StartRecording ( ) ;
125121
126122 //Cache
127123 if ( NetworkProfiler . IsRunning )
@@ -198,7 +194,7 @@ record = EditorGUILayout.Toggle("Record", record);
198194 {
199195 Rect dataRect = new Rect ( currentX , 100 , propWidth * emptyStreak , position . height - 100 ) ;
200196 currentX += propWidth * emptyStreak ;
201- EditorGUI . LabelField ( new Rect ( dataRect . x , dataRect . y , dataRect . width , dataRect . height ) , emptyStreak . ToString ( ) , wrapStyle ) ;
197+ if ( emptyStreak >= 4 ) EditorGUI . LabelField ( new Rect ( dataRect . x , dataRect . y , dataRect . width , dataRect . height ) , emptyStreak . ToString ( ) , wrapStyle ) ;
202198 emptyStreak = 0 ;
203199 }
204200
0 commit comments