Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Setting up VS/VSCode to work with *.hlsl and *.shader

Discussion in 'Shaders' started by AlexTorbin, Jan 3, 2021.

  1. AlexTorbin


    Dec 12, 2019
    I searched the forum and found nothing specific. I know about External Tools, I think this is more relevant here.

    So I know there is no good support for shader languages, but I hope that my current setup can be improved.

    Pretty much the only extension for VS2019 I found was HLSL Tools for Visual Studio. It works, but only with pure *.hlsl code. It highlights red the inclusion of other files and any built-in unity stuff. Long story short, I couldn't get it to work with *.shader. If anybody knows how to do it, please share your experience.

    Then I looked towards VS Code, which has wider choice of extensions. The best was Shader languages support for VS Code. It colorizes everything, knows intrinsic functions and semantics of HLSL. However, to make it fully work in *.shader files, I had to modify settings.json inside VS Code.
    Code (json):
    1. {
    2.     ...
    3.     "files.associations": {
    4.         "*.shader" : "hlsl"
    5.       }
    6. }
    But I haven't been able to make Unity and VS Code friends in the sense of C# programming. So the next problem was how to open different files inside Unity Editor with different IDE's (default being VS2019). I've done it with the editor script, if anyone is interested here it is:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Callbacks;
    3. using System.Diagnostics;
    4. using System.IO;
    5. using System.Collections.Generic;
    7. public static class ExternalToolSelector
    8. {
    9.     static readonly ProcessStartInfo vsCodeInfo = new ProcessStartInfo("Your path to /Code.exe"); //insert full path to file
    10.     static readonly HashSet<System.Type> vsCodeTypes = new HashSet<System.Type>
    11.     {
    12.         typeof(UnityEngine.TextAsset), //hlsl is considered text
    13.         typeof(UnityEngine.Shader)
    14.     };
    16.     [OnOpenAsset(1)]
    17.     public static bool HandleOpenAsset(int instanceID, int line)
    18.     {
    19.         var asset = Selection.activeObject;
    20.         var assetPath = AssetDatabase.GetAssetPath(asset);
    21.         var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
    22.         if (vsCodeTypes.Contains(assetType)) //can check file extension instead
    23.         {
    24.             vsCodeInfo.Arguments = "\"" + Path.GetFullPath(assetPath) + "\""; //without "" will break at spaces
    25.             Process.Start(vsCodeInfo);
    26.             return true;
    27.         }
    29.         return false;
    30.     }
    31. }

    Now scripts and shaders open in different programs. Someone will not like this approach, but I can live with it. The only problem I still have is that VS Code suggests anything but variables and functions I declare. It doesn't see them and their contents..

    By the way, there was no such problem in VS2019 HLSL plugin (except everything else was the problem). I don't have much experience in this kind of stuff. How can this be fixed? Or, if you know better way to streamline the work with shaders, I will be very grateful.
    Last edited: Jan 3, 2021
  2. SerializeField


    May 22, 2017
    I am struggling with much of the same things. However I can't seem to get the "HLSL Tools for Visual Studio" to work for me. I have it installed but nothing changes when I open a .shader file
  3. AlexTorbin


    Dec 12, 2019
    In Visual Studio go to options/text editor/file extension and associate "shader" with HLSL editor. It should be available in drop list if you have hlsl tools installed. But I don't think it will accept shaderlab syntax..
    upload_2021-1-19_19-19-25.png ->
  4. Henry_Sun


    Jun 1, 2019
    Try this VS2019 extension for untiy shaderlab. It doesn't work better than VS code plugin like ShaderLabVSCode which is my favorite.
    It provides less smart grammar tip and lacks highlighting.
    But at least you don't need to bear with the red tildes.

    And I think switching between two IDEs isn't a bad idea. I'll give it a try.
  5. Henry_Sun


    Jun 1, 2019
    Your Script works great. Thanks.
  6. pony_pony_pony


    May 14, 2021
  7. ahSOLO1


    Feb 1, 2020
    Did some research and this appears to be the best (albeit paid) solution for editing .shader files with VS as of August 2021.

    If you're willing to consider another IDE, Rider has built-in support for Unity's .shader files.
    Last edited: Aug 25, 2021
    DSivtsov and Goularou like this.
  8. stuepfnick


    Apr 19, 2017
    Really? Are you 100% sure and also on macOS? From my class I won 1 year license for any Jetbrains product and already searching a while for a code editor that supports .compute shader files (and maybe others).
    Seems the VS Code plug-in will run on macOS too, but not HLSL extension for Visual Studio.

    It is quite confusing that they don't say anything on the marketplace, that the extension are windows-only. But at least now I know.

    Why I ask: Because I couldn't find a word about shader support of Rider, but in your link there seems to be one! Great …
    So I guess it will be 1 year Rider license then, so I can finally get into shaders!! (I mean: i played around a bit with surface shaders, as they are a bit easier, but for compute shader it would help extremely, if the code editor is more than a plain text editor!)