SamplerSys.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity.Module_PerformanceSampler
  5. {
  6. /* >> SamplerSys <<
  7. *
  8. * 性能采集模块
  9. *
  10. * 回调方式获取数据
  11. *
  12. * GetSample, GetSamples ---> 调用方法
  13. * SampleType ---> 样本种类
  14. *
  15. */
  16. public class SamplerSys
  17. {
  18. public delegate void SamplePickCallBack(string val);
  19. public delegate void SamplePickMultipleCallBack(string[] vals);
  20. public void GetSample(SamplePickCallBack callback, SampleType sampleType)
  21. {
  22. getSample(callback, (int)sampleType);
  23. }
  24. /// <summary>
  25. /// callback 返回的 String[] 按照传入的 SampleType[] 顺序返回, 长度一致
  26. /// </summary>
  27. public void GetSamples(SamplePickMultipleCallBack callback, SampleType[] samples)
  28. {
  29. int[] SampleTypes = new int[samples.Length];
  30. for (int i = 0; i < SampleTypes.Length; i++)
  31. {
  32. SampleTypes[i] = (int)samples[i];
  33. }
  34. getSamples(callback, SampleTypes);
  35. }
  36. public enum SampleType
  37. {
  38. //Cpu
  39. TotalCpuRate = 1000,
  40. ProcessCpuRate = 1001,
  41. CpuModel = 1002,
  42. TopListByCpuOrder = 1003,
  43. //Mem
  44. TotalMemRate = 2000,
  45. ProcessMemRate = 2001,
  46. TotalMemSize = 2002, // MB
  47. ProcessTotalPSS = 2003, // MB
  48. TopListByMemOrder = 2004,
  49. SampleMemInfo = 2010, // MB
  50. DetailMemInfo = 2011, // KB
  51. AvailableMem = 2012, // MB
  52. PrivateDirty = 2020, // KB
  53. ProcessPSSInfo = 2021, // KB
  54. NativeHeap = 2022, // KB
  55. DalvikHeap = 2023, // KB
  56. //Gpu
  57. TotalGpuRate = 3000,
  58. GpuModel = 3001,
  59. GpuMinFreq = 3002,
  60. GpuMaxFreq = 3003
  61. }
  62. private static AndroidJavaObject _AJC = null;
  63. private static AndroidJavaObject AJC
  64. {
  65. get
  66. {
  67. if (_AJC == null)
  68. {
  69. _AJC = new AndroidJavaClass("com.Ychao.PerformanceAnalysis.SamplerSystem").CallStatic<AndroidJavaObject>("getInstance", Context);
  70. }
  71. return _AJC;
  72. }
  73. }
  74. private static AndroidJavaObject context = null;
  75. private static AndroidJavaObject Context
  76. {
  77. get
  78. {
  79. if (context == null)
  80. {
  81. using (AndroidJavaObject unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  82. {
  83. context = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
  84. }
  85. }
  86. return context;
  87. }
  88. }
  89. private static Lazy<SamplerSys> instance;
  90. public static SamplerSys Instance
  91. {
  92. get
  93. {
  94. if (instance == null)
  95. {
  96. instance = new Lazy<SamplerSys>();
  97. }
  98. return instance.Value;
  99. }
  100. }
  101. private void getSample(SamplePickCallBack callBack, int SampleType)
  102. {
  103. #if !UNITY_EDITOR && UNITY_ANDROID
  104. AJC.Call("getSample", new SamlpesReceiver(callBack, null), SampleType);
  105. #endif
  106. }
  107. private void getSamples(SamplePickMultipleCallBack callBack, int[] SampleTypes)
  108. {
  109. #if !UNITY_EDITOR && UNITY_ANDROID
  110. AJC.Call("getSamples", new SamlpesReceiver(null, callBack), SampleTypes);
  111. #endif
  112. }
  113. }
  114. }