ProfilerMemoryBlock.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections;
  3. using SRF;
  4. using UnityEngine;
  5. using UnityEngine.Profiling;
  6. using UnityEngine.UI;
  7. namespace SRDebugger.UI.Controls
  8. {
  9. public class ProfilerMemoryBlock : SRMonoBehaviourEx
  10. {
  11. protected override void OnEnable()
  12. {
  13. base.OnEnable();
  14. this.TriggerRefresh();
  15. }
  16. protected override void Update()
  17. {
  18. base.Update();
  19. if (SRDebug.Instance.IsDebugPanelVisible && Time.realtimeSinceStartup - this._lastRefresh > 1f)
  20. {
  21. this.TriggerRefresh();
  22. this._lastRefresh = Time.realtimeSinceStartup;
  23. }
  24. }
  25. public void TriggerRefresh()
  26. {
  27. long totalReservedMemoryLong = UnityEngine.Profiling.Profiler.GetTotalReservedMemoryLong();
  28. long totalAllocatedMemoryLong = UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong();
  29. long num = totalReservedMemoryLong >> 10;
  30. num /= 1024L;
  31. long num2 = totalAllocatedMemoryLong >> 10;
  32. num2 /= 1024L;
  33. this.Slider.maxValue = (float)num;
  34. this.Slider.value = (float)num2;
  35. this.TotalAllocatedText.text = "Reserved: <color=#FFFFFF>{0}</color>MB".Fmt(new object[]
  36. {
  37. num
  38. });
  39. this.CurrentUsedText.text = "<color=#FFFFFF>{0}</color>MB".Fmt(new object[]
  40. {
  41. num2
  42. });
  43. }
  44. public void TriggerCleanup()
  45. {
  46. base.StartCoroutine(this.CleanUp());
  47. }
  48. private IEnumerator CleanUp()
  49. {
  50. GC.Collect();
  51. yield return Resources.UnloadUnusedAssets();
  52. GC.Collect();
  53. this.TriggerRefresh();
  54. yield break;
  55. }
  56. private float _lastRefresh;
  57. [RequiredField]
  58. public Text CurrentUsedText;
  59. [RequiredField]
  60. public Slider Slider;
  61. [RequiredField]
  62. public Text TotalAllocatedText;
  63. }
  64. }