ProfilerMonoBlock.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. namespace SRDebugger.UI.Controls
  2. {
  3. using System;
  4. using SRF;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. #if UNITY_5_5_OR_NEWER
  8. using UnityEngine.Profiling;
  9. #endif
  10. public class ProfilerMonoBlock : SRMonoBehaviourEx
  11. {
  12. private float _lastRefresh;
  13. [RequiredField]
  14. public Text CurrentUsedText;
  15. [RequiredField]
  16. public GameObject NotSupportedMessage;
  17. [RequiredField]
  18. public Slider Slider;
  19. [RequiredField]
  20. public Text TotalAllocatedText;
  21. private bool _isSupported;
  22. protected override void OnEnable()
  23. {
  24. base.OnEnable();
  25. #if UNITY_5_6_OR_NEWER
  26. _isSupported = Profiler.GetMonoUsedSizeLong() > 0;
  27. #else
  28. _isSupported = Profiler.GetMonoUsedSize() > 0;
  29. #endif
  30. NotSupportedMessage.SetActive(!_isSupported);
  31. CurrentUsedText.gameObject.SetActive(_isSupported);
  32. TriggerRefresh();
  33. }
  34. protected override void Update()
  35. {
  36. base.Update();
  37. if (SRDebug.Instance.IsDebugPanelVisible && (Time.realtimeSinceStartup - _lastRefresh > 1f))
  38. {
  39. TriggerRefresh();
  40. _lastRefresh = Time.realtimeSinceStartup;
  41. }
  42. }
  43. public void TriggerRefresh()
  44. {
  45. long max;
  46. long current;
  47. #if UNITY_5_6_OR_NEWER
  48. max = _isSupported ? Profiler.GetMonoHeapSizeLong() : GC.GetTotalMemory(false);
  49. current = Profiler.GetMonoUsedSizeLong();
  50. #else
  51. max = _isSupported ? Profiler.GetMonoHeapSize() : GC.GetTotalMemory(false);
  52. current = Profiler.GetMonoUsedSize();
  53. #endif
  54. var maxMb = (max >> 10);
  55. maxMb /= 1024; // On new line to workaround IL2CPP bug
  56. var currentMb = (current >> 10);
  57. currentMb /= 1024;
  58. Slider.maxValue = maxMb;
  59. Slider.value = currentMb;
  60. TotalAllocatedText.text = "Total: <color=#FFFFFF>{0}</color>MB".Fmt(maxMb);
  61. if (currentMb > 0)
  62. {
  63. CurrentUsedText.text = "<color=#FFFFFF>{0}</color>MB".Fmt(currentMb);
  64. }
  65. }
  66. public void TriggerCollection()
  67. {
  68. GC.Collect();
  69. TriggerRefresh();
  70. }
  71. }
  72. }