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

MonoDevelop C#: How to find and remove all unused "using" statements for all scripts in project?

Discussion in 'Scripting' started by rawegames, Sep 17, 2016.

  1. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    I have many "using" statements that actually aren't in use in the script.

    E.g
    using UnityEngine; //used
    using UnityEngine; //duplicate
    using UnityEngine.UI; //not used
    using UnityEngine.SceneManagement; //not used
    using System.Collections; //not used
    using System.Collections; //duplicate
    using System.Collections.Generic; //used​

    My project has almost 1000 scripts, and most contains these statements (see above).
    I would like to declutter the scripts by removing those statements.

    I've tried using MonoDevelop's "Source Analysis" feature that scans open files for "using" statements that are not in use or duplicates. (I also found out that it can sort these statements alphabetically) This feature is limited that:
    1) It only scans scripts that are open in MonoDevelop.
    2) I have to remove them manually (waste of time).

    Is there any MonoDevelop feature to automate the search and removal (of unused and duplicate "using"), and alphabetial order sorting of all scripts in my project so that I don't have to do that manually?

    Assume:
    1) I don't have any "CS0104: Ambiguous Reference" errors.
    2) I've a backup of the project.

    Sorry for the bolds! :)

    Edit:
    I found this 2013 feature request. Has it been implemented?
    https://bugzilla.xamarin.com/show_bug.cgi?id=15203

    P.S. I'm OK with using other software that can automate this issue. However, I can only afford freeware.
     
    Last edited: Sep 17, 2016
  2. gibbie_learnersedge

    gibbie_learnersedge

    Joined:
    Aug 11, 2016
    Posts:
    25
    Try using Visual Studio. If you don't use the libraries. The text will become light grey. You can safely remove them from the script.
     
    Kiwasi likes this.
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Also with Visual Studio you can install the power tools, which have an option to format the code and remove and sort the usings on file save ;)
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just enable code folding and collapse the using statements ;)
     
  5. rawegames

    rawegames

    Joined:
    Aug 1, 2016
    Posts:
    91
    Anyone got a solution that automates the cleanup to all scripts in my project?
     
  6. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I can't help you with a tool -- but you do know the using statements don't add overhead to the compiled output, right? They're just a coding-time shortcut. I've met people who thought they were like an import or created dependencies, so I figure it's worth mentioning in case this is the reason you're asking.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is probably why the OP isn't getting serious answers. No one cares that much to worry about doing this. It's purely a convinience thing.

    There will be a button somewhere in MonoDevelop that does this. But if you really are this concerned about code prettiness, upgrade to visual studio and add on something like reshaper.
     
  8. gibbie_learnersedge

    gibbie_learnersedge

    Joined:
    Aug 11, 2016
    Posts:
    25
    When you build it into your file anything that is coloured light grey will automatically be remove from the build. Do note comments will still be build.
    When unity compiles all of your code it will become one giant page of your code list. All of your "using" will be compiled into only necessary ones.
    Your conveniences honestly doesn't really matter, it does not change the fact that your game is not optimized, or even if you manage to clean up all this "using" how many kb will you save? less than 1kb i presume.
    I'd rather you concentrate on refactoring your code instead of removing "using".
     
    MarkHelsinki likes this.