- Home /
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?).
Answer by yoyo · Jan 05, 2011 at 06:01 PM
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.
Amazing! Thanks so much.
I am going to look into StackTrace some more, and will look for the equivalent to "-n" for VisualStudio.
This might work for Visual Studio 2008 and above.
http://stackoverflow.com/questions/350323/open-a-file-in-visual-studio-at-a-specific-line-number
Visual Studio 2010 command line options (actually devenv.com) are documented here -- http://msdn.microsoft.com/en-us/library/xee0c8y7(v=VS.100).aspx -- no sign of an option to go to a specific line I'm afraid. $$anonymous$$icrosoft Visual Studio 2010 Express (C# edition) uses VCSExpress.exe to launch the IDE, but it also lacks goto-line functionality. Looks like you're stuck with a custom macro approach in Visual Studio. Disappointing.
Answer by Aubrey Hesselgren · Jan 07, 2011 at 07:58 PM
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");
}
}
Oops. The gotoLine bit will only take you to the line if you're starting the IDE for the first time. Laaame.
Yeah, that was the conclusion on StackOverflow too. Darn visual studio.
Yeah, I did see that, but for some reason, I thought it would magically work for me because of how super drunk I am. But it didn't! Who'da thunk?
You might think after, what, 10 versions of Visual Studio they'd provide this sort of basic functionality. ($$anonymous$$aybe their product manager's been drinking too ... !)
Ahh, it's not the end of the world or anything. I was just hoping it was possible.
But it leaves me wondering... how come unity can take me to a particular line, but this approach doesn't work? How does unity do it? Is it opening via a .sln related call as opposed to a straight file get? Hmm. I dunno anymore!
Answer by Avaista · Aug 06, 2012 at 10:47 PM
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.
Nice! Thanks :) Need to fine the line number now...
This one's giving me a weird flicker. It opens up VS then jumps quickly back to Unity. As if I pressed Alt-Tab twice very quickly.
You was saved my day mate. Thanks so much :D , now I'm thinking how to get the method line inside Script. I want to list all methods and it line. Then jump to which I want just by one click :D
Answer by aeroson · Aug 05, 2015 at 12:18 AM
Found this: http://stackoverflow.com/questions/350323/open-a-file-in-visual-studio-at-a-specific-line-number
And subsequently from that question arised this tool, which is the perfect solution: https://github.com/diimdeep/VisualStudioFileOpenTool
Your answer
Follow this Question
Related Questions
in-editor cloning of game objects 1 Answer
Help with door animation 1 Answer