Open script editor IDE to a particular line/function from EditorScript?

When using things like "Debug.Log", the console window can be double clicked when that log is hit, and open your editor to the line which created it.

In the same way, I would like one of the buttons in my custom Editor Window to open a component's associated script file, and ideally to a particular function name.

i.e. you are able to open web pages with this: http://unity3d.com/support/documentation/ScriptReference/Application.OpenURL.html

Is there some equivalent for opening your chosen IDE to a particular script, and a particular line (if not a particular function name?).

I know that this one is probably old, but I was looking for how to do this.

I found AssetDatabase.OpenAsset(Object,int) got what I wanted.

I don’t know if this was available before, but I thought I would post it here since this was the first Google result.

This works ...

if (GUILayout.Button("edit"))
{
    System.Diagnostics.Process.Start("notepad.exe", @"C:\foo.txt");
}

But you'll need to know the command line for your IDE, and also a way to determine which file and line you want to edit. I use notepad++, which supports "-n#" to go to a certain line.

To get the filename, you can use AssetDatabase.GetAssetPath on the MonoScript asset for your script. Note that this will be relative to Application.dataPath.

Getting the MonoScript from the component should be possible, perhaps with EditorUtility.CollectDependencies.

Another way to find file and line information is via StackTrace, like so:

if (GUILayout.Button("edit"))
{
    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);
    string currentFile = st.GetFrame(0).GetFileName(); 
    int currentLine = st.GetFrame(0).GetFileLineNumber();
    System.Diagnostics.Process.Start("notepad++.exe", String.Format("-n{0} {1}", currentLine, currentFile));
}

To get file and line information for a particular method, you'd need to retrieve the StackTrace while in that method. Alternatively, if you can find the path to the file you can read and parse the file yourself to find the line number of the method you're interested in.

See MSDN for more info on Process and StackTrace.

I got it working! Here's a C# version for opening c# files associated with a given component (anything deriving form MonoBehaviour).

using System.IO;

...

  public static void OpenComponentInVisualStudioIDE(MonoBehaviour component, int gotoLine)
    {

        string[] fileNames = Directory.GetFiles(Application.dataPath, component.GetType().ToString() + ".cs", SearchOption.AllDirectories);
        if (fileNames.Length > 0)
        {
            string finalFileName = Path.GetFullPath(fileNames[0]);
            //Debug.Log("File Found:" + fileNames[0] + ". Converting forward slashes: to backslashes" + finalFileName );

            System.Diagnostics.Process.Start("devenv", " /edit \"" + finalFileName + "\" /command \"edit.goto " + gotoLine.ToString() + " \" ");

        } else {
            Debug.Log("File Not Found:" + component.GetType().ToString() + ".cs");
        }
    }

Found this:

And subsequently from that question arised this tool, which is the perfect solution: