繁体   English   中英

Unity3d - 覆盖 Debug.Log()

[英]Unity3d - Overwrite Debug.Log()

在 JavaScript 中,您可以像这样覆盖一个函数,例如console.log()

 console.log("test1"); console.log = function() { return; }; console.log("test2");

我经常用它来隐藏所有调试控制台消息。 我尝试在 Unitys Debug.Log() C# 中应用它,如下所示:

Debug.Log = () => { return; };

但是我得到了Cannot assign to Log, because it is a message group并且The left-hand side of an assignment must be a variable, a property or an indexer

是否有可能以某种方式覆盖它? 我只是好奇。

您可以简单地以理智的方式禁用记录器

Debug.unityLogger.logEnabled = false;

您可以自定义默认记录器处理程序

using System;
using UnityEngine;

public class LogHandler : ILogHandler
{
    public bool enable;
    private static ILogHandler unityLogHandler = Debug.unityLogger.logHandler;

    public LogHandler(bool enable = true)
    {
        this.enable = enable;
    }

    public void LogException(Exception exception, UnityEngine.Object context)
    {
        if(enable)
        {
            unityLogHandler.LogException(exception, context);
        }
    }

    public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
    {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
        if(enable)
        {
            unityLogHandler.LogFormat(logType, context, format, args);
        }
#endif
    }
}

使用:

Debug.unityLogger.logHandler = new LogHandler(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM