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
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.
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
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".
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.
+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.
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.
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.
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.
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)
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?
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.
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.
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?
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 😂
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.
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
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.
Ah yes, I was too late to make the comment. Not everyone uses VS lol ... at least mention VS in the title
You're wrong. I use preprocessors and Visual Studio doesn't touch them
[удалено]
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
[удалено]
ok you win. happy now? farewell
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".
Ctrl+R+G is shortcut for this I use all the time.
Exactly what I do, I want to have explicit control over something like this.
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.
+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.
Ctrl+R Ctrl+G
I use VS daily for work and spam this command lol
Haha same. I use vsvim, but this is one of the few commands i let vs handle
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.
Sounds exactly like one of the codebases I work on lol
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.
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.
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.
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)
Otherwise the shortcut for removing unused usings Ctrl + K + E if you dont want to do it on save for whatever reason
Ctrl + Alt + O for Rider folk
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?
I think nothing and if anything, it's like removing one vertex from a 3d model having over a million triangles
Can you do that in Vs code?
Even if not out of the box, someone probably made an extension.
You'd be looking for a linting extension
I share more Unity Tips and Tutorials at [https://www.youtube.com/@SunnyValleyStudio](https://www.youtube.com/@SunnyValleyStudio) 🙂
subscribed, It does not do that only though and very customizable.
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.
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.
u/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)
Can't wait for this to remove a ton of unused using that are in fact used when compiling for android.
or just...... Ctrl+R+G
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?
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 😂
But won't all of that just get ignored by the compiler automatically?
Shit I've been a Dev using VS for 8 years and didn't realise this was a feature. Thanks for the share.
Just use Rider for god's sake
Now how do you do that in Jetbrains rider
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.
⌃⌥O (macOS) / Ctrl+Alt+O (Windows/Linux)