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

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.

{
    ...
    "files.associations": {
        "*.shader" : "hlsl"
      }
}

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:

using UnityEditor;
using UnityEditor.Callbacks;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

public static class ExternalToolSelector
{
    static readonly ProcessStartInfo vsCodeInfo = new ProcessStartInfo("Your path to /Code.exe"); //insert full path to file
    static readonly HashSet<System.Type> vsCodeTypes = new HashSet<System.Type>
    {
        typeof(UnityEngine.TextAsset), //hlsl is considered text
        typeof(UnityEngine.Shader)
    };

    [OnOpenAsset(1)]
    public static bool HandleOpenAsset(int instanceID, int line)
    {
        var asset = Selection.activeObject;
        var assetPath = AssetDatabase.GetAssetPath(asset);
        var assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
        if (vsCodeTypes.Contains(assetType)) //can check file extension instead
        {
            vsCodeInfo.Arguments = "\"" + Path.GetFullPath(assetPath) + "\""; //without "" will break at spaces
            Process.Start(vsCodeInfo);
            return true;
        }

        return false;
    }
}

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.

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

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…
6741124--776869--upload_2021-1-19_19-19-25.png

5 Likes

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.

Your Script works great. Thanks.

You can download VS Code and download an extention called Debugger for Unity (Debugger for Unity - Visual Studio Marketplace) and for .shader files - Shader languages support for VS Code (Shader languages support for VS Code - Visual Studio Marketplace). Debugger for Unity let you work with C# like you do in VS provided by Unity. By the way, don’t forget to set VS Code in Editor > Edit > Prefernces > External Tools > External Script Editor.

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.

3 Likes

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!)

GREAT!