Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Aubrey Hesselgren · Jan 05, 2011 at 01:10 PM · editor-scriptingopenide

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?).

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aubrey Hesselgren · Jan 06, 2011 at 12:03 PM 0
Share

Amazing! Thanks so much.

I am going to look into StackTrace some more, and will look for the equivalent to "-n" for VisualStudio.

avatar image Aubrey Hesselgren · Jan 06, 2011 at 04:23 PM 0
Share

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

avatar image yoyo · Jan 06, 2011 at 05:17 PM 0
Share

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.

avatar image
3
Best Answer

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");
     }
 }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aubrey Hesselgren · Jan 07, 2011 at 08:19 PM 0
Share

Oops. The gotoLine bit will only take you to the line if you're starting the IDE for the first time. Laaame.

avatar image yoyo · Jan 07, 2011 at 09:17 PM 0
Share

Yeah, that was the conclusion on StackOverflow too. Darn visual studio.

avatar image Aubrey Hesselgren · Jan 08, 2011 at 12:36 AM 0
Share

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?

avatar image yoyo · Jan 08, 2011 at 04:31 AM 0
Share

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 ... !)

avatar image Aubrey Hesselgren · Jan 08, 2011 at 03:20 PM 0
Share

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!

avatar image
13

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jasperstocker · Mar 09, 2014 at 10:22 AM 0
Share

Nice! Thanks :) Need to fine the line number now...

avatar image vexe · Jun 24, 2016 at 02:19 AM 0
Share

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.

avatar image JVLVince · Oct 26, 2017 at 11:07 AM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

in-editor cloning of game objects 1 Answer

How do I set up Atom Editor as my script editor, with autocomplete and the ability to open the right script file when clicking it? 2 Answers

Help with door animation 1 Answer

BCE0005: Unknown identifier: 'EditorUtility'. 1 Answer

Rider 2018.3.1 Editor Package Missing Symbols 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges