Is there a way to use hyperlink in Unity's console?

Viewed 2530

I'm using a custom logger(log4net) for Unity, following this guide. This involves making a custom appender that writes to Unity's console using Debug.Log(). All of this works great, however the problem is that whenever a log statement is printed it originates from the custom appender. So when I double click the message in the console I end up opening the code for the appender and not the source code from where the message originated. I want to print a clickable hyperlink in the console to the path of the source code.(I already have the path)

It seems the Unity console already supports clickable links: enter image description here I just don't know how to make it print links that I want.

In IDE's such as Intellij the console automatically parses links,so I tried to print the path for the source in the unity console for example:

(at Assets/Scripts/MovablePlatform.cs:)

But this didn't work

TL;DR

I want to know if it's possible to print clickable link in Unity's console such as 'http://www.google.com' or 'file:///D:/Mysourcode/main.cs'

4 Answers

Rich text supports anchor tags for file paths, but it doesn't appear to support URLs. Your example would be:

Debug.Log("<a href=\"Assets/Scripts/MovablePlatform.cs\" line=\"7\">Assets/Scripts/MovablePlatform.cs:7</a>");

As far as I know, no.

Those links are there so that you can jump to any of the calling methods in the stack trace that resulted in the Debug.Log call, and all you have to do is scroll down. Unity added this feature precisely because of log appenders but the system is entirely managed by Unity. Double-click is still going to go to the actual Debug.Log line, but you can still access other lines via those "hyperlinks" but they are built automagically by Unity and there is no API for writing those links yourself.

If you don't want your log appender to lose that information, don't do whatever it is that is making Unity lose that information when you use your log appender. You didn't include your code, so why the extra information is being lost is unclear. Also check your console settings and that you are using Unity 2019.1 or newer.

An open source Unity Console plug-in is recommended to solve this problem. Click console-hyperlinks!

Adds hyperlink handling for Unity console logs and more

Logging a link like <a href="Assets/A.cs" line="2">asset</a> works for creating asset links since Unity 2019. This is the same mechanism used internally by Unity for clickable stack traces (also introduced in that version). The path must reference an existing Unity asset (not just any file).

Note, however, that while the link renders in blue in the main list of debug log messages, it isn't clickable there, only in the detailed pane below (which also shows the stack trace). That is, you must first click the log message, then click the link in the detailed message pane.

(Finally, in my testing I occasionally hit what may be a Unity bug or a bug in the Rider IDE integration, where I sometimes have to Alt+Tab away from Unity and back before it will open any asset link, stack trace links included. I assume doing so resets some sort of state in Unity's asset database, which is why it fixes things.)

Since Unity 2021.2.0a11, both web links and file links are supported:

Debug.Log("Click me: <a href=\"Assets/A.cs\" line=\"2\">local file</a>");
Debug.Log("Click me: <a href=\"https://example.org\">website</a>");

You can now even hook into the hyperlink handling and do whatever you please with the result using EditorGUI.hyperLinkClicked.

UnityEditor.EditorGUI.hyperLinkClicked += (window, args)
    => Debug.Log($"clicked link to {args.hyperLinkData["href"]} in {window}");

so you can even implement your own ad-hoc links (by using other attributes than href, to avoid clashes).

Related