因为最近在使用E神的GF框架,他自带的调试器感觉非常实用,但因为不能脱离框架单独使用,所以就参照着他的界面做了一个,功能几乎差不多(代码基本复制的),附上E神博客的链接:http://gameframework.cn/archives/279
1 using UnityEngine; 2 using System.Collections.Generic; 3 using UnityEngine.Profiling; 4 using UnityEngine.SceneManagement; 5 6 public class Debugger : MonoBehaviour 7 { 8 /// <summary> 9 /// 是否允许调试 10 /// </summary> 11 public bool AllowDebugging = true; 12 13 private DebugType _debugType = DebugType.Console; 14 private ProfilerType _profilerType = ProfilerType.Summary; 15 private ProfilerMenoryType _profilerMenoryType = ProfilerMenoryType.ALL; 16 private Information _InformationType = Information.System; 17 private List<LogData> _logInformations = new List<LogData>(); 18 private int _currentLogIndex = -1; 19 private int _infoLogCount = 0; 20 private int _warningLogCount = 0; 21 private int _errorLogCount = 0; 22 private int _fatalLogCount = 0; 23 private bool _showInfoLog = true; 24 private bool _showWarningLog = true; 25 private bool _showErrorLog = true; 26 private bool _showFatalLog = true; 27 private Vector2 _scrollLogView = Vector2.zero; 28 private Vector2 _scrollCurrentLogView = Vector2.zero; 29 private Vector2 _scrollSystemView = Vector2.zero; 30 private bool _expansion = false; 31 private Rect _windowRect = new Rect(0, 0, 100, 60); 32 private string[] ProfilerMenorys = { "ALL", "Texture", "Mesh", "Material", "AnimitionClip", "AudioClip", "Font", "Componet" }; 33 private string[] debugTypes = { "Console", "Information", "Profiler", "Close" }; 34 private string[] Informations = { "System", "Environmen", "screen", "Graphic", "Input", "Other" }; 35 36 37 private int _fps = 0; 38 private Color _fpsColor = Color.white; 39 private int _frameNumber = 0; 40 private float _lastShowFPSTime = 0f; 41 private RuntimeMemoryInformationWindow<Object> m_RuntimeMemoryAllInformationWindow = new RuntimeMemoryInformationWindow<Object>(); 42 private RuntimeMemoryInformationWindow<Texture> m_RuntimeMemoryTextureInformationWindow = new RuntimeMemoryInformationWindow<Texture>(); 43 private ProfilerInformationWindow m_ProfilerInformationWindow = new ProfilerInformationWindow(); 44 private RuntimeMemoryInformationWindow<Mesh> m_RuntimeMemoryMeshInformationWindow = new RuntimeMemoryInformationWindow<Mesh>(); 45 private RuntimeMemoryInformationWindow<Material> m_RuntimeMemoryMaterialInformationWindow = new RuntimeMemoryInformationWindow<Material>(); 46 private RuntimeMemoryInformationWindow<AnimationClip> m_RuntimeMemoryAnimationClipInformationWindow = new RuntimeMemoryInformationWindow<AnimationClip>(); 47 private RuntimeMemoryInformationWindow<AudioClip> m_RuntimeMemoryAudioClipInformationWindow = new RuntimeMemoryInformationWindow<AudioClip>(); 48 private RuntimeMemoryInformationWindow<Font> m_RuntimeMemoryFontInformationWindow = new RuntimeMemoryInformationWindow<Font>(); 49 private RuntimeMemoryInformationWindow<GameObject> m_RuntimeMemoryGameObjectInformationWindow = new RuntimeMemoryInformationWindow<GameObject>(); 50 private RuntimeMemoryInformationWindow<Component> m_RuntimeMemoryComponentInformationWindow = new RuntimeMemoryInformationWindow<Component>(); 51 private SystemInformationWindow m_SystemInformationWindow = new SystemInformationWindow(); 52 private EnvironmentInformationWindow m_EnvironmentInformationWindow = new EnvironmentInformationWindow(); 53 private ScreenInformationWindow m_ScreenInformationWindow = new ScreenInformationWindow(); 54 private GraphicsInformationWindow m_GraphicsInformationWindow = new GraphicsInformationWindow(); 55 private InputSummaryInformationWindow m_InputSummaryInformationWindow = new InputSummaryInformationWindow(); 56 private QualityInformationWindow m_QualityInformationWindow = new QualityInformationWindow(); 57 58 private void Awake() 59 { 60 if (AllowDebugging) 61 { 62 Application.logMessageReceived += LogHandler; 63 } 64 } 65 private void Update() 66 { 67 if (AllowDebugging) 68 { 69 _frameNumber += 1; 70 float time = Time.realtimeSinceStartup - _lastShowFPSTime; 71 if (time >= 1) 72 { 73 _fps = (int)(_frameNumber / time); 74 _frameNumber = 0; 75 _lastShowFPSTime = Time.realtimeSinceStartup; 76 } 77 } 78 } 79 private void OnDestory() 80 { 81 if (AllowDebugging) 82 { 83 Application.logMessageReceived -= LogHandler; 84 } 85 } 86 private void LogHandler(string condition, string stackTrace, LogType type) 87 { 88 LogData log = new LogData(); 89 log.time = System.DateTime.Now.ToString("HH:mm:ss"); 90 log.message = condition; 91 log.stackTrace = stackTrace; 92 93 if (type == LogType.Assert) 94 { 95 log.type = "Fatal"; 96 _fatalLogCount += 1; 97 } 98 else if (type == LogType.Exception || type == LogType.Error) 99 { 100 log.type = "Error"; 101 _errorLogCount += 1; 102 } 103 else if (type == LogType.Warning) 104 { 105 log.type = "Warning"; 106 _warningLogCount += 1; 107 } 108 else if (type == LogType.Log) 109 { 110 log.type = "Info"; 111 _infoLogCount += 1; 112 } 113 114 _logInformations.Add(log); 115 116 if (_warningLogCount > 0) 117 { 118 _fpsColor = Color.yellow; 119 } 120 if (_errorLogCount > 0) 121 { 122 _fpsColor = Color.red; 123 } 124 } 125 126 private void OnGUI() 127 { 128 if (AllowDebugging) 129 { 130 if (_expansion) 131 { 132 _windowRect = GUI.Window(0, _windowRect, ExpansionGUIWindow, "DEBUGGER"); 133 } 134 else 135 { 136 _windowRect = GUI.Window(0, _windowRect, ShrinkGUIWindow, "DEBUGGER"); 137 } 138 } 139 } 140 private void ExpansionGUIWindow(int windowId) 141 { 142 GUI.DragWindow(new Rect(0, 0, 10000, 20)); 143 144 #region title 145 GUILayout.BeginHorizontal(); 146 GUI.contentColor = _fpsColor; 147 for (int i = 0; i < debugTypes.Length; i++) 148 { 149 string tempStr = System.Enum.GetName(typeof(DebugType), _debugType); 150 GUI.contentColor = (tempStr == debugTypes[i] ? Color.white : Color.gray); 151 152 if (GUILayout.Button(debugTypes[i], GUILayout.Height(30))) 153 { 154 _debugType = (DebugType)System.Enum.Parse(typeof(DebugType), debugTypes[i]); 155 } 156 157 } 158 159 GUILayout.EndHorizontal(); 160 #endregion 161 switch (_debugType) 162 { 163 #region Console 164 case DebugType.Console: 165 GUILayout.BeginHorizontal(); 166 if (GUILayout.Button("Clear")) 167 { 168 _logInformations.Clear(); 169 _fatalLogCount = 0; 170 _warningLogCount = 0; 171 _errorLogCount = 0; 172 _infoLogCount = 0; 173 _currentLogIndex = -1; 174 _fpsColor = Color.white; 175 } 176 GUI.contentColor = (_showInfoLog ? Color.white : Color.gray); 177 _showInfoLog = GUILayout.Toggle(_showInfoLog, "Info [" + _infoLogCount + "]"); 178 GUI.contentColor = (_showWarningLog ? Color.white : Color.gray); 179 _showWarningLog = GUILayout.Toggle(_showWarningLog, "Warning [" + _warningLogCount + "]"); 180 GUI.contentColor = (_showErrorLog ? Color.white : Color.gray); 181 _showErrorLog = GUILayout.Toggle(_showErrorLog, "Error [" + _errorLogCount + "]"); 182 GUI.contentColor = (_showFatalLog ? Color.white : Color.gray); 183 _showFatalLog = GUILayout.Toggle(_showFatalLog, "Fatal [" + _fatalLogCount + "]"); 184 GUI.contentColor = Color.white; 185 GUILayout.EndHorizontal(); 186 187 _scrollLogView = GUILayout.BeginScrollView(_scrollLogView, "Box", GUILayout.Height(165)); 188 for (int i = 0; i < _logInformations.Count; i++) 189 { 190 bool show = false; 191 Color color = Color.white; 192 switch (_logInformations[i].type) 193 { 194 case "Fatal": 195 show = _showFatalLog; 196 color = Color.red; 197 break; 198 case "Error": 199 show = _showErrorLog; 200 color = Color.red; 201 break; 202 case "Info": 203 show = _showInfoLog; 204 color = Color.white; 205 break; 206 case "Warning": 207 show = _showWarningLog; 208 color = Color.yellow; 209 break; 210 default: 211 break; 212 } 213 214 if (show) 215 { 216 GUILayout.BeginHorizontal(); 217 if (GUILayout.Toggle(_currentLogIndex == i, "")) 218 { 219 _currentLogIndex = i; 220 } 221 GUI.contentColor = color; 222 GUILayout.Label("[" + _logInformations[i].type + "] "); 223 GUILayout.Label("[" + _logInformations[i].time + "] "); 224 GUILayout.Label(_logInformations[i].message); 225 GUILayout.FlexibleSpace(); 226 GUI.contentColor = Color.white; 227 GUILayout.EndHorizontal(); 228 } 229 } 230 GUILayout.EndScrollView(); 231 232 _scrollCurrentLogView = GUILayout.BeginScrollView(_scrollCurrentLogView, "Box", GUILayout.Height(100)); 233 if (_currentLogIndex != -1) 234 { 235 GUILayout.Label(_logInformations[_currentLogIndex].message + "\r\n\r\n" + _logInformations[_currentLogIndex].stackTrace); 236 } 237 GUILayout.EndScrollView(); 238 239 #endregion 240 break; 241 #region Information 242 case DebugType.Information: 243 GUILayout.BeginHorizontal(); 244 for (int i = 0; i < Informations.Length; i++) 245 { 246 string tempStr = System.Enum.GetName(typeof(Information), _InformationType); 247 GUI.contentColor = (tempStr == Informations[i] ? Color.white : Color.gray); 248 249 if (GUILayout.Button(Informations[i], GUILayout.Height(30))) 250 { 251 _InformationType = (Information)System.Enum.Parse(typeof(Information), Informations[i]); 252 } 253 254 } 255 GUILayout.EndHorizontal(); 256 GUILayout.BeginHorizontal(); 257 switch (_InformationType) 258 { 259 case Information.System: 260 m_SystemInformationWindow.OnDrawScrollableWindow(); 261 break; 262 case Information.Environmen: 263 m_EnvironmentInformationWindow.OnDrawScrollableWindow(); 264 break; 265 case Information.screen: 266 m_ScreenInformationWindow.OnDrawScrollableWindow(); 267 break; 268 case Information.Graphic: 269 m_GraphicsInformationWindow.OnDrawScrollableWindow(); 270 break; 271 case Information.Input: 272 m_InputSummaryInformationWindow.OnDrawScrollableWindow(); 273 break; 274 case Information.Other: 275 m_QualityInformationWindow.OnDrawScrollableWindow(); 276 break; 277 default: 278 break; 279 } 280 GUILayout.EndHorizontal(); 281 #endregion 282 break; 283 #region Profiler 284 case DebugType.Profiler: 285 GUILayout.BeginHorizontal(); 286 GUI.contentColor = (_profilerType == ProfilerType.Summary ? Color.white : Color.gray); 287 if (GUILayout.Button("总体", GUILayout.Height(30))) 288 { 289 _profilerType = ProfilerType.Summary; 290 } 291 GUI.contentColor = (_profilerType == ProfilerType.Memory ? Color.white : Color.gray); 292 if (GUILayout.Button("内存", GUILayout.Height(30))) 293 { 294 _profilerType = ProfilerType.Memory; 295 } 296 GUILayout.EndHorizontal(); 297 if (_profilerType == ProfilerType.Summary) 298 { 299 GUI.contentColor = Color.white; 300 m_ProfilerInformationWindow.OnDrawScrollableWindow(); 301 302 } 303 else if (_profilerType == ProfilerType.Memory) 304 { 305 GUILayout.BeginHorizontal(); 306 for (int i = 0; i < ProfilerMenorys.Length; i++) 307 { 308 string tempStr = System.Enum.GetName(typeof(ProfilerMenoryType), _profilerMenoryType); 309 GUI.contentColor = (tempStr == ProfilerMenorys[i] ? Color.white : Color.gray); 310 311 if (GUILayout.Button(ProfilerMenorys[i], GUILayout.Height(30))) 312 { 313 _profilerMenoryType = (ProfilerMenoryType)System.Enum.Parse(typeof(ProfilerMenoryType), ProfilerMenorys[i]); 314 } 315 316 } 317 GUILayout.EndHorizontal(); 318 GUILayout.BeginHorizontal(); 319 switch (_profilerMenoryType) 320 { 321 case ProfilerMenoryType.ALL: 322 GUI.contentColor = Color.white; 323 m_RuntimeMemoryAllInformationWindow.OnDrawScrollableWindow(); 324 break; 325 case ProfilerMenoryType.Texture: 326 GUI.contentColor = Color.white; 327 m_RuntimeMemoryTextureInformationWindow.OnDrawScrollableWindow(); 328 break; 329 case ProfilerMenoryType.Mesh: 330 GUI.contentColor = Color.white; 331 m_RuntimeMemoryMeshInformationWindow.OnDrawScrollableWindow(); 332 break; 333 case ProfilerMenoryType.Material: 334 GUI.contentColor = Color.white; 335 m_RuntimeMemoryMaterialInformationWindow.OnDrawScrollableWindow(); 336 break; 337 case ProfilerMenoryType.AnimitionClip: 338 GUI.contentColor = Color.white; 339 m_RuntimeMemoryAnimationClipInformationWindow.OnDrawScrollableWindow(); 340 break; 341 case ProfilerMenoryType.AudioClip: 342 GUI.contentColor = Color.white; 343 m_RuntimeMemoryAudioClipInformationWindow.OnDrawScrollableWindow(); 344 break; 345 case ProfilerMenoryType.Font: 346 GUI.contentColor = Color.white; 347 m_RuntimeMemoryFontInformationWindow.OnDrawScrollableWindow(); 348 break; 349 case ProfilerMenoryType.Componet: 350 GUI.contentColor = Color.white; 351 m_RuntimeMemoryComponentInformationWindow.OnDrawScrollableWindow(); 352 break; 353 default: 354 break; 355 } 356 GUILayout.EndHorizontal(); 357 } 358 359 break; 360 #endregion 361 case DebugType.Close: 362 _expansion = false; 363 _windowRect.width = 100; 364 _windowRect.height = 60; 365 break; 366 default: 367 break; 368 } 369 370 371 } 372 373 374 private void ShrinkGUIWindow(int windowId) 375 { 376 GUI.DragWindow(new Rect(0, 0, 10000, 20)); 377 378 GUI.contentColor = _fpsColor; 379 if (GUILayout.Button("FPS:" + _fps, GUILayout.Width(80), GUILayout.Height(30))) 380 { 381 _expansion = true; 382 _debugType = DebugType.Console; 383 _windowRect.width = 1200; 384 _windowRect.height = 800; 385 } 386 GUI.contentColor = Color.white; 387 } 388 389 390 391 } 392 public struct LogData 393 { 394 public string time; 395 public string type; 396 public string message; 397 public string stackTrace; 398 } 399 public enum DebugType 400 { 401 Console, 402 Information, 403 Profiler, 404 Close, 405 } 406 public enum ProfilerMenoryType 407 { 408 ALL = 0, 409 Texture = 1, 410 Mesh = 2, 411 Material = 3, 412 AnimitionClip = 4, 413 AudioClip = 5, 414 Font = 6, 415 Componet = 7 416 } 417 418 public enum ProfilerType 419 { 420 Summary, 421 Memory, 422 423 } 424 425 public enum Information 426 { 427 System, Environmen, screen, Graphic, Input, Other, 428 } 429 430 public class SystemInformationWindow 431 { 432 public void OnDrawScrollableWindow() 433 { 434 GUILayout.Label("<b>System Information</b>"); 435 GUILayout.BeginVertical("box"); 436 { 437 DrawItem("Device Unique ID:", SystemInfo.deviceUniqueIdentifier); 438 DrawItem("Device Name:", SystemInfo.deviceName); 439 DrawItem("Device Type:", SystemInfo.deviceType.ToString()); 440 DrawItem("Device Model:", SystemInfo.deviceModel); 441 DrawItem("Processor Type:", SystemInfo.processorType); 442 DrawItem("Processor Count:", SystemInfo.processorCount.ToString()); 443 DrawItem("Processor Frequency:", string.Format("{0} MHz", SystemInfo.processorFrequency.ToString())); 444 DrawItem("Memory Size:", string.Format("{0} MB", SystemInfo.systemMemorySize.ToString())); 445 #if UNITY_5_5_OR_NEWER 446 DrawItem("Operating System Family:", SystemInfo.operatingSystemFamily.ToString()); 447 #endif 448 DrawItem("Operating System:", SystemInfo.operatingSystem); 449 #if UNITY_5_6_OR_NEWER 450 DrawItem("Battery Status:", SystemInfo.batteryStatus.ToString()); 451 DrawItem("Battery Level:", GetBatteryLevelString(SystemInfo.batteryLevel)); 452 #endif 453 #if UNITY_5_4_OR_NEWER 454 DrawItem("Supports Audio:", SystemInfo.supportsAudio.ToString()); 455 #endif 456 DrawItem("Supports Location Service:", SystemInfo.supportsLocationService.ToString()); 457 DrawItem("Supports Accelerometer:", SystemInfo.supportsAccelerometer.ToString()); 458 DrawItem("Supports Gyroscope:", SystemInfo.supportsGyroscope.ToString()); 459 DrawItem("Supports Vibration:", SystemInfo.supportsVibration.ToString()); 460 DrawItem("Genuine:", Application.genuine.ToString()); 461 DrawItem("Genuine Check Available:", Application.genuineCheckAvailable.ToString()); 462 } 463 GUILayout.EndVertical(); 464 } 465 466 private string GetBatteryLevelString(float batteryLevel) 467 { 468 if (batteryLevel < 0f) 469 { 470 return "Unavailable"; 471 } 472 473 return batteryLevel.ToString("P0"); 474 } 475 private const float TitleWidth = 240f; 476 private void DrawItem(string title, string content) 477 { 478 GUILayout.BeginHorizontal(); 479 { 480 GUILayout.Label(title, GUILayout.Width(TitleWidth)); 481 GUILayout.Label(content); 482 } 483 GUILayout.EndHorizontal(); 484 } 485 } 486 public class RuntimeMemoryInformationWindow<T> where T : UnityEngine.Object 487 { 488 private const int ShowSampleCount = 300; 489 490 private System.DateTime m_SampleTime = System.DateTime.MinValue; 491 private long m_SampleSize = 0; 492 private long m_DuplicateSampleSize = 0; 493 private int m_DuplicateSimpleCount = 0; 494 private List<Sample> m_Samples = new List<Sample>(); 495 496 public void OnDrawScrollableWindow() 497 { 498 string typeName = typeof(T).Name; 499 GUILayout.Label(string.Format("<b>{0} Runtime Memory Information</b>", typeName)); 500 GUILayout.BeginVertical("box"); 501 { 502 if (GUILayout.Button(string.Format("Take Sample for {0}", typeName), GUILayout.Height(30f))) 503 { 504 TakeSample(); 505 } 506 507 if (m_SampleTime <= System.DateTime.MinValue) 508 { 509 GUILayout.Label(string.Format("<b>Please take sample for {0} first.</b>", typeName)); 510 } 511 else 512 { 513 if (m_DuplicateSimpleCount > 0) 514 { 515 GUILayout.Label(string.Format("<b>{0} {1}s ({2}) obtained at {3}, while {4} {1}s ({5}) might be duplicated.</b>", m_Samples.Count.ToString(), typeName, GetSizeString(m_SampleSize), m_SampleTime.ToString("yyyy-MM-dd HH:mm:ss"), m_DuplicateSimpleCount.ToString(), GetSizeString(m_DuplicateSampleSize))); 516 } 517 else 518 { 519 GUILayout.Label(string.Format("<b>{0} {1}s ({2}) obtained at {3}.</b>", m_Samples.Count.ToString(), typeName, GetSizeString(m_SampleSize), m_SampleTime.ToString("yyyy-MM-dd HH:mm:ss"))); 520 } 521 522 if (m_Samples.Count > 0) 523 { 524 GUILayout.BeginHorizontal(); 525 { 526 GUILayout.Label(string.Format("<b>{0} Name</b>", typeName)); 527 GUILayout.Label("<b>Type</b>", GUILayout.Width(240f)); 528 GUILayout.Label("<b>Size</b>", GUILayout.Width(80f)); 529 } 530 GUILayout.EndHorizontal(); 531 } 532 533 int count = 0; 534 for (int i = 0; i < m_Samples.Count; i++) 535 { 536 GUILayout.BeginHorizontal(); 537 { 538 GUILayout.Label(m_Samples[i].Highlight ? string.Format("<color=yellow>{0}</color>", m_Samples[i].Name) : m_Samples[i].Name); 539 GUILayout.Label(m_Samples[i].Highlight ? string.Format("<color=yellow>{0}</color>", m_Samples[i].Type) : m_Samples[i].Type, GUILayout.Width(240f)); 540 GUILayout.Label(m_Samples[i].Highlight ? string.Format("<color=yellow>{0}</color>", GetSizeString(m_Samples[i].Size)) : GetSizeString(m_Samples[i].Size), GUILayout.Width(80f)); 541 } 542 GUILayout.EndHorizontal(); 543 544 count++; 545 if (count >= ShowSampleCount) 546 { 547 break; 548 } 549 } 550 } 551 } 552 GUILayout.EndVertical(); 553 } 554 555 private void TakeSample() 556 { 557 m_SampleTime = System.DateTime.Now; 558 m_SampleSize = 0L; 559 m_DuplicateSampleSize = 0L; 560 m_DuplicateSimpleCount = 0; 561 m_Samples.Clear(); 562 563 T[] samples = Resources.FindObjectsOfTypeAll<T>(); 564 for (int i = 0; i < samples.Length; i++) 565 { 566 long sampleSize = 0L; 567 #if UNITY_5_6_OR_NEWER 568 sampleSize = Profiler.GetRuntimeMemorySizeLong(samples[i]); 569 #else 570 sampleSize = Profiler.GetRuntimeMemorySize(samples[i]); 571 #endif 572 m_SampleSize += sampleSize; 573 m_Samples.Add(new Sample(samples[i].name, samples[i].GetType().Name, sampleSize)); 574 } 575 576 m_Samples.Sort(SampleComparer); 577 578 for (int i = 1; i < m_Samples.Count; i++) 579 { 580 if (m_Samples[i].Name == m_Samples[i - 1].Name && m_Samples[i].Type == m_Samples[i - 1].Type && m_Samples[i].Size == m_Samples[i - 1].Size) 581 { 582 m_Samples[i].Highlight = true; 583 m_DuplicateSampleSize += m_Samples[i].Size; 584 m_DuplicateSimpleCount++; 585 } 586 } 587 } 588 589 private string GetSizeString(long size) 590 { 591 if (size < 1024L) 592 { 593 return string.Format("{0} Bytes", size.ToString()); 594 } 595 596 if (size < 1024L * 1024L) 597 { 598 return string.Format("{0} KB", (size / 1024f).ToString("F2")); 599 } 600 601 if (size < 1024L * 1024L * 1024L) 602 { 603 return string.Format("{0} MB", (size / 1024f / 1024f).ToString("F2")); 604 } 605 606 if (size < 1024L * 1024L * 1024L * 1024L) 607 { 608 return string.Format("{0} GB", (size / 1024f / 1024f / 1024f).ToString("F2")); 609 } 610 611 return string.Format("{0} TB", (size / 1024f / 1024f / 1024f / 1024f).ToString("F2")); 612 } 613 614 private int SampleComparer(Sample a, Sample b) 615 { 616 int result = b.Size.CompareTo(a.Size); 617 if (result != 0) 618 { 619 return result; 620 } 621 622 result = a.Type.CompareTo(b.Type); 623 if (result != 0) 624 { 625 return result; 626 } 627 628 return a.Name.CompareTo(b.Name); 629 } 630 } 631 public class ProfilerInformationWindow 632 { 633 private const int MBSize = 1024 * 1024; 634 private const float TitleWidth = 240f; 635 private void DrawItem(string title, string content) 636 { 637 GUILayout.BeginHorizontal(); 638 { 639 GUILayout.Label(title, GUILayout.Width(TitleWidth)); 640 GUILayout.Label(content); 641 } 642 GUILayout.EndHorizontal(); 643 } 644 public void OnDrawScrollableWindow() 645 { 646 GUILayout.Label("<b>Profiler Information</b>"); 647 GUILayout.BeginVertical("box"); 648 { 649 DrawItem("Supported:", Profiler.supported.ToString()); 650 DrawItem("Enabled:", Profiler.enabled.ToString()); 651 DrawItem("Enable Binary Log:", Profiler.enableBinaryLog ? string.Format("True, {0}", Profiler.logFile) : "False"); 652 #if UNITY_5_3 || UNITY_5_4 653 DrawItem("Max Samples Number Per Frame:", Profiler.maxNumberOfSamplesPerFrame.ToString()); 654 #endif 655 #if UNITY_5_6_OR_NEWER 656 DrawItem("Mono Used Size:", string.Format("{0} MB", (Profiler.GetMonoUsedSizeLong() / (float)MBSize).ToString("F3"))); 657 DrawItem("Mono Heap Size:", string.Format("{0} MB", (Profiler.GetMonoHeapSizeLong() / (float)MBSize).ToString("F3"))); 658 DrawItem("Used Heap Size:", string.Format("{0} MB", (Profiler.usedHeapSizeLong / (float)MBSize).ToString("F3"))); 659 DrawItem("Total Allocated Memory:", string.Format("{0} MB", (Profiler.GetTotalAllocatedMemoryLong() / (float)MBSize).ToString("F3"))); 660 DrawItem("Total Reserved Memory:", string.Format("{0} MB", (Profiler.GetTotalReservedMemoryLong() / (float)MBSize).ToString("F3"))); 661 DrawItem("Total Unused Reserved Memory:", string.Format("{0} MB", (Profiler.GetTotalUnusedReservedMemoryLong() / (float)MBSize).ToString("F3"))); 662 #else 663 DrawItem("Mono Used Size:", string.Format("{0} MB", (Profiler.GetMonoUsedSize() / (float)MBSize).ToString("F3"))); 664 DrawItem("Mono Heap Size:", string.Format("{0} MB", (Profiler.GetMonoHeapSize() / (float)MBSize).ToString("F3"))); 665 DrawItem("Used Heap Size:", string.Format("{0} MB", (Profiler.usedHeapSize / (float)MBSize).ToString("F3"))); 666 DrawItem("Total Allocated Memory:", string.Format("{0} MB", (Profiler.GetTotalAllocatedMemory() / (float)MBSize).ToString("F3"))); 667 DrawItem("Total Reserved Memory:", string.Format("{0} MB", (Profiler.GetTotalReservedMemory() / (float)MBSize).ToString("F3"))); 668 DrawItem("Total Unused Reserved Memory:", string.Format("{0} MB", (Profiler.GetTotalUnusedReservedMemory() / (float)MBSize).ToString("F3"))); 669 #endif 670 #if UNITY_5_5_OR_NEWER 671 DrawItem("Temp Allocator Size:", string.Format("{0} MB", (Profiler.GetTempAllocatorSize() / (float)MBSize).ToString("F3"))); 672 #endif 673 } 674 GUILayout.EndVertical(); 675 } 676 } 677 public class Sample 678 { 679 private readonly string m_Name; 680 private readonly string m_Type; 681 private readonly long m_Size; 682 private bool m_Highlight; 683 684 public Sample(string name, string type, long size) 685 { 686 m_Name = name; 687 m_Type = type; 688 m_Size = size; 689 m_Highlight = false; 690 } 691 692 public string Name 693 { 694 get 695 { 696 return m_Name; 697 } 698 } 699 700 public string Type 701 { 702 get 703 { 704 return m_Type; 705 } 706 } 707 708 public long Size 709 { 710 get 711 { 712 return m_Size; 713 } 714 } 715 716 public bool Highlight 717 { 718 get 719 { 720 return m_Highlight; 721 } 722 set 723 { 724 m_Highlight = value; 725 } 726 } 727 } 728 729 public class EnvironmentInformationWindow : ScrollableDebuggerWindowBase 730 { 731 732 733 public override void OnDrawScrollableWindow() 734 { 735 GUI.contentColor = Color.white; 736 GUILayout.Label("<b>系统属性</b>"); 737 GUILayout.BeginVertical("box"); 738 { 739 DrawItem("操作系统:", SystemInfo.operatingSystem); 740 DrawItem("系统内存:", SystemInfo.systemMemorySize + "MB"); 741 DrawItem("处理器:", SystemInfo.processorType); 742 DrawItem("处理器数量:", SystemInfo.processorCount.ToString()); 743 DrawItem("显卡:", SystemInfo.graphicsDeviceName); 744 DrawItem("显卡类型:", SystemInfo.graphicsDeviceType.ToString()); 745 DrawItem("显存:", SystemInfo.graphicsMemorySize.ToString() + "MB"); 746 DrawItem("显卡标识:", SystemInfo.graphicsDeviceID.ToString()); 747 DrawItem("显卡供应商:", SystemInfo.graphicsDeviceVendor); 748 DrawItem("显卡供应商标识码:", SystemInfo.graphicsDeviceVendorID.ToString()); 749 DrawItem("设备模式:", SystemInfo.deviceModel); 750 DrawItem("设备名称:", SystemInfo.deviceName); 751 DrawItem("设备类型:", SystemInfo.deviceType.ToString()); 752 DrawItem("设备标识:", SystemInfo.deviceUniqueIdentifier); 753 DrawItem("项目名称:", Application.productName); 754 DrawItem("公司名称:", Application.companyName); 755 #if UNITY_5_6_OR_NEWER 756 DrawItem("项目ID:", Application.identifier); 757 #else 758 DrawItem("项目ID:", Application.bundleIdentifier); 759 #endif 760 DrawItem("项目版本:", Application.version); 761 DrawItem("Unity版本:", Application.unityVersion); 762 DrawItem("发布平台:", Application.platform.ToString()); 763 DrawItem("语言:", Application.systemLanguage.ToString()); 764 765 } 766 GUILayout.EndVertical(); 767 } 768 } 769 770 public class ScrollableDebuggerWindowBase 771 { 772 private const float TitleWidth = 240f; 773 public void DrawItem(string title, string content) 774 { 775 GUILayout.BeginHorizontal(); 776 { 777 GUILayout.Label(title, GUILayout.Width(TitleWidth)); 778 GUILayout.Label(content); 779 } 780 GUILayout.EndHorizontal(); 781 } 782 public virtual void OnDrawScrollableWindow() 783 { 784 785 } 786 } 787 public class ScreenInformationWindow : ScrollableDebuggerWindowBase 788 { 789 public override void OnDrawScrollableWindow() 790 { 791 GUI.contentColor = Color.white; 792 GUILayout.Label("<b>屏幕信息</b>"); 793 GUILayout.BeginVertical("box"); 794 { 795 DrawItem("分辨率", GetResolutionString(Screen.currentResolution)); 796 DrawItem("宽", string.Format("{0} px ", Screen.width.ToString())); 797 DrawItem("高", string.Format("{0} px ", Screen.height.ToString())); 798 DrawItem("屏幕 DPI", Screen.dpi.ToString("F2")); 799 DrawItem("屏幕方向", Screen.orientation.ToString()); 800 DrawItem("是否全屏", Screen.fullScreen.ToString()); 801 DrawItem("Sleep Timeout", GetSleepTimeoutDescription(Screen.sleepTimeout)); 802 DrawItem("Cursor Visible", Cursor.visible.ToString()); 803 DrawItem("Cursor Lock State", Cursor.lockState.ToString()); 804 DrawItem("Auto Landscape Left", Screen.autorotateToLandscapeLeft.ToString()); 805 DrawItem("Auto Landscape Right", Screen.autorotateToLandscapeRight.ToString()); 806 DrawItem("Auto Portrait", Screen.autorotateToPortrait.ToString()); 807 DrawItem("Auto Portrait Upside Down", Screen.autorotateToPortraitUpsideDown.ToString()); 808 DrawItem("支持分辨率", GetResolutionsString(Screen.resolutions)); 809 810 } 811 GUILayout.EndVertical(); 812 } 813 814 private string GetSleepTimeoutDescription(int sleepTimeout) 815 { 816 if (sleepTimeout == SleepTimeout.NeverSleep) 817 { 818 return "Never Sleep"; 819 } 820 821 if (sleepTimeout == SleepTimeout.SystemSetting) 822 { 823 return "System Setting"; 824 } 825 826 return sleepTimeout.ToString(); 827 } 828 829 private string GetResolutionString(Resolution resolution) 830 { 831 return string.Format("{0} x {1} @ {2}Hz", resolution.width.ToString(), resolution.height.ToString(), resolution.refreshRate.ToString()); 832 } 833 834 private string GetResolutionsString(Resolution[] resolutions) 835 { 836 string[] resolutionStrings = new string[resolutions.Length]; 837 for (int i = 0; i < resolutions.Length; i++) 838 { 839 resolutionStrings[i] = GetResolutionString(resolutions[i]); 840 } 841 842 return string.Join("; ", resolutionStrings); 843 } 844 } 845 846 public class GraphicsInformationWindow : ScrollableDebuggerWindowBase 847 { 848 public override void OnDrawScrollableWindow() 849 { 850 GUI.contentColor = Color.white; 851 GUILayout.Label("<b>设备信息</b>"); 852 GUILayout.BeginVertical("box"); 853 { 854 DrawItem("Device ID:", SystemInfo.graphicsDeviceID.ToString()); 855 DrawItem("Device Name:", SystemInfo.graphicsDeviceName); 856 DrawItem("Device Vendor ID:", SystemInfo.graphicsDeviceVendorID.ToString()); 857 DrawItem("Device Vendor:", SystemInfo.graphicsDeviceVendor); 858 DrawItem("Device Type:", SystemInfo.graphicsDeviceType.ToString()); 859 DrawItem("Device Version:", SystemInfo.graphicsDeviceVersion); 860 DrawItem("Memory Size:", string.Format("{0} MB", SystemInfo.graphicsMemorySize.ToString())); 861 DrawItem("Multi Threaded:", SystemInfo.graphicsMultiThreaded.ToString()); 862 DrawItem("Shader Level:", GetShaderLevelString(SystemInfo.graphicsShaderLevel)); 863 DrawItem("NPOT Support:", SystemInfo.npotSupport.ToString()); 864 DrawItem("Max Texture Size:", SystemInfo.maxTextureSize.ToString()); 865 #if UNITY_5_6_OR_NEWER 866 DrawItem("Max Cubemap Size:", SystemInfo.maxCubemapSize.ToString()); 867 #endif 868 #if UNITY_5_4_OR_NEWER 869 DrawItem("Copy Texture Support:", SystemInfo.copyTextureSupport.ToString()); 870 #endif 871 DrawItem("Supported Render Target Count:", SystemInfo.supportedRenderTargetCount.ToString()); 872 #if UNITY_5_3 || UNITY_5_4 873 DrawItem("Supports Stencil:", SystemInfo.supportsStencil.ToString()); 874 DrawItem("Supports Render Textures:", SystemInfo.supportsRenderTextures.ToString()); 875 #endif 876 DrawItem("Supports Sparse Textures:", SystemInfo.supportsSparseTextures.ToString()); 877 DrawItem("Supports 3D Textures:", SystemInfo.supports3DTextures.ToString()); 878 #if UNITY_5_6_OR_NEWER 879 DrawItem("Supports 3D Render Textures:", SystemInfo.supports3DRenderTextures.ToString()); 880 #endif 881 #if UNITY_5_4_OR_NEWER 882 DrawItem("Supports 2D Array Textures:", SystemInfo.supports2DArrayTextures.ToString()); 883 #endif 884 DrawItem("Supports Shadows:", SystemInfo.supportsShadows.ToString()); 885 DrawItem("Supports Raw Shadow Depth Sampling:", SystemInfo.supportsRawShadowDepthSampling.ToString()); 886 DrawItem("Supports Render To Cubemap:", SystemInfo.supportsRenderToCubemap.ToString()); 887 DrawItem("Supports Compute Shader:", SystemInfo.supportsComputeShaders.ToString()); 888 DrawItem("Supports Instancing:", SystemInfo.supportsInstancing.ToString()); 889 DrawItem("Supports Image Effects:", SystemInfo.supportsImageEffects.ToString()); 890 #if UNITY_5_5_OR_NEWER 891 DrawItem("Supports Cubemap Array Textures:", SystemInfo.supportsCubemapArrayTextures.ToString()); 892 #endif 893 #if UNITY_5_4_OR_NEWER 894 DrawItem("Supports Motion Vectors:", SystemInfo.supportsMotionVectors.ToString()); 895 #endif 896 #if UNITY_5_6_OR_NEWER 897 DrawItem("Graphics UV Starts At Top:", SystemInfo.graphicsUVStartsAtTop.ToString()); 898 #endif 899 #if UNITY_5_5_OR_NEWER 900 DrawItem("Uses Reversed ZBuffer:", SystemInfo.usesReversedZBuffer.ToString()); 901 #endif 902 } 903 GUILayout.EndVertical(); 904 } 905 906 private string GetShaderLevelString(int shaderLevel) 907 { 908 return string.Format("Shader Model {0}.{1}", (shaderLevel / 10).ToString(), (shaderLevel % 10).ToString()); 909 } 910 } 911 912 public class InputSummaryInformationWindow : ScrollableDebuggerWindowBase 913 { 914 private Vector2 _scrollSystemView = Vector2.zero; 915 916 public override void OnDrawScrollableWindow() 917 { 918 GUI.contentColor = Color.white; 919 GUILayout.Label("<b>输入设备信息</b>"); 920 _scrollSystemView = GUILayout.BeginScrollView(_scrollSystemView, GUILayout.Height(300)); 921 GUILayout.BeginVertical("box"); 922 { 923 DrawItem("Back Button Leaves App:", Input.backButtonLeavesApp.ToString()); 924 DrawItem("Device Orientation:", Input.deviceOrientation.ToString()); 925 DrawItem("Mouse Present:", Input.mousePresent.ToString()); 926 DrawItem("Mouse Position:", Input.mousePosition.ToString()); 927 DrawItem("Mouse Scroll Delta:", Input.mouseScrollDelta.ToString()); 928 DrawItem("Any Key:", Input.anyKey.ToString()); 929 DrawItem("Any Key Down:", Input.anyKeyDown.ToString()); 930 DrawItem("Input String:", Input.inputString); 931 DrawItem("IME Is Selected:", Input.imeIsSelected.ToString()); 932 DrawItem("IME Composition Mode:", Input.imeCompositionMode.ToString()); 933 DrawItem("Compensate Sensors:", Input.compensateSensors.ToString()); 934 DrawItem("Composition Cursor Position:", Input.compositionCursorPos.ToString()); 935 DrawItem("Composition String:", Input.compositionString); 936 DrawItem("Touch Supported:", Input.touchSupported.ToString()); 937 DrawItem("Touch Pressure Supported:", Input.touchPressureSupported.ToString()); 938 DrawItem("Stylus Touch Supported:", Input.stylusTouchSupported.ToString()); 939 DrawItem("Simulate Mouse With Touches:", Input.simulateMouseWithTouches.ToString()); 940 DrawItem("Multi Touch Enabled:", Input.multiTouchEnabled.ToString()); 941 DrawItem("Touch Count:", Input.touchCount.ToString()); 942 DrawItem("Touches:", GetTouchesString(Input.touches)); 943 } 944 GUILayout.EndVertical(); 945 GUILayout.EndScrollView(); 946 } 947 private string GetTouchString(Touch touch) 948 { 949 return string.Format("{0}, {1}, {2}, {3}, {4}", touch.position.ToString(), touch.deltaPosition.ToString(), touch.rawPosition.ToString(), touch.pressure.ToString(), touch.phase.ToString()); 950 } 951 952 private string GetTouchesString(Touch[] touches) 953 { 954 string[] touchStrings = new string[touches.Length]; 955 for (int i = 0; i < touches.Length; i++) 956 { 957 touchStrings[i] = GetTouchString(touches[i]); 958 } 959 960 return string.Join("; ", touchStrings); 961 } 962 } 963 964 public class QualityInformationWindow : ScrollableDebuggerWindowBase 965 { 966 private bool m_ApplyExpensiveChanges = false; 967 968 public override void OnDrawScrollableWindow() 969 { 970 GUILayout.BeginHorizontal(); 971 if(GUILayout.Button("退出")) 972 { 973 Application.Quit(); 974 } 975 GUILayout.EndHorizontal(); 976 } 977 }