SamlpesReceiver.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity.Module_PerformanceSampler
  5. {
  6. public class SamlpesReceiver : AndroidJavaProxy
  7. {
  8. private readonly SamplerSys.SamplePickCallBack sampleCall;
  9. private readonly SamplerSys.SamplePickMultipleCallBack samplesCall;
  10. private readonly SamplesCallBackHelper callbackHelper;
  11. public SamlpesReceiver(SamplerSys.SamplePickCallBack sampleCall, SamplerSys.SamplePickMultipleCallBack samplesCall) : base("com.Ychao.PerformanceAnalysis.SampleReceiver")
  12. {
  13. this.sampleCall = sampleCall;
  14. this.samplesCall = samplesCall;
  15. callbackHelper = new GameObject("SamplesCallBackHelper").AddComponent<SamplesCallBackHelper>();
  16. }
  17. public void OnSampleReceived(string val)
  18. {
  19. callbackHelper.CallonMainThread(() => sampleCallBack(val));
  20. }
  21. public void OnSamplesReceived(string[] vals)
  22. {
  23. callbackHelper.CallonMainThread(() => samplesCallBcak(vals));
  24. }
  25. private void sampleCallBack(string val)
  26. {
  27. if (string.IsNullOrEmpty(val))
  28. {
  29. val = null;
  30. }
  31. try
  32. {
  33. if (sampleCall != null)
  34. sampleCall(val);
  35. }
  36. finally
  37. {
  38. Object.Destroy(callbackHelper.gameObject);
  39. }
  40. }
  41. private void samplesCallBcak(string[] vals)
  42. {
  43. if (vals == null||vals.Length == 0)
  44. {
  45. vals = null;
  46. }
  47. for (int item = 0; item < vals.Length; item++)
  48. {
  49. if (string.IsNullOrEmpty(vals[item]))
  50. {
  51. vals[item] = null;
  52. }
  53. }
  54. try
  55. {
  56. if (samplesCall != null)
  57. samplesCall(vals);
  58. }
  59. finally
  60. {
  61. Object.Destroy(callbackHelper.gameObject);
  62. }
  63. }
  64. }
  65. }