1
- using System . Collections ;
1
+ using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
4
+ using System . Linq ;
3
5
using UnityEngine ;
6
+ using UnityEngine . Profiling ;
4
7
5
8
namespace Wanderer . GameFramework
6
9
{
@@ -24,27 +27,52 @@ public override void OnInit(params object[] args)
24
27
}
25
28
26
29
//概要分析
27
- internal class SummaryProfiler : IDebuggerWindow
30
+ internal class SummaryProfiler : DebuggerWindowBase
28
31
{
29
- public void OnInit ( params object [ ] args )
30
- {
31
- }
32
-
33
- public void OnClose ( )
34
- {
35
- }
36
-
37
- public void OnDraw ( )
38
- {
39
- }
40
-
41
- public void OnEnter ( )
32
+ public override void OnDraw ( )
42
33
{
34
+ GUILayout . Label ( "<b>Profiler Information</b>" ) ;
35
+ GUILayout . BeginVertical ( "box" ) ;
36
+ {
37
+ GuiUtility . DrawItem ( "Supported" , Profiler . supported . ToString ( ) ) ;
38
+ GuiUtility . DrawItem ( "Enabled" , Profiler . enabled . ToString ( ) ) ;
39
+ GuiUtility . DrawItem ( "Enable Binary Log" , Profiler . enableBinaryLog ? string . Format ( "True, {0}" , Profiler . logFile ) : "False" ) ;
40
+ #if UNITY_2018_3_OR_NEWER
41
+ GuiUtility . DrawItem ( "Area Count" , Profiler . areaCount . ToString ( ) ) ;
42
+ #endif
43
+ #if UNITY_5_3 || UNITY_5_4
44
+ GuiUtility . DrawItem ( "Max Samples Number Per Frame" , Profiler . maxNumberOfSamplesPerFrame . ToString ( ) ) ;
45
+ #endif
46
+ #if UNITY_2018_3_OR_NEWER
47
+ GuiUtility . DrawItem ( "Max Used Memory" , Profiler . maxUsedMemory . ToByteLengthString ( ) ) ;
48
+ #endif
49
+ #if UNITY_5_6_OR_NEWER
50
+ GuiUtility . DrawItem ( "Mono Used Size" , Profiler . GetMonoUsedSizeLong ( ) . ToByteLengthString ( ) ) ;
51
+ GuiUtility . DrawItem ( "Mono Heap Size" , Profiler . GetMonoHeapSizeLong ( ) . ToByteLengthString ( ) ) ;
52
+ GuiUtility . DrawItem ( "Used Heap Size" , Profiler . usedHeapSizeLong . ToByteLengthString ( ) ) ;
53
+ GuiUtility . DrawItem ( "Total Allocated Memory" , Profiler . GetTotalAllocatedMemoryLong ( ) . ToByteLengthString ( ) ) ;
54
+ GuiUtility . DrawItem ( "Total Reserved Memory" , Profiler . GetTotalReservedMemoryLong ( ) . ToByteLengthString ( ) ) ;
55
+ GuiUtility . DrawItem ( "Total Unused Reserved Memory" , Profiler . GetTotalUnusedReservedMemoryLong ( ) . ToByteLengthString ( ) ) ;
56
+ #else
57
+ GuiUtility . DrawItem ( "Mono Used Size" , GetByteLengthString ( Profiler . GetMonoUsedSize ( ) ) ) ;
58
+ GuiUtility . DrawItem ( "Mono Heap Size" , GetByteLengthString ( Profiler . GetMonoHeapSize ( ) ) ) ;
59
+ GuiUtility . DrawItem ( "Used Heap Size" , GetByteLengthString ( Profiler . usedHeapSize ) ) ;
60
+ GuiUtility . DrawItem ( "Total Allocated Memory" , GetByteLengthString ( Profiler . GetTotalAllocatedMemory ( ) ) ) ;
61
+ GuiUtility . DrawItem ( "Total Reserved Memory" , GetByteLengthString ( Profiler . GetTotalReservedMemory ( ) ) ) ;
62
+ GuiUtility . DrawItem ( "Total Unused Reserved Memory" , GetByteLengthString ( Profiler . GetTotalUnusedReservedMemory ( ) ) ) ;
63
+ #endif
64
+ #if UNITY_2018_1_OR_NEWER
65
+ GuiUtility . DrawItem ( "Allocated Memory For Graphics Driver" , Profiler . GetAllocatedMemoryForGraphicsDriver ( ) . ToByteLengthString ( ) ) ;
66
+ #endif
67
+ #if UNITY_5_5_OR_NEWER
68
+ GuiUtility . DrawItem ( "Temp Allocator Size" , Profiler . GetTempAllocatorSize ( ) . ToByteLengthString ( ) ) ;
69
+ // GuiUtility.DrawItem("Marshal Cached HGlobal Size", Utility.Marshal.CachedHGlobalSize.);
70
+ // GuiUtility.DrawItem("Data Provider Cached Bytes Size", DataProviderCreator.CachedBytesSize);
71
+ #endif
72
+ }
73
+ GUILayout . EndVertical ( ) ;
43
74
}
44
75
45
- public void OnExit ( )
46
- {
47
- }
48
76
}
49
77
50
78
//内存分析
@@ -68,9 +96,13 @@ public override void OnInit(params object[] args)
68
96
}
69
97
70
98
//内存分析基类
71
- internal abstract class MemoryProfilerBase : IDebuggerWindow
99
+ internal abstract class MemoryProfilerBase < T > : IDebuggerWindow where T : UnityEngine . Object
72
100
{
73
101
Vector2 _scrollPos = Vector2 . zero ;
102
+ protected List < ProfilerSample > _samples = new List < ProfilerSample > ( ) ;
103
+ protected long _sampleSize = 0 ;
104
+ protected DateTime _sampleDateTime = DateTime . MinValue ;
105
+
74
106
public virtual void OnInit ( params object [ ] args )
75
107
{
76
108
}
@@ -90,6 +122,8 @@ public virtual void OnExit()
90
122
public virtual void OnDraw ( )
91
123
{
92
124
_scrollPos = GUILayout . BeginScrollView ( _scrollPos , "box" ) ;
125
+ string typeName = typeof ( T ) . Name ;
126
+ GUILayout . Label ( string . Format ( "<b>{0} Runtime Memory Information</b>" , typeName ) ) ;
93
127
if ( GUILayout . Button ( "Take Sample" ) )
94
128
{
95
129
TakeSample ( ) ;
@@ -98,131 +132,208 @@ public virtual void OnDraw()
98
132
GUILayout . EndScrollView ( ) ;
99
133
}
100
134
101
- protected abstract void OnScrollViewDraw ( ) ;
135
+ protected virtual void OnScrollViewDraw ( )
136
+ {
137
+ // if (m_SampleTime <= DateTime.MinValue)
138
+ // {
139
+ // GUILayout.Label(Utility.Text.Format("<b>Please take sample for {0} first.</b>", typeName));
140
+ // }
141
+ // else
142
+ {
143
+ // if (m_DuplicateSimpleCount > 0)
144
+ // {
145
+ // GUILayout.Label(Utility.Text.Format("<b>{0} {1}s ({2}) obtained at {3}, while {4} {1}s ({5}) might be duplicated.</b>", m_Samples.Count.ToString(), typeName, GetByteLengthString(m_SampleSize), m_SampleTime.ToString("yyyy-MM-dd HH:mm:ss"), m_DuplicateSimpleCount.ToString(), GetByteLengthString(m_DuplicateSampleSize)));
146
+ // }
147
+ // else
148
+ // {
149
+ // GUILayout.Label(Utility.Text.Format("<b>{0} {1}s ({2}) obtained at {3}.</b>", m_Samples.Count.ToString(), typeName, GetByteLengthString(m_SampleSize), m_SampleTime.ToString("yyyy-MM-dd HH:mm:ss")));
150
+ // }
151
+ string typeName = typeof ( T ) . Name ;
152
+ GUILayout . Label ( string . Format ( "<b>{0} {1}s ({2}) obtained at {3}.</b>" , _samples . Count . ToString ( ) , typeName , _sampleSize . ToByteLengthString ( ) , _sampleDateTime . ToString ( "yyyy-MM-dd HH:mm:ss" ) ) ) ;
153
+ if ( _samples . Count > 0 )
154
+ {
155
+ GUILayout . BeginHorizontal ( ) ;
156
+ {
157
+ GUILayout . Label ( string . Format ( "<b>{0} Name</b>" , typeName ) ) ;
158
+ GUILayout . Label ( "<b>Type</b>" , GUILayout . Width ( 240f ) ) ;
159
+ GUILayout . Label ( "<b>Size</b>" , GUILayout . Width ( 80f ) ) ;
160
+ }
161
+ GUILayout . EndHorizontal ( ) ;
162
+ }
102
163
103
- protected abstract void TakeSample ( ) ;
164
+ for ( int i = 0 ; i < _samples . Count ; i ++ )
165
+ {
166
+ GUILayout . BeginHorizontal ( ) ;
167
+ {
168
+ GUILayout . Label ( _samples [ i ] . Highlight ? string . Format ( "<color=yellow>{0}</color>" , _samples [ i ] . Name ) : _samples [ i ] . Name ) ;
169
+ GUILayout . Label ( _samples [ i ] . Highlight ? string . Format ( "<color=yellow>{0}</color>" , _samples [ i ] . TypeName ) : _samples [ i ] . TypeName , GUILayout . Width ( 240f ) ) ;
170
+ GUILayout . Label ( _samples [ i ] . Highlight ? string . Format ( "<color=yellow>{0}</color>" , _samples [ i ] . Size . ToByteLengthString ( ) ) : _samples [ i ] . Size . ToByteLengthString ( ) , GUILayout . Width ( 80f ) ) ;
171
+ }
172
+ GUILayout . EndHorizontal ( ) ;
173
+ }
174
+ }
175
+ }
104
176
105
- }
106
- //概要内存分析
107
- internal class SummaryMemoryProfiler : MemoryProfilerBase
108
- {
109
- protected override void OnScrollViewDraw ( )
177
+ protected virtual void TakeSample ( )
110
178
{
179
+ ReleaseSamples ( ) ;
180
+
181
+ _sampleSize = 0 ;
182
+ _sampleDateTime = DateTime . Now ;
183
+ //整理所有的数据
184
+ T [ ] samples = Resources . FindObjectsOfTypeAll < T > ( ) ;
185
+ for ( int i = 0 ; i < samples . Length ; i ++ )
186
+ {
187
+ long sampleSize = Profiler . GetRuntimeMemorySizeLong ( samples [ i ] ) ;
188
+ var sample = ProfilerSamplePool . Get ( samples [ i ] . name , samples [ i ] . GetType ( ) . Name , sampleSize ) ;
189
+ _samples . Add ( sample ) ;
190
+
191
+ _sampleSize += sampleSize ;
192
+ }
193
+ //sort
194
+ _samples = _samples . OrderByDescending ( x => x . Size ) . ToList ( ) ;
111
195
}
112
196
113
- protected override void TakeSample ( )
197
+ //释放所有的数据
198
+ protected void ReleaseSamples ( )
114
199
{
200
+ foreach ( var item in _samples )
201
+ {
202
+ ProfilerSamplePool . Release ( item ) ;
203
+ }
204
+ _samples . Clear ( ) ;
115
205
}
116
206
}
117
- //所有的内存分析
118
- internal class AllMemoryProfiler : MemoryProfilerBase
207
+ //概要内存分析
208
+ internal class SummaryMemoryProfiler : MemoryProfilerBase < UnityEngine . Object >
119
209
{
120
- protected override void OnScrollViewDraw ( )
121
- {
122
- }
123
210
124
211
protected override void TakeSample ( )
125
212
{
213
+ base . TakeSample ( ) ;
214
+ Dictionary < string , ProfilerSample > newSamples = new Dictionary < string , ProfilerSample > ( ) ;
215
+ ProfilerSample tempSample ;
216
+ for ( int i = 0 ; i < _samples . Count ; i ++ )
217
+ {
218
+ tempSample = _samples [ i ] ;
219
+ ProfilerSample sample ;
220
+ if ( ! newSamples . TryGetValue ( tempSample . TypeName , out sample ) )
221
+ {
222
+ sample = ProfilerSamplePool . Get ( tempSample . TypeName , tempSample . TypeName , tempSample . Size , tempSample . Highlight ) ;
223
+ newSamples . Add ( tempSample . TypeName , sample ) ;
224
+ }
225
+ else
226
+ {
227
+ sample . Size += tempSample . Size ;
228
+ }
229
+ ProfilerSamplePool . Release ( tempSample ) ;
230
+ tempSample = null ;
231
+ }
232
+ _samples . Clear ( ) ;
233
+ foreach ( var item in newSamples . Values )
234
+ {
235
+ _samples . Add ( item ) ;
236
+ }
237
+ newSamples . Clear ( ) ;
126
238
}
127
239
}
128
- //Texture内存分析
129
- internal class TextureMemoryProfiler : MemoryProfilerBase
240
+ //所有的内存分析
241
+ internal class AllMemoryProfiler : MemoryProfilerBase < UnityEngine . Object >
130
242
{
131
- protected override void OnScrollViewDraw ( )
132
- {
133
- }
134
-
135
243
protected override void TakeSample ( )
136
244
{
245
+ base . TakeSample ( ) ;
246
+ for ( int i = 0 ; i < _samples . Count ; i ++ )
247
+ {
248
+ if ( _samples [ i ] . Size < 1024 )
249
+ {
250
+ for ( int j = i ; j < _samples . Count ; j ++ )
251
+ {
252
+ ProfilerSamplePool . Release ( _samples [ j ] ) ;
253
+ }
254
+ _samples . RemoveRange ( i , _samples . Count - i ) ;
255
+ break ;
256
+ }
257
+ }
137
258
}
138
259
}
260
+ //Texture内存分析
261
+ internal class TextureMemoryProfiler : MemoryProfilerBase < Texture >
262
+ {
263
+ }
139
264
//Mesh内存分析
140
- internal class MeshMemoryProfiler : MemoryProfilerBase
265
+ internal class MeshMemoryProfiler : MemoryProfilerBase < Mesh >
141
266
{
142
- protected override void OnScrollViewDraw ( )
143
- {
144
- }
145
267
146
- protected override void TakeSample ( )
147
- {
148
- }
149
268
}
150
269
//Material内存分析
151
- internal class MaterialMemoryProfiler : MemoryProfilerBase
270
+ internal class MaterialMemoryProfiler : MemoryProfilerBase < Material >
152
271
{
153
- protected override void OnScrollViewDraw ( )
154
- {
155
- }
156
272
157
- protected override void TakeSample ( )
158
- {
159
- }
160
273
}
161
274
//Shader内存分析
162
- internal class ShaderMemoryProfiler : MemoryProfilerBase
275
+ internal class ShaderMemoryProfiler : MemoryProfilerBase < Shader >
163
276
{
164
- protected override void OnScrollViewDraw ( )
165
- {
166
- }
167
277
168
- protected override void TakeSample ( )
169
- {
170
- }
171
278
}
172
279
//AnimationClip内存分析
173
- internal class AnimationClipMemoryProfiler : MemoryProfilerBase
280
+ internal class AnimationClipMemoryProfiler : MemoryProfilerBase < AnimationClip >
174
281
{
175
- protected override void OnScrollViewDraw ( )
176
- {
177
- }
178
282
179
- protected override void TakeSample ( )
180
- {
181
- }
182
283
}
183
284
//AudioClip内存分析
184
- internal class AudioClipMemoryProfiler : MemoryProfilerBase
285
+ internal class AudioClipMemoryProfiler : MemoryProfilerBase < AudioClip >
185
286
{
186
- protected override void OnScrollViewDraw ( )
187
- {
188
- }
189
287
190
- protected override void TakeSample ( )
191
- {
192
- }
193
288
}
194
289
// Font内存分析
195
- internal class FontMemoryProfiler : MemoryProfilerBase
290
+ internal class FontMemoryProfiler : MemoryProfilerBase < Font >
196
291
{
197
- protected override void OnScrollViewDraw ( )
198
- {
199
- }
200
292
201
- protected override void TakeSample ( )
202
- {
203
- }
204
293
}
205
294
//TextAsset内存分析
206
- internal class TextAssetMemoryProfiler : MemoryProfilerBase
295
+ internal class TextAssetMemoryProfiler : MemoryProfilerBase < TextAsset >
207
296
{
208
- protected override void OnScrollViewDraw ( )
209
- {
210
- }
211
297
212
- protected override void TakeSample ( )
298
+ }
299
+ //ScriptableObject内存分析
300
+ internal class ScriptableObjectMemoryProfiler : MemoryProfilerBase < ScriptableObject >
301
+ {
302
+
303
+ }
304
+
305
+ // profiler
306
+ internal class ProfilerSample
307
+ {
308
+ public string Name { get ; private set ; }
309
+ public string TypeName { get ; private set ; }
310
+ public long Size { get ; set ; }
311
+ public bool Highlight { get ; set ; }
312
+
313
+ public ProfilerSample Set ( string name , string type , long size , bool highlight )
213
314
{
315
+ Name = name ;
316
+ TypeName = type ;
317
+ Size = size ;
318
+ Highlight = highlight ;
319
+ return this ;
214
320
}
215
321
}
216
- //ScriptableObject内存分析
217
- internal class ScriptableObjectMemoryProfiler : MemoryProfilerBase
322
+ // profiler pool
323
+ internal class ProfilerSamplePool
218
324
{
219
- protected override void OnScrollViewDraw ( )
325
+ static ObjectPool < ProfilerSample > _pool = new ObjectPool < ProfilerSample > ( null , null ) ;
326
+
327
+ public static ProfilerSample Get ( string name , string type , long size , bool highlight = false )
220
328
{
329
+ return _pool . Get ( ) . Set ( name , type , size , highlight ) ;
221
330
}
222
331
223
- protected override void TakeSample ( )
332
+ public static void Release ( ProfilerSample sample )
224
333
{
334
+ _pool . Release ( sample ) ;
225
335
}
336
+
226
337
}
227
338
228
339
}
0 commit comments