ConsoleProDebug.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using System;
  3. #if UNITY_EDITOR
  4. using System.Reflection;
  5. #endif
  6. public static class ConsoleProDebug
  7. {
  8. // Clear the console and the native console
  9. public static void Clear()
  10. {
  11. #if UNITY_EDITOR
  12. if(ConsoleClearMethod != null)
  13. {
  14. ConsoleClearMethod.Invoke(null, null);
  15. }
  16. #endif
  17. }
  18. // Send a log to a specific filter regardless of contents
  19. // Ex: ConsoleProDebug.LogToFilter("Hi", "CustomFilter");
  20. public static void LogToFilter(string inLog, string inFilterName)
  21. {
  22. Debug.Log(inLog + "\nCPAPI:{\"cmd\":\"Filter\" \"name\":\"" + inFilterName + "\"}");
  23. }
  24. // Watch a variable. This will only produce one log entry regardless of how many times it is logged, allowing you to track variables without spam.
  25. // Ex:
  26. // void Update() {
  27. // ConsoleProDebug.Watch("Player X Position", transform.position.x);
  28. // }
  29. public static void Watch(string inName, string inValue)
  30. {
  31. Debug.Log(inName + " : " + inValue + "\nCPAPI:{\"cmd\":\"Watch\" \"name\":\"" + inName + "\"}");
  32. }
  33. #if UNITY_EDITOR
  34. // Reflection calls to access Console Pro from runtime
  35. private static bool _checkedConsoleClearMethod = false;
  36. private static MethodInfo _consoleClearMethod = null;
  37. private static MethodInfo ConsoleClearMethod
  38. {
  39. get
  40. {
  41. if(_consoleClearMethod == null || !_checkedConsoleClearMethod)
  42. {
  43. _checkedConsoleClearMethod = true;
  44. if(ConsoleWindowType == null)
  45. {
  46. return null;
  47. }
  48. _consoleClearMethod = ConsoleWindowType.GetMethod("ClearEntries", BindingFlags.Static | BindingFlags.Public);
  49. }
  50. return _consoleClearMethod;
  51. }
  52. }
  53. private static bool _checkedConsoleWindowType = false;
  54. private static Type _consoleWindowType = null;
  55. private static Type ConsoleWindowType
  56. {
  57. get
  58. {
  59. if(_consoleWindowType == null || !_checkedConsoleWindowType)
  60. {
  61. _checkedConsoleWindowType = true;
  62. Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
  63. for(int iAssembly = 0; iAssembly < assemblies.Length; iAssembly++)
  64. {
  65. Type[] types = assemblies[iAssembly].GetTypes();
  66. for(int iType = 0; iType < types.Length; iType++)
  67. {
  68. if(types[iType].Name == "ConsolePro3Window")
  69. {
  70. _consoleWindowType = types[iType];
  71. }
  72. }
  73. }
  74. }
  75. return _consoleWindowType;
  76. }
  77. }
  78. #endif
  79. }