- Home /
Is there a way to make Console events write to the web browser's console?
I'm wondering, is there a way to have Debug.Log, Debug.LogWarning and Debug.LogError write to the web browser's console when building to the web player? That way, errors and warnings will write to the web browser's console (example - chrome: console.log) in order to see where something went wrong.
Answer by Kryptos · Jul 30, 2012 at 08:22 AM
This might be possible. My guess would be to call a javascript function (real javascript on the HTML page) from Unity. This javascript function will be responsible for writing to the browser console.
That's the idea, but it's not the problem. I know how to do that. It's done with Application.ExternalCall("webJavascriptFunctionName"), but do you know how would I got about extending the Debug class to use that function as well?
I don't think that you can replace the built-in Debug class.
If you just browse around in the Scripting reference, you would have found this:
Application.RegisterLogCallback which allows you to get notified when a log event occurs.
You're assu$$anonymous$$g I didn't check the script reference. I did, immensely, but I guess I missed this. Thanks.
Answer by sotirosn · May 18, 2013 at 02:32 PM
This static function works nicely:
public static void Log(string message, Object context = null) {
#if UNITY_EDITOR
Debug.Log(message, context);
#else
Application.ExternalCall("console.log", message);
#endif
}
Answer by jemonsuarez · Aug 30, 2020 at 08:50 AM
It does write all logging information (such as Debug.Log, Console.WriteLine or Unity’s internal logging) to the browser’s JavaScript console. Documentation.
Yes, that's true for WebGL builds. However WebPlayer builds didn't do this since the WebPlayer was essentially a native code browser plugin which wrote its own log file into a seperate directory. However the [webplayer doesn't exists anymore since most browsers have dropped support for native plugins][1]. WebGL builds weren't really a thing back then.
yah it's rather silly that this site shows questions from 2012 so pro$$anonymous$$ently