首页 > 代码库 > Unity4.x Code Auto Clear Console Log

Unity4.x Code Auto Clear Console Log

效果

可在Editor模式下执行,当然也可以Runtime模式下执行

image

作用:当在制作Editor的一些功能时,常常需要手动的点击Console窗口的Clear来清除日志,通过这个方法可以很方便的在脚本编译后自动清除日志

[InitializeOnLoad][ExecuteInEditMode]public partial class CSimulateEnv : MonoBehaviour{    static CSimulateEnv()    {       ClearLog();    }}

代码如下:

using System.Reflection;using UnityEngine;public class ClearTest : MonoBehaviour{    // Use this for initialization    void Start()    {        ClearLog();    }    void OnGUI()    {        if (GUILayout.Button(" clear "))        {            ClearLog();        }    }    public void ClearLog()    {        var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));        var type = assembly.GetType("UnityEditorInternal.LogEntries");        var method = type.GetMethod("Clear");        method.Invoke(new object(), null);    }}

参考自:http://answers.unity3d.com/questions/10580/editor-script-how-to-clear-the-console-output-wind.html

Unity4.x Code Auto Clear Console Log