Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Microsoft VS - Class View

Discussion in 'Scripting' started by Karwoch, Sep 1, 2016.

  1. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    I`m creating turn based game, and as it is very code heavy, some of my scripts have more than few thousand line of code. Sometimes I just need to find right function - with help of class view. But I already have a lot of different scripts, and I always first have to find a file I`m currently in, and then search for right class... this is time comsuming, so I was wandering if anyone knows a way to automaticly choose and highlight right script in class view I`m currently in, so I can easly find right function in file I`m currently working with.
     
  2. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
  3. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    more info :
    Unload your project,
    Edit the CSPROJ file,
    Search for the compile items

    Do this to your partial class

    Code (csharp):
    1.  
    2.  <CompileInclude="TestClass.cs" />
    3.  <CompileInclude="TestClass.SomeLogic.cs" >
    4.       <DependentUpon>TestClass.cs</DependentUpon>
    5.  </Compile>
    6. <CompileInclude ="TestClass.SomeLogic2.cs">
    7.     <DependentUpon>TestClass.cs</DependentUpon>
    8.  </Compile>
    9.  
    and see the result in your solution explorer

    upload_2016-9-1_14-29-21.png
     
  4. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    French, not exacly my language :D But eather way, first of - thanks, secondly, I hoped there will be some automatical easy solution, but as my research gone deeper - none of this I can find. I like to customise my workspace, so it is more efficient in my way, but it seems, I can`t do that kind of magic wich Unity/Editor scripting does ;)

    Or.. what You listed now is exacly this? I`ll look into this, thanks!
     
  5. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Yes is exactly this, Partial class work in the same way as a single class, it's used only for splitting your code and have more visibility

    Every file should start like this :

    Code (csharp):
    1.  
    2. //this my file TestClass.SomeLogic.cs
    3. public partial class TestClass
    4. {
    5. //Put all the method for SomeLogic
    6. Public void SomeLogic()
    7. {
    8.  
    9. }
    10. public void Bar()
    11. {
    12. }
    13. }
    14.  
    Code (csharp):
    1.  
    2. //this my file TestClass.SomeLogic2.cs
    3. public partial class TestClass
    4. {
    5. //Put all the method for SomeLogic2
    6. Public void SomeLogic2()
    7. {
    8.  
    9. }
    10.  
    11. public void Foo()
    12. {
    13. }
    14. }
    15.  
     
  6. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Not exactly an answer to your question, but your scripts are several thousand lines, you could look into refactoring parts of your code into different classes.

    Check this out.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Visual Studios has a lot of ways to help you navigate and organize your code. Besides the easier solution of splitting up task between classes that is.

    Right clicking a method call and selecting "Peek Definition" or "Go to Definition" allows you to look at that method. It can be a method within the same class or different classes. Super easy way to navigate around if needed.

    Also, don't forget region tags
    #region This is code
    #endregion

    Splitting code into regions will help you organize code better as you can collapse regions of code that you may not normally need to look at much.

    Also, right clicking a method, variable, or property and using "Find all References" will allow you to find references to the item.

    There are many ways to easily navigate through your code. (and note the above has shortcut commands for the right click stuff, just not listing it here) These just happen to be some that I use so I don't have to hunt down stuff.

    Of course, there are also the drop downs along the top under your tabs that can help you navigate around if needed.
     
  8. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Reading your post again, I'm not exactly sure what you're asking. Are you looking for more efficient ways of organising your code, or navigating through your code? Can you give another example of your problem?
     
  9. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    #Brathnann, thanks! Didn`t know about those useful tips, especially region, I was looking for something like this, I thought it just doesn`t exist in VS as usually clicking on line where function is did collapse thing.

    #Boz0r, I have already pretty much differernt scipts, so i try to not add more, and try to keep everything in classes which are responsible for very specific thing.

    But to be specific - there is class view window on upper right side (on default window setup), with search panel. Below that is list of all scripts in my project - I would like to VS choose for me always same script I`m working with - so in window below that, I could simpy jump from function to function.

    Now, I have to always search for correct script, and then search for right function, therefor this is useles, as crtl+f key and search usually is faster, but sometimes I end up finding something different than function call ;) (Beside f12 key to find reference of course)
     
  10. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    just search in the "solution explorer" or use control+; that will search all class and methods in the solution.
     
  11. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Well first of all I'd say your project needs better organization. This doesn't help your immediate problem, but think about not putting more than one class in a single file. Spread those out... use folder and namespaces for logical division, you'll also find that some of your code becomes reusable and you get less duplication.

    That being said, there are a couple of great ways to find what you're looking for. If you know the class name for sure (or method name, what have you) and you're not sure what file it's in, there's a handy Search bar right above the file explorer. You can use it to narrow down your search like so:

    vssearch1.PNG

    If you know the file and you're already in it, there are dropdowns at the top of the editor window. The first you can ignore in my example above because I have multiple projects in this solution... the second is the Class dropdown and will show all classes in the file... use it to navigate to the class you want. Once your cursor is in that class, the last dropdown will allow you to navigate right to a specific property / method within that class:

    vssearch2.png
     
  12. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    Thank You, as this is my first THAT big project, all help from more experienced VS users are very welcome :) For now I`m trying to get founds for my project, and if I will be successful, than I will rewrite everything (or rather copy/paste) to get right organization. I try my best to create everything component style, and reusable style, but in my turn based game many things are for one specific use only, and code multiply itself like crazy ;)
     
  13. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    There's a setting in Visual Studio that always shows you the active file in the Solution Explorer:

    Tools – Options – Projects and Solutions – Track Active Item in Solution Explorer

    It's also a good idea to learn all the cool keyboard shortcuts, and get ReSharper if you can. Here's the default shortcuts for Visual Studio. The ones under View and Edit are probably the most important ones. These are some of the important ones:

    View.NavigateBackward
    View.NavigateForward
    Edit.GoToDefinition
    Edit.FindAllReferences