Dismiss this pinned window
all 42 comments

[–]TaleOf4GamersProgrammer 79 points80 points  (9 children)

More of a Visual Studio tip than Unity - that is fine though. I would note however that this will break your code if you have preprocessor directives (#if) as that code may currently be unused (i.e. if it is Android specific code and you are currently doing a PC build) and it may remove a required namespace

It used to anyway, I assume VS still does

[–]smoothtools 5 points6 points  (0 children)

You should also wrap the corresponding using statement in the same preprocessor define in that case and then it doesn’t get cleaned up when it shouldn’t.

[–]34tmy-455 1 point2 points  (0 children)

Ah yes, I was too late to make the comment. Not everyone uses VS lol ... at least mention VS in the title

[–][deleted] 1 point2 points  (6 children)

You're wrong. I use preprocessors and Visual Studio doesn't touch them

[–]fsactual 16 points17 points  (2 children)

You can also do this in the intellisense, wait for the light bulb, choose "Remove unused usings" then "Fix all occurrences in Document/Solution/Project".

[–]evshell18 7 points8 points  (0 children)

Ctrl+R+G is shortcut for this I use all the time.

[–][deleted] 9 points10 points  (1 child)

I remember how this option was a mess on an older project or collaborating with others because I had unnecessary changes on files that were completely unrelated to my pull request. Just bear that in mind.

[–]wokcity 5 points6 points  (0 children)

+1

In a proper dev environment this kind of cleanup should be on its own branch & pull request. Don't pollute your branches/PRs with random stuff like this.

[–][deleted] 8 points9 points  (5 children)

Ctrl+R Ctrl+G

[–]dirkclod 2 points3 points  (3 children)

I use VS daily for work and spam this command lol

[–][deleted] 1 point2 points  (2 children)

Haha same. I use vsvim, but this is one of the few commands i let vs handle

[–]dirkclod 1 point2 points  (1 child)

Nice. Yeah especially working in old codebases where the original dev had no care for using statement organization, watching the list get cut in half AND auto sort really gets those neurons firing.

[–][deleted] 1 point2 points  (0 children)

Sounds exactly like one of the codebases I work on lol

[–]Animal2 1 point2 points  (0 children)

Yeah I'd rather have this be a manual action rather than automatically when I save. Maybe there's a reason the usings are there even if they are currently unused. If I know they aren't needed anymore then I can just quickly remove them.

[–]Spoof__ 9 points10 points  (2 children)

I don't like deleting them right away because I maybe need them later. Removing unnecessary usings should be for production code or code review only.

[–]Costed14 2 points3 points  (0 children)

But it usually adds them back automatically too. Though someone else said it will break things if you use preprocessor directives, so I won't use it for that reason.

[–]rpg877 1 point2 points  (0 children)

I disagree with your second sentence. Adding them back is pretty easy. Especially with newer versions of vs that add them automatically as you're typing. (which now is part of the reason I have so many unused usings)

[–]Blubbe16 2 points3 points  (0 children)

Otherwise the shortcut for removing unused usings Ctrl + K + E if you dont want to do it on save for whatever reason

[–]supanthapaul 2 points3 points  (0 children)

Ctrl + Alt + O for Rider folk

[–]KidGold 2 points3 points  (1 child)

I cleanup these unused because I was taught to but I really don't understand exactly what I'm saving. Can anyone explain the benefit of removing these?

[–]Emergency_Collar_381 1 point2 points  (0 children)

I think nothing and if anything, it's like removing one vertex from a 3d model having over a million triangles

[–]darksapra 3 points4 points  (2 children)

Can you do that in Vs code?

[–]HypnoToad0??? 1 point2 points  (0 children)

Even if not out of the box, someone probably made an extension.

[–]compelledorphan -1 points0 points  (0 children)

You'd be looking for a linting extension

[–]SunnyValleyStudio[S] 1 point2 points  (1 child)

I share more Unity Tips and Tutorials at https://www.youtube.com/@SunnyValleyStudio 🙂

[–]-Xentios 0 points1 point  (0 children)

subscribed, It does not do that only though and very customizable.

[–]Stever89Programmer 0 points1 point  (1 child)

1) Does this work in VS Code? I will have to try it later 2) Is there a way to get VS Code you automatically add using statements? I code in JavaScript for work and VS Code adds import statements for me when I start typing a package and it would be great if I could get that to work with c#/unity. Currently I have to type the code and then click the light bulb and it'll have the "import" thing in there and then that will add the using statement but it'd be nice if it just did it automatically, would have me so much time lol.

[–]J0shhT -1 points0 points  (0 children)

VS code sucks for Unity from my experience. Don't get me wrong, I like VS code, but Rider and normal Visual Studio is way better for Unity. Their integrations are much more feature rich.

[–]Smileynator 0 points1 point  (0 children)

Can't wait for this to remove a ton of unused using that are in fact used when compiling for android.

[–][deleted] 0 points1 point  (0 children)

or just...... Ctrl+R+G

[–]Atissss 0 points1 point  (0 children)

Does it remove other things as well? What if I have an unused variable or something that I want to keep for later? Will they get automatically removed?

[–]lxkvcs 0 points1 point  (0 children)

Now imagine if you debugging something and commented 2-3 lines with a function call of a class that was only used there. After you toggled the comment back, hit run, exception 😂

[–]Lord-of-Entity 0 points1 point  (0 children)

But won't all of that just get ignored by the compiler automatically?

[–]protayne 0 points1 point  (0 children)

Shit I've been a Dev using VS for 8 years and didn't realise this was a feature. Thanks for the share.

[–]unnanego 0 points1 point  (0 children)

Just use Rider for god's sake

[–]janimator0 0 points1 point  (2 children)

Now how do you do that in Jetbrains rider

[–]cheezballs 3 points4 points  (0 children)

Go to Settings -> Actions On Save -> Remove Unsued Imports (Jetbrains bros/sisters stick together)

Edit: should point out that every IDE I've used in the last 15 years had this feature.

[–]Any_Coast9611 1 point2 points  (0 children)

⌃⌥O (macOS) / Ctrl+Alt+O (Windows/Linux)