Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Visual Studio: draw highlights on scroll bar?

Discussion in 'Scripting' started by Marscaleb, May 1, 2021.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    971
    So I've got section of my larger scripts decorated with long lines of asterisks so it's easy to find them when scrolling through the file. Old trick I picked up from the last century.

    But now I've noticed how Visual Studio adds little blocks of color to the scroll bar to make it super easy to find certain things. Where the word I've searched for shows up, where the code I've changed this session is, and probably more. (Don't know what the grey blocks are for.)

    But I was just thinking, is there any way I can manually add decoration to that scroll bar? For example, to add a line in the same place where I have my whole row of asterisks?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,109
    Short answer is, I don't know, but...

    One feature I really like is the expanded scrollbar. Right click, Scroll Bar Options, set Source Overview to "wide". This gives you a little visual breakdown of the whole file, like this:

    upload_2021-5-1_16-11-25.png

    I find that the combination of indentation and syntax highlighting means I easily jump to the place I want, especially in scripts I'm very familiar with.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Another feature I love is partial classes. When you can't be arsed to do the heavy lifting of breaking up a class that has grown a bit large, just put the different functions into different partial class files.

    As long as there is one named correctly inheriting MonoBehaviour, it will work.

    For instance, my Jetpack Kurt Space Flight controller has grown completely out of control from an original "TEST1" script, but I dread refactoring it, so it's currently a pretty massive class, all broken into partials, and I keep all of them in a single subdirectory along with the scene that contains the flight vehicle to be additively loaded into whatever content scene you play:

    Screen Shot 2021-05-01 at 1.13.08 PM.png

    Those are all
    public partial class SpaceFlightTest1
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    971
    Sweet; not quite what I was looking fot, but that may be just as good!

    Is this a special function to divide up a class, or do you just mean to copy the script and divide up the functionality, since you can just drop multiple scripts onto a single game object?

    If it's the latter, it is worth keeping in mind but I don't think I can separate the functions in classes like my player class without causing more hassle than it is worth.
    ...Maybe I could manage that with my game manager though.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,109
    I expect he was mentioning the use of partial classes as an alternative organization scene for your script. You're not really separating any functionality; all of the code in all of the "partial" files are fully aware of each other. So the effort to reorganize things is minimal. If you wanted, you could put every function into a separate file, and it would behave just as though you had everything in the same file. Then, instead of scrolling down to some point in a large file where you know your 3rd set of ******* are, you'd just open the partial file associated with the functionality you wanted to look at.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    As @dgoyette says above, it just lets you slice up a class and put it into more than one file.

    You actually only put ONE instance of the script on your GameObject and it contains ALL the entire class.

    Only one file has to be named precisely as your MonoBehavious is named (the usual Unity limitation).

    Every single one of the files listed above in my massive Jetpack Kurt controller are the same exact class.

    Thus, every single variable or function in any given file is visible to any other of the files.

    The top file has:

    Code (csharp):
    1. public partial class SpaceFlightTest1  : MonoBehaviour
    2. {...
    and pretty much every other file is:

    Code (csharp):
    1. public partial class SpaceFlightTest1
    2. {...
    But you can include the MonoBehaviour part as many times as you like, doesn't matter.

    It's particularly nice if you can break stuff apart, such as in the case above where I break out all the collision stuff into the file marked Damage. In the future if I decide to make that a separate class, I would already have things physically broken apart.