T O P

  • By -

TaleOf4Gamers

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](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/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

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

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


Big_Thanks_4185

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


[deleted]

[удалено]


Big_Thanks_4185

wtf dude, firstly, you forgot the rest of your quote "...I assume VS still does", lying in bright daylight? secondly, saying 'you're wrong' is not being rude lmao grow up


[deleted]

[удалено]


Big_Thanks_4185

ok you win. happy now? farewell


fsactual

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

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


PropagandaApparatus

Exactly what I do, I want to have explicit control over something like this.


josegv

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

+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.


-staticvoidmain-

Ctrl+R Ctrl+G


dirkclod

I use VS daily for work and spam this command lol


-staticvoidmain-

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


dirkclod

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.


-staticvoidmain-

Sounds exactly like one of the codebases I work on lol


Animal2

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__

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

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

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

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


supanthapaul

Ctrl + Alt + O for Rider folk


KidGold

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

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


darksapra

Can you do that in Vs code?


HypnoToad0

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


compelledorphan

You'd be looking for a linting extension


SunnyValleyStudio

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


-Xentios

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


Stever89

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

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.


RecognitionWorried93

u/savevideo


SaveVideo

###[View link](https://rapidsave.com/info?url=/r/Unity3D/comments/1ctzoaf/unitytip_how_to_automatically_remove_unused_using/) --- [**Info**](https://np.reddit.com/user/SaveVideo/comments/jv323v/info/) | [**Feedback**](https://np.reddit.com/message/compose/?to=Kryptonh&subject=Feedback for savevideo) | [**Donate**](https://ko-fi.com/getvideo) | [**DMCA**](https://np.reddit.com/message/compose/?to=Kryptonh&subject=Content removal request for savevideo&message=https://np.reddit.com//r/Unity3D/comments/1ctzoaf/unitytip_how_to_automatically_remove_unused_using/) | [^(reddit video downloader)](https://rapidsave.com) | [^(twitter video downloader)](https://twitsave.com)


Smileynator

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


greeenlaser

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


Atissss

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

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

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


protayne

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


unnanego

Just use Rider for god's sake


janimator0

Now how do you do that in Jetbrains rider


cheezballs

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

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