Profiler.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Diagnostics;
  3. namespace XGame.Framework
  4. {
  5. public static class Profiler
  6. {
  7. [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  8. public static void BeginSample(string sample)
  9. {
  10. UnityEngine.Profiling.Profiler.BeginSample(sample);
  11. Log(sample);
  12. }
  13. [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  14. public static void EndSample()
  15. {
  16. UnityEngine.Profiling.Profiler.EndSample();
  17. }
  18. [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  19. public static void BeginSample(object obj, string sample)
  20. {
  21. BeginSample(sample, obj.GetType());
  22. }
  23. [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  24. public static void BeginSample(Type type, string sample)
  25. {
  26. BeginSample(sample, type);
  27. }
  28. [Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  29. public static void BeginSample(string sample, Type type)
  30. {
  31. string str = $"{type.Namespace}.{type.Name} - {sample}";
  32. UnityEngine.Profiling.Profiler.BeginSample(str);
  33. Log(str);
  34. }
  35. private static void Log(string msg)
  36. {
  37. UnityEngine.Debug.Log($"[Profiler] Time:{UnityEngine.Time.realtimeSinceStartup} msg:{msg}");
  38. }
  39. }
  40. }