Unity默認(rèn)使用的編譯器VisualStudio帶有擴(kuò)展插件ShaderLabVS,但功能很差,所以還是選用VisualStudioCode作為編寫Shader的編譯器,一方面其能自動識別Shaderlab語法,并且還有豐富的Shader擴(kuò)展插件來輔助編寫。
實(shí)際上編寫時我們只希望.shader文件有VSCODE打開,其他腳本正常還是用VS,可以通過Editor.Callbacks的特性來完成。
using System;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public class ShaderEditor
{
[OnOpenAssetAttribute(1)]
public static bool step1(int instanceID, int line)
{
string path = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
string name = Application.dataPath + "/" + path.Replace("Assets/", "");
if (name.EndsWith(".shader")) //文件擴(kuò)展名類型
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "C:/Users/Administrator/AppData/Local/Programs/Microsoft VS Code/Code.exe"; //VSCODE程序
startInfo.Arguments = name;
process.StartInfo = startInfo;
process.Start();
return true;
}
return false;
}
}
將startInfo.FileName替換成自己VSCODE的路徑即可,這里不建議使用環(huán)境變量,Windows自帶的路徑是\而C#啟用的地址是/,這里轉(zhuǎn)換比較麻煩,不如直接寫明。
使用的時候?qū)⑦@個腳本放在Editor文件下即可,需要提前打開VSCode,并且重新打開項目時,需要在ShaderEditor里面敲個空行重新編譯才能使用(作者也不知道為什么,只有腳本和Shader不能正常進(jìn)邏輯,其他類型都可以)。文章來源:http://www.zghlxwxcb.cn/news/detail-609594.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ShaderVS
{
[MenuItem("Tools/啟用VSCode編輯Shader文件")]
public static void OpenVSCode()
{
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
}
}
寫個Menu進(jìn)項目強(qiáng)制重新編譯下。文章來源地址http://www.zghlxwxcb.cn/news/detail-609594.html
到了這里,關(guān)于在Unity中編寫Shader的編譯器環(huán)境配置(支持CG和HLSL)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!