在unity开发时,我们通常需要编辑不同的脚本(C#、Lua、XML、Shader...),而unity通常打开所有二级制文本文件都是要么MONO要么VS,下面将以shader为例改变shader文件的默认打开方式为VS Code。
1.首先我我们在editor下添加以下脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class ShaderEditor : MonoBehaviour
{
public static bool step1(int instanceID, int line)
{
return false;
}
[UnityEditor.Callbacks.OnOpenAssetAttribute(2)]
public static bool step2(int instanceID, int line)
{
string strFilePath = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
string strFileName = System.IO.Directory.GetParent(Application.dataPath) + "/" + strFilePath;
if (strFileName.EndsWith(".shader")) //文件扩展名类型
{
string editorPath = Environment.GetEnvironmentVariable("VSCode_Path");//环境变量名
if (editorPath != null && editorPath.Length > 0)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = editorPath + (editorPath.EndsWith("/") ? "" : "/") + "Code.exe"; //你的文件名字
startInfo.Arguments = "\"" + strFileName + "\"";
process.StartInfo = startInfo;
process.Start();
return true;
}
else
{
Debug.Log("null environment : VSCode_Path");
return false;
}
}
return false;
}
}
其实就是通过环境变量获取exe的路径,然后修改process的startinfo
2.在环境变量中,将VSCode_Path添加进去(如果不使用环境变量,直接在代码里面写死就行)
3.重启unity,双击shader,以VScode打开了要编辑的shader,另外,用这个vscode扩展敲shader有提示哦
用此方法可以修改任意unity asset面板中资源的打开方式,是不是很方便