Search Unity

Visual Studio Tips and Tricks (With Visuals)

Discussion in 'Community Learning & Teaching' started by yasirkula, Sep 8, 2020.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Hi,

    I wanted share the keyboard shortcuts I commonly use on Visual Studio Community 2017/2019 editions, as well as some other tips. Some of these may be new to some developers.

    Hope some of you find these useful!

    SHORTCUTS
    • CTRL+K followed by CTRL+C: comments out selected line(s) with
      //
      . If a portion of a single line is selected, that portion is commented out with
      /*
      and
      */
      :

    • CTRL+K followed by CTRL+U: uncomments selected line(s)
    • CTRL+T: learnt this one recently and it is one of my favorites since then. Shows a search bar at the top right corner of the window and you can type the name of a class here to quickly navigate to that class:

    • CTRL+G: allows you to quickly jump to a line:

    • CTRL+Shift+F: searches a term in the whole project. You just need to set “Look in“ to “Entire solution”. If you click the "..." button to the right of “Look in”, you can search the term in a specific folder:

    • CTRL+Period (.): shows Quick Actions dialog at the cursor position. It can be used to e.g. automatically add a using statement for a class:

    • CTRL+Space: shows auto-complete dialog at the cursor position:

    • CTRL+Shift+Space: if the cursor is inside a function's parentheses, displays that function's signature in a dialog. If the function has multiple overloads, you can scroll between them with up and down arrow keys:

    • CTRL+R followed by CTRL+R: renames a variable, function, class, parameter in every place that it is used at:

    • CTRL+Left and right arrow keys: jumps cursor over words
    • CTRL+Backspace: deletes the code word by word instead of letter by letter:

    • CTRL+Up and down arrow keys: scrolls the code
    • Alt+Up and down arrow keys: shifts selected line(s) up or down:

    • CTRL+D: duplicates selected line:

    • CTRL+Enter: inserts new line at cursor position while keeping the cursor position intact:

    • Pressing Tab while some lines are selected: indents the selected line(s) forwards:

    • Pressing Shift+Tab while some lines are selected: indents the selected line(s) backwards
    • Typing for and hitting Tab twice: automatically inserts a for loop at cursor position. Same goes for while, do, switch and if statements:

    • Clicking different places in code while CTRL+Alt is held: places multiple cursors in the code. Keyboard actions are applied to all these cursors. Can be useful to insert a text to multiple lines at once or remove a text from multiple lines at once:


    EXTRAS
    • When you add a using statement to your scripts, sometimes new errors may show up in your script. For example, if you use
      Object.Destroy(anObject);
      in your script and add
      using System;
      to the top, “CS0104 Error: ‘Object’ is an ambiguous reference between ‘UnityEngine.Object’ and ‘object’” error will show up. That's because the class Object exists in both UnityEngine and System namespaces and the compiler can't figure out which one our Object refers to. To solve this, you can either change
      Object.Destroy
      to
      UnityEngine.Object.Destroy
      or add
      using Object = UnityEngine.Object;
      line to the top of your script. In the latter case, it tells the compiler to consider every Object a UnityEngine.Object in that script (unless System.Object is declared explicity)
    • [SerializeField] variables can cause “warning CS0649: Field ‘variable’ is never assigned to, and will always have its default value null” warnings in Unity console. To fix these warnings, you can put all your [SerializeField] variables in-between
      #pragma warning disable 0649
      and
      #pragma warning restore 0649
      lines:
    Code (CSharp):
    1. #pragma warning disable 0649
    2. [SerializeField]
    3. private float movementAreaRadius = 75f;
    4. [SerializeField]
    5. private bool isDynamicJoystick = false;
    6. #pragma warning restore 0649
     
    John_MSFT likes this.