0%

Shader Tools

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// ShaderOpenTools.cs
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Diagnostics;
using System.Text;
using UnityEditor.ProjectWindowCallback;
public partial class ShaderOpenTools : EditorWindow
{
const string k_WorkDirectoryKey = "Shader_WorkDirectoryKey";
static string m_keyPath = null;
static string keyPath{
get{
if(m_keyPath == null)
m_keyPath = $"{Application.dataPath}_{k_WorkDirectoryKey}";
return m_keyPath;
}
}
static string workDirectory
{
get
{
string path = EditorPrefs.GetString(keyPath, null);
if(string.IsNullOrEmpty(path))
return Application.dataPath;
return path;
}
set
{
EditorPrefs.SetString(keyPath, value);
}
}
[MenuItem("Tools/ShaderOpenTools")]
static void CreateWindow()
{
Rect rect = new Rect();
if(SceneView.lastActiveSceneView != null)
rect = SceneView.lastActiveSceneView.position;
rect.size = new Vector2(400, 200);
rect.position = rect.position + new Vector2(10, 10);
ShaderOpenTools win = GetWindowWithRect<ShaderOpenTools>(rect, true);
win.titleContent = new GUIContent("ShaderOpenTools Setting");
win.Show();
}

string path = null;
void OnEnable()
{
path = workDirectory;
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(path);
if(GUILaout.Button("o", GUILayout.Width(30)))
{
string temp = path;
temp = EditorUtility.OpenFolderPanel("select shader project", temp, Application.dataPath);
if(!string.IsNullOrEmpty(temp) && path != temp)
{
path = temp;
workDirectory = path;
}
}
EditorGUILayout.EndHorizontal();

GUILayout.FlexibleSpace();
if(GUILayout.Button("Open Project"))
{
OpenProject();
}
}
}

public partial class ShaderOpenTools
{
static ProcessStartInfo CreateProcessStartInfo(string rootDirectory)
{
ProcessStartInfo start = new ProcessStartInfo("cmd.exe");
start.CreateNoWindow = true;
start.ErrorDialog = true;
start.UseShellExecute = false;
start.WorkingDirectory = rootDirectory;

start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.RedirectStandardInput = true;
start.StandardOutputEncoding = Encoding.GetEncoding("GBK");
start.StandardErrorEncoding = Encoding.GetEncoding("GBK");
return start;
}
[OnOpenAsset(-100)]
static bool OnShaderClick(int instanceID, int line)
{
string strFilePath = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
if(strFilePath.EndsWith(".shader")
|| strFilePath.EndsWith(".hlsl")
|| strFilePath.EndsWith(".cginc"))
{
string rootDirectory = workDirectory;
string strFileName = Path.GetFullPath(strFilePath);
ProcessStartInfo start = CreateProcessStartInfo(rootDirectory);
Process process = Process.Start(start);
using(var sw = process.StandardInput)
{
if(line > 0)
{
sw.WriteLine($"code . && code -g \"{strFileName}\":{line} -r");
}
else
{
sw.WriteLine($"code . && code \"{strFileName}\" -r");
}
}
bool rst = true;
using(var sr = process.StandardError)
{
string error;
do
{
error = sr.ReadLine();
if(!string.IsNullOrEmpty(error))
{
rst = false;
}
}
while(error != null);
}
process.Close();
return rst;
}
return false;
}
static void OpenProject()
{
string rootDirectory = workDirectory;
ProcessStartInfo start = CreateProcessStartInfo(rootDirectory);
Process process = Process.Start(start);
using(var sw = process.StandardInput)
{
sw.WroteLine($"code .");
}
using(var sr = process.StandardError)
{
string error;
do
{
error = sr.ReadLine();
}
while(error!=null);
}
process.Close();
}
}

public partial class ShaderOpenTools
{
[SuppressMessage("Microsft.Performance", "CA1812")]
internal class CreateHLSLAsset : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
FileInfo fileInfo = new FileInfo(pathName);
string fileName = Path.GetFileNameWithoutExtension(pathName);
using(var stream = fileInfo.CreateText())
{
string define = ObjectNames.NicifyVariableName(fileName)
.Replace(" ", "_").ToUpper();
stream.WriteLine($"#ifndef _{define}_INCLUDE");
stream.WriteLine($"#define _{define}_INCLUDE");
stream.WriteLine("\t");
stream.WriteLine($"#endif //_{define}_INCLUDE");
}
AssetDatabase.Refresh();
}
}
[MenuItem("Assets/Create/Shader/HLSL")]
static void CreateHLSL()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
0,CreateInstance<CreateHLSLAsset>(), "NewHLSL.hlsl", null, null);
}
}

参考

VSCode Command line
VSCode settings
VSCode GlobPattern:可以用于设置VSCode不显示.meta文件,在Settings界面,搜索file:exclude,然后增加**/*.meta

使用配置文件

上面是通过路径打开VSCode文件,更实用的一种方法是直接通过配置文件打开。通过配置文件打开有几个好处,就是可以在同一个VSCode里面打开多个文件路径,并且保存设置。配置文件格式如下,文件名为shaderworkspace.code-workspace

1
2
3
4
5
6
7
8
9
10
11
{
"folder":[
{"path":"folder\\path1"},
{"path":"folder\\path2"}
],
"settings":{
"files.exclude":{
"**/*.meta":true
}
}
}

然后前面打开VSCode工程的命令可以改成:

1
2
3
4
5
6
7
8
if(line > 0)
{
sw.WriteLine($"code shaderworkspace.code-workspace && code -g \"{strFileName}\":{line} -r");
}
else
{
sw.WriteLine($"code shaderworkspace.code-workspace && code \"{strFileName}\" -r");
}

当然,上面命令有效的前提是shaderworkspace.code-workspace文件在当前工作目录下。