Unity・3DCG技術ブログ

TAもどきによるUnity・3DCGに関する記事をアップします。

Unityでファイルの特定の行数にジャンプする

概要

Unityでコンソールウィンドウの行をダブルクリックしたときにコードにジャンプしますが それと同じことをUnityのインターナルなメソッドを呼ぶことでできるのでメモ

コード

    static void OpenFileOnSpecificLineAndColumn(string filePath, int line, int column)
    {
        Assembly.GetAssembly(typeof(EditorApplication))
        .GetType("UnityEditor.LogEntries")
        .GetMethod(nameof(OpenFileOnSpecificLineAndColumn), BindingFlags.Static | BindingFlags.Public)
        .Invoke(null, new object[] { filePath, line, column });
    }

使用例

        var path = AssetDatabase.GetAssetPath(アセット);
        var ioPath = Application.dataPath.Replace("Assets", "") + path;
        OpenFileOnSpecificLineAndColumn(ioPath, line, 0);

pathにはApplication.dataPathAssetDatabase.GetAssetPathをくっつけたものを指定する。