简体   繁体   中英

Unity3d - Overwrite Debug.Log()

In JavaScript you are able to overwrite a function eg console.log() like this:

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

I often used this to hide all debug console messages. I tried to apply this in C# at Unitys Debug.Log() like this:

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

But I get Cannot assign to Log, because it is a message group and The left-hand side of an assignment must be a variable, a property or an indexer .

Is it possible to overwrite it somehow? Im just curious.

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

Debug.unityLogger.logEnabled = false;

You can customize the default logger handler

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
    }
}

Using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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