有些特殊后缀名的文件在unity里是不可识别的。如下图所示,这里我把文本的后缀改成了*.xx 这样unity就不认识了。那么双击就没反应了,我想做的就是在双击此类文件的时候指定一个应用程序打开它。
代码中我指定了用sublime来打开后缀是.xx的文件。
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
|
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
public class MyAssetHandler
{
[OnOpenAssetAttribute(1)]
public static bool step1(int instanceID, int line)
{
//string name = EditorUtility.InstanceIDToObject(instanceID).name;
// Debug.Log("Open Asset step: 1 (" + name + ")");
return false; // we did not handle the open
}
// step2 has an attribute with index 2, so will be called after step1
[OnOpenAssetAttribute(2)]
public static bool step2(int instanceID, int line)
{
string path = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
string name = Application.dataPath + "/" + path.Replace("Assets/", "");
if (name.EndsWith(".xx"))
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "D:/Program Files/Sublime Text 3/sublime_text.exe";
//改成能够打开.lua文件的IDE工具即可
startInfo.Arguments = name;
process.StartInfo = startInfo;
process.Start();
return true;
}
// Debug.Log("Open Asset step: 2 (" + name + ")");
return false; // we did not handle the open
}
}
|
这样就OK啦。我在双击的时候sublime就打开啦。
如果想直接定位在某一行,比如lua文件的某一行。 Windows下可以直接设置VS打开,但是MAC下没有, 不过可以传入文件路径 和文件的 行数直接定位。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
static bool OpenFileAtLineExternal(string fileName, int line)
{
#if UNITY_EDITOR_OSX
string sublimePath = @"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl";
if(File.Exists(sublimePath)){
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName =sublimePath;
proc.StartInfo.Arguments = string.Format("{0}:{1}:0",fileName,line);
proc.Start();
return true;
}else{
return InternalEditorUtility.OpenFileAtLineExternal(fileName, line);
}
#else
return InternalEditorUtility.OpenFileAtLineExternal(fileName, line);
#endif
}
|