查看编辑器源码(工具:ILSpy
)
利用反射来执行图示Clear方法
[MenuItem("CustomTools/LogText")]
private static void LogText()
{
Debug.Log("Hello Text ~~~");
}
// 利用反射来执行 LogEntries.Clear() 方法
[MenuItem("CustomTools/ClearConsole")]
private static void ClearConsole()
{
Assembly assembly = Assembly.GetAssembly(typeof(Editor));
// 反射获取 LogEntries 对象
MethodInfo methodInfo = assembly.GetType("UnityEditor.LogEntries").GetMethod("Clear");
// 反射执行 Clear 方法
methodInfo?.Invoke(null, null);
}