문제
커스텀 디버그 클래스를 작성하고 이 함수를 사용했을 때, 에디터 콘솔에서 더블 클릭을 하면 함수를 호출한 파일이 아니라 커스텀 디버그 클래스 파일이 열린다.
예를 들면 CoroutineTest.cs
에서 Debug.Log
를 호출했는데도 불구하고 Debug.cs
가 열린다.

커스텀 디버그 클래스를 작성하는 이유는 이 글을 참고하자.
[Unity/최적화] 04. Debug Log를 에디터에서만 작동하게 하자
문제 일반적으로 생각하는 것과는 달리, Debug Log 함수들은 디버그 빌드에서만 작동하는게 아니라 릴리즈 빌드에서도 작동한다. 이는 성능 저하로 이어지기 때문에 빌드를 하기 전에 Debug Log 함수
upbo.tistory.com
해결 방안
에디터에서 더블클릭으로 에셋을 열 때 호출되는 OnOpenAsset
콜백을 활용한다. 또한 C# 리플렉션을 활용해서 현재 에디터의 콘솔에 활성화되어있는 텍스트를 긁어온 다음, 파일 경로를 찾아 해당 파일을 연다.
[UnityEditor.Callbacks.OnOpenAsset()]
private static bool OnOpenDebugLog(int instance, int line)
{
string name = EditorUtility.InstanceIDToObject(instance).name;
if (!name.Equals("Debug")) return false;
// 에디터 콘솔 윈도우의 인스턴스를 찾는다.
var assembly = Assembly.GetAssembly(typeof(EditorWindow));
if(assembly == null) return false;
var consoleWindowType = assembly.GetType("UnityEditor.ConsoleWindow");
if (consoleWindowType == null) return false;
var consoleWindowField = consoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);
if (consoleWindowField == null) return false;
var consoleWindowInstance = consoleWindowField.GetValue(null);
if (consoleWindowInstance == null) return false;
if (consoleWindowInstance != (object)EditorWindow.focusedWindow) return false;
// 콘솔 윈도우 인스턴스의 활성화된 텍스트를 찾는다.
var activeTextField = consoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);
if (activeTextField == null) return false;
string activeTextValue = activeTextField.GetValue(consoleWindowInstance).ToString();
if (string.IsNullOrEmpty(activeTextValue)) return false;
// 디버그 로그를 호출한 파일 경로를 찾아 편집기로 연다.
Match match = Regex.Match(activeTextValue, @"\(at (.+)\)");
if (match.Success) match = match.NextMatch(); // stack trace의 첫번째를 건너뛴다.
if (match.Success)
{
string path = match.Groups[1].Value;
var split = path.Split(':');
string filePath = split[0];
int lineNum = Convert.ToInt32(split[1]);
string dataPath = UnityEngine.Application.dataPath.Substring(0, UnityEngine.Application.dataPath.LastIndexOf("Assets"));
UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(dataPath + filePath, lineNum);
return true;
}
return false;
}
작성하고 나면 콘솔에서 더블클릭했을 때 제대로 파일이 열리는 것을 확인할 수 있다. 정규식에 처음 매칭되는 것을 건너뛰기 때문에 스택트레이스의 두 번째 경로를 여는 것을 볼 수 있다.

출처
Double-click the redirect script in Unity's Console Window | ProgrammersSought.com
문제
커스텀 디버그 클래스를 작성하고 이 함수를 사용했을 때, 에디터 콘솔에서 더블 클릭을 하면 함수를 호출한 파일이 아니라 커스텀 디버그 클래스 파일이 열린다.
예를 들면 CoroutineTest.cs
에서 Debug.Log
를 호출했는데도 불구하고 Debug.cs
가 열린다.

커스텀 디버그 클래스를 작성하는 이유는 이 글을 참고하자.
[Unity/최적화] 04. Debug Log를 에디터에서만 작동하게 하자
문제 일반적으로 생각하는 것과는 달리, Debug Log 함수들은 디버그 빌드에서만 작동하는게 아니라 릴리즈 빌드에서도 작동한다. 이는 성능 저하로 이어지기 때문에 빌드를 하기 전에 Debug Log 함수
upbo.tistory.com
해결 방안
에디터에서 더블클릭으로 에셋을 열 때 호출되는 OnOpenAsset
콜백을 활용한다. 또한 C# 리플렉션을 활용해서 현재 에디터의 콘솔에 활성화되어있는 텍스트를 긁어온 다음, 파일 경로를 찾아 해당 파일을 연다.
[UnityEditor.Callbacks.OnOpenAsset()]
private static bool OnOpenDebugLog(int instance, int line)
{
string name = EditorUtility.InstanceIDToObject(instance).name;
if (!name.Equals("Debug")) return false;
// 에디터 콘솔 윈도우의 인스턴스를 찾는다.
var assembly = Assembly.GetAssembly(typeof(EditorWindow));
if(assembly == null) return false;
var consoleWindowType = assembly.GetType("UnityEditor.ConsoleWindow");
if (consoleWindowType == null) return false;
var consoleWindowField = consoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);
if (consoleWindowField == null) return false;
var consoleWindowInstance = consoleWindowField.GetValue(null);
if (consoleWindowInstance == null) return false;
if (consoleWindowInstance != (object)EditorWindow.focusedWindow) return false;
// 콘솔 윈도우 인스턴스의 활성화된 텍스트를 찾는다.
var activeTextField = consoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);
if (activeTextField == null) return false;
string activeTextValue = activeTextField.GetValue(consoleWindowInstance).ToString();
if (string.IsNullOrEmpty(activeTextValue)) return false;
// 디버그 로그를 호출한 파일 경로를 찾아 편집기로 연다.
Match match = Regex.Match(activeTextValue, @"\(at (.+)\)");
if (match.Success) match = match.NextMatch(); // stack trace의 첫번째를 건너뛴다.
if (match.Success)
{
string path = match.Groups[1].Value;
var split = path.Split(':');
string filePath = split[0];
int lineNum = Convert.ToInt32(split[1]);
string dataPath = UnityEngine.Application.dataPath.Substring(0, UnityEngine.Application.dataPath.LastIndexOf("Assets"));
UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(dataPath + filePath, lineNum);
return true;
}
return false;
}
작성하고 나면 콘솔에서 더블클릭했을 때 제대로 파일이 열리는 것을 확인할 수 있다. 정규식에 처음 매칭되는 것을 건너뛰기 때문에 스택트레이스의 두 번째 경로를 여는 것을 볼 수 있다.

출처
Double-click the redirect script in Unity's Console Window | ProgrammersSought.com