[轉(zhuǎn)]打開unity不可識別的文件

有些特殊后綴名的文件在unity里是不可識別的。如下圖所示,這里我把文本的后綴改成了*.xx 這樣unity就不認識了。那么雙擊就沒反應(yīng)了,我想做的就是在雙擊此類文件的時候指定一個應(yīng)用程序打開它。


代碼中我指定了用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就打開啦。

如果想直接定位在某一行,比如lua文件的某一行。 Windows下可以直接設(shè)置VS打開,但是MAC下沒有, 不過可以傳入文件路徑 和文件的 行數(shù)直接定位。

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
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容