有些特殊后缀名的文件在unity里是不可识别的。如下图所示,这里我把文本的后缀改成了*.xx 这样unity就不认识了。那么双击就没反应了,我想做的就是在双击此类文件的时候指定一个应用程序打开它。
代码中我指定了用sublime来打开后缀是.xx的文件。
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"; 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就打开啦。