9
9
using UnityEngine ;
10
10
using UnityEngine . Profiling ;
11
11
12
+ #if UNITY_2019_1_OR_NEWER
13
+ public static class ProfilerColumn
14
+ {
15
+ public static int DontSort = - 1 ;
16
+ public static int FunctionName = 0 ;
17
+ public static int TotalPercent = 1 ;
18
+ public static int SelfPercent = 2 ;
19
+ public static int Calls = 3 ;
20
+ public static int GCMemory = 4 ;
21
+ public static int TotalTime = 5 ;
22
+ public static int SelfTime = 6 ;
23
+ public static int DrawCalls = 7 ;
24
+ public static int TotalGPUTime = 8 ;
25
+ public static int SelfGPUTime = 9 ;
26
+ public static int TotalGPUPercent = 10 ; // 0x0000000A
27
+ public static int SelfGPUPercent = 11 ; // 0x0000000B
28
+ public static int WarningCount = 12 ; // 0x0000000C
29
+ public static int ObjectName = 13 ; // 0x0000000D
30
+ }
31
+
32
+ public static class ProfilerViewType
33
+ {
34
+ public static int Hierarchy = 0 ;
35
+ public static int Timeline = 1 ;
36
+ public static int RawHierarchy = 2 ;
37
+ }
38
+
39
+ #endif
40
+
12
41
namespace Moobyte . MemoryProfiler
13
42
{
14
43
internal struct StackSample
@@ -36,6 +65,7 @@ private static void StartRecordingQuickly()
36
65
}
37
66
38
67
private static Dictionary < int , List < int > > metadatas ;
68
+ private static byte profilerAreaCount = 0 ;
39
69
40
70
public static void StartRecording ( bool includeUnityFormat = false )
41
71
{
@@ -68,19 +98,33 @@ public static void StartRecording(bool includeUnityFormat = false)
68
98
69
99
var offset = stream . Position ;
70
100
metadatas = new Dictionary < int , List < int > > ( ) ;
71
- stream . Write ( ( byte ) ProfilerArea . AreaCount ) ; // area count
72
- for ( var area = ProfilerArea . CPU ; area < ProfilerArea . AreaCount ; area ++ )
101
+ var paType = typeof ( ProfilerArea ) ;
102
+ var areaNames = Enum . GetNames ( paType ) ;
103
+
104
+ profilerAreaCount = ( byte ) areaNames . Length ;
105
+ if ( areaNames [ areaNames . Length - 1 ] . Equals ( "AreaCount" ) )
73
106
{
107
+ -- profilerAreaCount ;
108
+ }
109
+
110
+ stream . Write ( profilerAreaCount ) ; // area count
111
+ for ( var area = 0 ; area < profilerAreaCount ; area ++ )
112
+ {
113
+ var areaValue = ( ProfilerArea ) area ;
74
114
stream . Write ( ( byte ) area ) ;
75
- stream . Write ( area . ToString ( ) ) ;
115
+ stream . Write ( areaValue . ToString ( ) ) ;
76
116
List < int > children ;
77
117
metadatas . Add ( ( int ) area , children = new List < int > ( ) ) ;
78
- var properties = ProfilerDriver . GetGraphStatisticsPropertiesForArea ( area ) ;
118
+ var properties = ProfilerDriver . GetGraphStatisticsPropertiesForArea ( areaValue ) ;
79
119
stream . Write ( ( byte ) properties . Length ) ;
80
120
for ( var i = 0 ; i < properties . Length ; i ++ )
81
121
{
82
122
var name = properties [ i ] ;
123
+ #if UNITY_2018_1_OR_NEWER
124
+ var identifier = ProfilerDriver . GetStatisticsIdentifierForArea ( areaValue , name ) ;
125
+ #else
83
126
var identifier = ProfilerDriver . GetStatisticsIdentifier ( name ) ;
127
+ #endif
84
128
stream . Write ( name ) ;
85
129
86
130
children . Add ( identifier ) ;
@@ -109,7 +153,6 @@ public static void StartRecording(bool includeUnityFormat = false)
109
153
[ MenuItem ( "性能/停止采样" , false , 55 ) ]
110
154
public static void StopRecording ( )
111
155
{
112
- Profiler . enabled = false ;
113
156
Profiler . logFile = null ;
114
157
EditorApplication . update -= Update ;
115
158
@@ -166,8 +209,6 @@ internal static int getStringRef(string v)
166
209
static void Update ( )
167
210
{
168
211
var stopFrameIndex = ProfilerDriver . lastFrameIndex ;
169
-
170
-
171
212
var frameIndex = Math . Max ( frameCursor + 1 , ProfilerDriver . firstFrameIndex ) ;
172
213
while ( frameIndex <= stopFrameIndex )
173
214
{
@@ -187,8 +228,8 @@ static void Update()
187
228
{
188
229
cursor . Pop ( ) ;
189
230
}
190
-
191
- var drawCalls = root . GetColumnAsSingle ( ProfilerColumn . DrawCalls ) ;
231
+
232
+ #if UNITY_2017_1_OR_NEWER
192
233
samples . Add ( sequence , new StackSample
193
234
{
194
235
id = sequence ,
@@ -198,11 +239,16 @@ static void Update()
198
239
totalTime = root . GetColumnAsSingle ( ProfilerColumn . TotalTime ) ,
199
240
selfTime = root . GetColumnAsSingle ( ProfilerColumn . SelfTime ) ,
200
241
} ) ;
201
-
202
- if ( cursor . Count != 0 )
203
- {
204
- relations [ sequence ] = cursor . Peek ( ) ;
205
- }
242
+ #else
243
+ var ss = new StackSample { id = sequence , name = root . propertyName } ;
244
+ int . TryParse ( root . GetColumn ( ProfilerColumn . Calls ) , out ss . callsCount ) ;
245
+ int . TryParse ( root . GetColumn ( ProfilerColumn . GCMemory ) , out ss . gcAllocBytes ) ;
246
+ float . TryParse ( root . GetColumn ( ProfilerColumn . TotalTime ) , out ss . totalTime ) ;
247
+ float . TryParse ( root . GetColumn ( ProfilerColumn . SelfTime ) , out ss . selfTime ) ;
248
+ samples . Add ( sequence , ss ) ;
249
+ #endif
250
+
251
+ relations [ sequence ] = cursor . Count != 0 ? cursor . Peek ( ) : - 1 ;
206
252
207
253
if ( root . HasChildren )
208
254
{
@@ -217,9 +263,26 @@ static void Update()
217
263
stream . Write ( frameIndex ) ;
218
264
stream . Write ( string . IsNullOrEmpty ( root . frameTime ) ? ( 1000f / frameFPS ) : float . Parse ( root . frameTime ) ) ;
219
265
stream . Write ( frameFPS ) ;
220
-
266
+ if ( frameIndex == stopFrameIndex ) // encode memory info
267
+ {
268
+ stream . Write ( ( ushort ) 0 ) ;
269
+ /**
270
+ stream.Write((ushort) (8 * 6));
271
+ stream.Write(Profiler.usedHeapSizeLong); // 1
272
+ stream.Write(Profiler.GetMonoUsedSizeLong()); // 2
273
+ stream.Write(Profiler.GetMonoHeapSizeLong()); // 3
274
+ stream.Write(Profiler.GetTotalAllocatedMemoryLong()); // 4
275
+ stream.Write(Profiler.GetTotalReservedMemoryLong()); // 5
276
+ stream.Write(Profiler.GetTotalUnusedReservedMemoryLong()); // 6
277
+ */
278
+ }
279
+ else
280
+ {
281
+ stream . Write ( ( ushort ) 0 ) ;
282
+ }
283
+
221
284
//encode statistics
222
- for ( ProfilerArea area = 0 ; area < ProfilerArea . AreaCount ; area ++ )
285
+ for ( var area = 0 ; area < profilerAreaCount ; area ++ )
223
286
{
224
287
var statistics = metadatas [ ( int ) area ] ;
225
288
stream . Write ( ( byte ) area ) ;
@@ -260,4 +323,4 @@ static void Update()
260
323
frameCursor = stopFrameIndex ;
261
324
}
262
325
}
263
- }
326
+ }
0 commit comments