TC_Reporter.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. public class TC_Reporter : MonoBehaviour
  4. {
  5. static public TC_Reporter instance;
  6. static bool hasReported;
  7. int frame;
  8. public bool report;
  9. public bool[] channels;
  10. public float[] timeStart;
  11. public TC_Reporter()
  12. {
  13. instance = this;
  14. }
  15. void OnEnable()
  16. {
  17. instance = this;
  18. }
  19. void OnDisable()
  20. {
  21. instance = null;
  22. }
  23. private void LateUpdate()
  24. {
  25. if (hasReported)
  26. {
  27. UnityEngine.Debug.Log("----------------------------------------------------------> " + frame.ToString() + "\n");
  28. frame++;
  29. hasReported = false;
  30. }
  31. }
  32. public static string GetInclination()
  33. {
  34. System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
  35. string text = string.Empty;
  36. for (int i = 0; i < stackTrace.FrameCount - 2; i++) text += " ";
  37. return text;
  38. }
  39. public static void Log(string text, int channelIndex = 0)
  40. {
  41. if (instance == null) return;
  42. if (!instance.report) return;
  43. if (instance.channels == null) instance.channels = new bool[5];
  44. if (instance.channels.Length != 5) instance.channels = new bool[5];
  45. if (instance.channels[channelIndex] || channelIndex == -1)
  46. {
  47. UnityEngine.Debug.Log(GetInclination() + text + "\n");
  48. hasReported = true;
  49. }
  50. }
  51. public static void BenchmarkStart(int channel = 0)
  52. {
  53. if (instance.timeStart == null) instance.timeStart = new float[5];
  54. if (instance.timeStart.Length != 5) instance.timeStart = new float[5];
  55. instance.timeStart[channel] = Time.realtimeSinceStartup;
  56. }
  57. public static string BenchmarkStop(string text = "", bool logToConsole = true, int channel = 0)
  58. {
  59. float time = Time.realtimeSinceStartup - instance.timeStart[channel];
  60. if (logToConsole)
  61. {
  62. text = text + " time " + time + " frame " + (1 / time);
  63. UnityEngine.Debug.Log(text);
  64. }
  65. else { text = text + (1 / time).ToString("F0"); }
  66. return text;
  67. }
  68. }