ConsoleHelper.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 
  2. /*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*\ ( ( ) )
  3. |/ \| ) ) _((_
  4. || (c) Wanzyee Studio < wanzyeestudio.blogspot.com > || ( ( |_ _ |=n
  5. |\ /| _____)) | ! ] U
  6. \.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ./ (_(__(S) |___*/
  7. using UnityEditor;
  8. using System;
  9. using System.Reflection;
  10. namespace WanzyeeStudio.Editrix.Toolkit{
  11. /// <summary>
  12. /// Helper to clear logs in Console window.
  13. /// </summary>
  14. ///
  15. /// <remarks>
  16. /// Trigger from the menu "Window/Clear Console", or hotkey ALT-Shift-C.
  17. /// </remarks>
  18. ///
  19. public static class ConsoleHelper{
  20. /// <summary>
  21. /// Clear the console logs, with hotkey Alt-Shift-C.
  22. /// </summary>
  23. /*
  24. * Invoke twice to avoid only focusing the Console window in case.
  25. */
  26. [MenuItem("Window/Clear Console &#c", false, 2200)]
  27. public static void ClearConsole(){
  28. if(!ClearConsoleValid()) throw new MissingMethodException("UnityEditorInternal.LogEntries", "Clear");
  29. _method.Invoke(null, null);
  30. _method.Invoke(null, null);
  31. }
  32. /// <summary>
  33. /// Check if <c>ClearConsole()</c> valid, reflection existing.
  34. /// </summary>
  35. /// <returns><c>true</c>, if valid.</returns>
  36. [MenuItem("Window/Clear Console &#c", true)]
  37. private static bool ClearConsoleValid(){
  38. return (null != _method);
  39. }
  40. /// <summary>
  41. /// Method info to clear the console window.
  42. /// Reflect to <c>UnityEditorInternal.LogEntries.Clear()</c>.
  43. /// It's a public static method, without params, return <c>void</c>.
  44. /// </summary>
  45. /*
  46. * http://answers.unity3d.com/questions/707636/
  47. */
  48. private static readonly MethodInfo _method = new Func<MethodInfo>(() => {
  49. var _t = Type.GetType("UnityEditorInternal.LogEntries, UnityEditor");
  50. if(null == _t) return null;
  51. var _b = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
  52. return _t.GetMethod("Clear", _b, null, new Type[0], null);
  53. })();
  54. }
  55. }