PerformanceSampler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using SC.XR.Unity.Module_PerformanceSampler;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class PerformanceSampler : MonoBehaviour
  8. {
  9. SamplerSys.SamplePickMultipleCallBack multipleCallBack;
  10. SamplerSys.SampleType[] samples;
  11. int FrameCount = 0;
  12. private float _framesPerSecond;
  13. double _peak = 0;
  14. Text Text_FPS;
  15. Text Text_USED;
  16. Text Text_PEAK;
  17. Text Text_TotalMemRate;
  18. GameObject Slider;
  19. RectTransform Slider_Total_Width;
  20. RectTransform Slider_USED_Width;
  21. void initUI()
  22. {
  23. Text_FPS = transform.Find("Text_FPS").GetComponent<Text>();
  24. Text_USED = transform.Find("Text_USED").GetComponent<Text>();
  25. Text_PEAK = transform.Find("Text_PEAK").GetComponent<Text>();
  26. Text_TotalMemRate = transform.Find("Text_TotalMemRate").GetComponent<Text>();
  27. Slider = transform.Find("Slider").gameObject;
  28. Slider_Total_Width = Slider.transform.Find("Slider_Total").GetComponent<RectTransform>();
  29. Slider_USED_Width = Slider.transform.Find("Slider_USED").GetComponent<RectTransform>();
  30. }
  31. private void Awake()
  32. {
  33. multipleCallBack = null;
  34. samples = new SamplerSys.SampleType[] { SamplerSys.SampleType.ProcessTotalPSS, SamplerSys.SampleType.TotalMemRate, SamplerSys.SampleType.ProcessMemRate };
  35. initUI();
  36. }
  37. private void Start()
  38. {
  39. StartCoroutine(Cor_CollectSamples());
  40. }
  41. private void Update()
  42. {
  43. FrameCount++;
  44. }
  45. private void OnDisable()
  46. {
  47. StopAllCoroutines();
  48. }
  49. private void OnDestroy()
  50. {
  51. StopAllCoroutines();
  52. }
  53. void UpdateProcessPSS(string val)
  54. {
  55. double processPss = Convert.ToDouble(val);
  56. Text_USED.text = $"USED : {val} MB";
  57. if (_peak <= processPss)
  58. {
  59. _peak = processPss;
  60. Text_PEAK.text = $"USED PEAK : {_peak} MB";
  61. }
  62. }
  63. void UpdateProcessCpuRate(string val)
  64. {
  65. float processPssRate = (float)Convert.ToDouble(val);
  66. Slider_USED_Width.sizeDelta = new Vector2(processPssRate * 3 + 5, Slider_USED_Width.rect.height);
  67. }
  68. void UpdateTotalMemRate(string val)
  69. {
  70. float totalMemRate = (float)Convert.ToDouble(val);
  71. Text_TotalMemRate.text = $"总内存使用 (率) : {totalMemRate}%";
  72. Color color = new Color(Text_TotalMemRate.color.r, 1f - (0.6666f * totalMemRate / 100f), Text_TotalMemRate.color.b);
  73. Text_TotalMemRate.color = color;
  74. Slider_Total_Width.GetComponent<Image>().color = color;
  75. var total = totalMemRate * 3 + 5;
  76. if (total >= 305)
  77. total = 305;
  78. Slider_Total_Width.sizeDelta = new Vector2(total, Slider_Total_Width.rect.height);
  79. }
  80. private IEnumerator Cor_CollectSamples()
  81. {
  82. int lastFrameCount = 0;
  83. while (true)
  84. {
  85. yield return new WaitForSecondsRealtime(1f);
  86. // Update FPS
  87. _framesPerSecond = (FrameCount - lastFrameCount);
  88. lastFrameCount = FrameCount;
  89. Text_FPS.text = string.Format("CPU帧速率 : {0} fps", _framesPerSecond);
  90. //Update Samples
  91. SamplerSys.Instance.GetSamples((vals) =>
  92. {
  93. if (!string.IsNullOrEmpty(vals[0]))
  94. {
  95. UpdateProcessPSS(vals[0]);
  96. }
  97. if (!string.IsNullOrEmpty(vals[1]))
  98. {
  99. UpdateTotalMemRate(vals[1]);
  100. }
  101. if (!string.IsNullOrEmpty(vals[2]))
  102. {
  103. UpdateProcessCpuRate(vals[2]);
  104. }
  105. }, samples);
  106. }
  107. }
  108. }