@@ -25,14 +25,16 @@ GUIStyle wrapStyle
25
25
return style ;
26
26
}
27
27
}
28
+
29
+ float hoverAlpha = 0f ;
28
30
float updateDelay = 1f ;
29
31
int captureCount = 100 ;
30
32
float showMax = 0 ;
31
33
float showMin = 0 ;
32
34
AnimationCurve curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
33
35
readonly List < ProfilerTick > currentTicks = new List < ProfilerTick > ( ) ;
34
36
float lastDrawn = 0 ;
35
- struct ProfilerContainer
37
+ class ProfilerContainer
36
38
{
37
39
public ProfilerTick [ ] ticks ;
38
40
}
@@ -46,7 +48,7 @@ private void StartRecording()
46
48
{
47
49
if ( NetworkProfiler . IsRunning )
48
50
StopRecording ( ) ;
49
-
51
+
50
52
if ( NetworkProfiler . Ticks != null && NetworkProfiler . Ticks . Count >= 2 )
51
53
curve = AnimationCurve . Constant ( NetworkProfiler . Ticks . ElementAt ( 0 ) . Frame , NetworkProfiler . Ticks . ElementAt ( NetworkProfiler . Ticks . Count - 1 ) . Frame , 0 ) ;
52
54
else
@@ -56,57 +58,78 @@ private void StartRecording()
56
58
NetworkProfiler . Start ( captureCount ) ;
57
59
}
58
60
61
+ private void ClearDrawing ( )
62
+ {
63
+ curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
64
+ lastDrawn = 0 ;
65
+ }
66
+
59
67
private void ChangeRecordState ( )
60
68
{
61
69
if ( NetworkProfiler . IsRunning ) StopRecording ( ) ;
62
70
else StartRecording ( ) ;
63
71
}
64
72
73
+ TickEvent eventHover = null ;
74
+ double lastSetup = 0 ;
65
75
private void OnGUI ( )
66
76
{
67
77
bool recording = NetworkProfiler . IsRunning ;
78
+ float deltaTime = ( float ) ( EditorApplication . timeSinceStartup - lastSetup ) ;
79
+ lastSetup = EditorApplication . timeSinceStartup ;
68
80
69
81
//Draw top bar
70
82
EditorGUILayout . BeginVertical ( ) ;
71
83
EditorGUILayout . BeginHorizontal ( ) ;
72
84
if ( GUILayout . Button ( recording ? "Stop" : "Capture" ) ) ChangeRecordState ( ) ;
73
85
86
+ if ( GUILayout . Button ( "Clear" ) ) ClearDrawing ( ) ;
87
+ EditorGUILayout . Space ( ) ;
88
+ EditorGUILayout . Space ( ) ;
89
+ EditorGUILayout . Space ( ) ;
90
+
74
91
if ( GUILayout . Button ( "Import datafile" ) )
75
92
{
76
- ProfilerTick [ ] ticks = BinarySerializer . Deserialize < ProfilerContainer > ( File . ReadAllBytes ( EditorUtility . OpenFilePanel ( "Choose a NetworkProfiler file" , "" , "" ) ) ) . ticks ;
77
- if ( ticks . Length >= 2 )
78
- {
79
- 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
- }
83
- else
84
- curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
85
- currentTicks . Clear ( ) ;
86
- for ( int i = 0 ; i < ticks . Length ; i ++ )
93
+ string path = EditorUtility . OpenFilePanel ( "Choose a NetworkProfiler file" , "" , "" ) ;
94
+ if ( ! string . IsNullOrEmpty ( path ) )
87
95
{
88
- currentTicks . Add ( ticks [ i ] ) ;
89
-
90
- uint bytes = 0 ;
91
- if ( ticks [ i ] . Events . Count > 0 )
96
+ ProfilerTick [ ] ticks = BinarySerializer . Deserialize < ProfilerContainer > ( File . ReadAllBytes ( path ) ) . ticks ;
97
+ if ( ticks . Length >= 2 )
98
+ {
99
+ curve = AnimationCurve . Constant ( ticks [ 0 ] . EventId , ticks [ ( ticks . Length - 1 ) ] . EventId , 0 ) ;
100
+ showMax = ticks . Length ;
101
+ showMin = ticks . Length - Mathf . Clamp ( 100 , 0 , ticks . Length ) ;
102
+ }
103
+ else
104
+ curve = AnimationCurve . Constant ( 0 , 1 , 0 ) ;
105
+ currentTicks . Clear ( ) ;
106
+ for ( int i = 0 ; i < ticks . Length ; i ++ )
92
107
{
93
- for ( int j = 0 ; j < ticks [ i ] . Events . Count ; j ++ )
108
+ currentTicks . Add ( ticks [ i ] ) ;
109
+
110
+ uint bytes = 0 ;
111
+ if ( ticks [ i ] . Events . Count > 0 )
94
112
{
95
- TickEvent tickEvent = ticks [ i ] . Events [ j ] ;
96
- bytes += tickEvent . Bytes ;
113
+ for ( int j = 0 ; j < ticks [ i ] . Events . Count ; j ++ )
114
+ {
115
+ TickEvent tickEvent = ticks [ i ] . Events [ j ] ;
116
+ bytes += tickEvent . Bytes ;
117
+ }
97
118
}
119
+ curve . AddKey ( ticks [ i ] . EventId , bytes ) ;
98
120
}
99
- curve . AddKey ( ticks [ i ] . EventId , bytes ) ;
100
121
}
101
122
}
102
123
103
124
if ( GUILayout . Button ( "Export datafile" ) )
104
125
{
105
- int ticksInRange = 0 ;
106
- for ( int i = 0 ; i < currentTicks . Count ; i ++ ) if ( currentTicks [ i ] . EventId >= showMin && currentTicks [ i ] . EventId <= showMin ) ticksInRange ++ ;
126
+ int max = ( int ) showMax ;
127
+ int min = ( int ) showMin ;
128
+ int ticksInRange = max - min ;
107
129
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 } ) ) ;
130
+ for ( int i = min ; i < max ; i ++ ) ticks [ i - min ] = currentTicks [ i ] ;
131
+ string path = EditorUtility . SaveFilePanel ( "Save NetworkProfiler data" , "" , "networkProfilerData" , "" ) ;
132
+ if ( ! string . IsNullOrEmpty ( path ) ) File . WriteAllBytes ( path , BinarySerializer . Serialize ( new ProfilerContainer ( ) { ticks = ticks } ) ) ;
110
133
}
111
134
112
135
EditorGUILayout . EndHorizontal ( ) ;
@@ -164,7 +187,7 @@ private void OnGUI()
164
187
}
165
188
166
189
//Draw main board
167
- TickEvent eventHover = null ;
190
+ bool hover = false ;
168
191
int nonEmptyTicks = 0 ;
169
192
int largestTickCount = 0 ;
170
193
int totalTicks = ( ( int ) showMax - ( int ) showMin ) ;
@@ -209,7 +232,11 @@ private void OnGUI()
209
232
TickEvent tickEvent = tick . Events [ j ] ;
210
233
Rect dataRect = new Rect ( currentX , currentY , widthPerTick , heightPerEvent ) ;
211
234
212
- if ( dataRect . Contains ( Event . current . mousePosition ) ) eventHover = tickEvent ;
235
+ if ( dataRect . Contains ( Event . current . mousePosition ) )
236
+ {
237
+ hover = true ;
238
+ eventHover = tickEvent ;
239
+ }
213
240
214
241
if ( j == tick . Events . Count - 1 )
215
242
dataRect . height -= 45f ;
@@ -231,17 +258,32 @@ private void OnGUI()
231
258
currentX += widthPerTick ;
232
259
}
233
260
261
+ //Calculate alpha
262
+ if ( hover )
263
+ {
264
+ hoverAlpha += deltaTime * 10f ;
265
+
266
+ if ( hoverAlpha > 1f ) hoverAlpha = 1f ;
267
+ else if ( hoverAlpha < 0f ) hoverAlpha = 0f ;
268
+ }
269
+ else
270
+ {
271
+ hoverAlpha -= deltaTime * 10f ;
272
+ if ( hoverAlpha > 1f ) hoverAlpha = 1f ;
273
+ else if ( hoverAlpha < 0f ) hoverAlpha = 0f ;
274
+ }
275
+
234
276
//Draw hover thingy
235
277
if ( eventHover != null )
236
278
{
237
279
Rect rect = new Rect ( Event . current . mousePosition , new Vector2 ( 500 , 100 ) ) ;
238
- EditorGUI . DrawRect ( rect , EditorColor ) ;
280
+ EditorGUI . DrawRect ( rect , GetEditorColorWithAlpha ( hoverAlpha ) ) ;
239
281
240
282
float heightPerField = ( rect . height - 5 ) / 4 ;
241
- EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + 5 , rect . width , rect . height ) , "EventType: " + eventHover . EventType . ToString ( ) ) ;
242
- EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + heightPerField * 1 + 5 , rect . width , rect . height ) , "Size: " + eventHover . Bytes + "B" ) ;
243
- EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + heightPerField * 2 + 5 , rect . width , rect . height ) , "Channel: " + eventHover . ChannelName ) ;
244
- EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + heightPerField * 3 + 5 , rect . width , rect . height ) , "MessageType: " + eventHover . MessageType ) ;
283
+ EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + 5 , rect . width , rect . height ) , "EventType: " + eventHover . EventType . ToString ( ) , GetStyleWithTextAlpha ( EditorStyles . label , hoverAlpha ) ) ;
284
+ EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + heightPerField * 1 + 5 , rect . width , rect . height ) , "Size: " + eventHover . Bytes + "B" , GetStyleWithTextAlpha ( EditorStyles . label , hoverAlpha ) ) ;
285
+ EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + heightPerField * 2 + 5 , rect . width , rect . height ) , "Channel: " + eventHover . ChannelName , GetStyleWithTextAlpha ( EditorStyles . label , hoverAlpha ) ) ;
286
+ EditorGUI . LabelField ( new Rect ( rect . x + 5 , rect . y + heightPerField * 3 + 5 , rect . width , rect . height ) , "MessageType: " + eventHover . MessageType , GetStyleWithTextAlpha ( EditorStyles . label , hoverAlpha ) ) ;
245
287
}
246
288
247
289
Repaint ( ) ;
@@ -269,6 +311,20 @@ private Color EditorColor
269
311
return EditorGUIUtility . isProSkin ? new Color32 ( 56 , 56 , 56 , 255 ) : new Color32 ( 194 , 194 , 194 , 255 ) ;
270
312
}
271
313
}
314
+
315
+ private Color GetEditorColorWithAlpha ( float alpha )
316
+ {
317
+ return EditorGUIUtility . isProSkin ? new Color ( 0.22f , 0.22f , 0.22f , alpha ) : new Color ( 0.76f , 0.76f , 0.76f , alpha ) ;
318
+ }
319
+
320
+ private GUIStyle GetStyleWithTextAlpha ( GUIStyle style , float alpha )
321
+ {
322
+ Color textColor = style . normal . textColor ;
323
+ textColor . a = alpha ;
324
+ GUIStyle newStyle = new GUIStyle ( style ) ;
325
+ newStyle . normal . textColor = textColor ;
326
+ return newStyle ;
327
+ }
272
328
}
273
329
274
330
}
0 commit comments