UnityEditor下文件操作方法匯總

#if UNITY_EDITOR
using UnityEditor;
using System;
using System.IO;
using System.Threading;
 
public static class FileStaticAPI
{
    /// 檢測文件是否存在Application.dataPath目錄
    public static bool IsFileExists (string fileName)
    {
        if (fileName.Equals (string.Empty)) {
            return false;
        }
 
        return File.Exists (GetFullPath (fileName));
    }
 
    /// 在Application.dataPath目錄下創(chuàng)建文件
    public static void CreateFile (string fileName)
    {
        if (!IsFileExists (fileName)) {
            CreateFolder (fileName.Substring (0, fileName.LastIndexOf ('/')));
 
#if UNITY_4 || UNITY_5
            FileStream stream = File.Create (GetFullPath (fileName));
            stream.Close ();
#else
            File.Create (GetFullPath (fileName));
#endif
        }
 
    }
 
    /// 寫入數(shù)據(jù)到對(duì)應(yīng)文件
    public static void Write (string fileName, string contents)
    {
        CreateFolder (fileName.Substring (0, fileName.LastIndexOf ('/')));
 
        TextWriter tw = new StreamWriter (GetFullPath (fileName), false);
        tw.Write (contents);
        tw.Close (); 
 
        AssetDatabase.Refresh ();
    }
 
    /// 從對(duì)應(yīng)文件讀取數(shù)據(jù)
    public static string Read (string fileName)
    {
#if !UNITY_WEBPLAYER
        if (IsFileExists (fileName)) {
            return File.ReadAllText (GetFullPath (fileName));
        } else {
            return "";
        }
#endif
 
#if UNITY_WEBPLAYER
        Debug.LogWarning("FileStaticAPI::CopyFolder is innored under wep player platfrom");
#endif
    }
 
    /// 復(fù)制文件
    public static void CopyFile (string srcFileName, string destFileName)
    {
        if (IsFileExists (srcFileName) && !srcFileName.Equals (destFileName)) {
            int index = destFileName.LastIndexOf ("/");
            string filePath = string.Empty;
 
            if (index != -1) {
                filePath = destFileName.Substring (0, index);
            }
 
            if (!Directory.Exists (GetFullPath (filePath))) {
                Directory.CreateDirectory (GetFullPath (filePath));
            }
 
            File.Copy (GetFullPath (srcFileName), GetFullPath (destFileName), true);
 
            AssetDatabase.Refresh ();
        }
    }
 
    /// 刪除文件
    public static void DeleteFile (string fileName)
    {
        if (IsFileExists (fileName)) {
            File.Delete (GetFullPath (fileName));
 
            AssetDatabase.Refresh ();
        }
    }
 
    /// 檢測是否存在文件夾
    public static bool IsFolderExists (string folderPath)
    {
        if (folderPath.Equals (string.Empty)) {
            return false;
        }
 
        return Directory.Exists (GetFullPath (folderPath));
    }
 
    /// 創(chuàng)建文件夾
    public static void CreateFolder (string folderPath)
    {
        if (!IsFolderExists (folderPath)) {
            Directory.CreateDirectory (GetFullPath (folderPath));
 
            AssetDatabase.Refresh ();
        }
    }
 
    /// 復(fù)制文件夾
    public static void CopyFolder (string srcFolderPath, string destFolderPath)
    {
 
#if !UNITY_WEBPLAYER
        if (!IsFolderExists (srcFolderPath)) {
            return;
        }
 
        CreateFolder (destFolderPath);
 
 
        srcFolderPath = GetFullPath (srcFolderPath);
        destFolderPath = GetFullPath (destFolderPath);
 
        // 創(chuàng)建所有的對(duì)應(yīng)目錄
        foreach (string dirPath in Directory.GetDirectories(srcFolderPath, "*", SearchOption.AllDirectories)) {
            Directory.CreateDirectory (dirPath.Replace (srcFolderPath, destFolderPath));
        }
 
        // 復(fù)制原文件夾下所有內(nèi)容到目標(biāo)文件夾驶沼,直接覆蓋
        foreach (string newPath in Directory.GetFiles(srcFolderPath, "*.*", SearchOption.AllDirectories)) {
 
            File.Copy (newPath, newPath.Replace (srcFolderPath, destFolderPath), true);
        }
 
        AssetDatabase.Refresh ();
#endif
 
#if UNITY_WEBPLAYER
        Debug.LogWarning("FileStaticAPI::CopyFolder is innored under wep player platfrom");
#endif
    }
 
    /// 刪除文件夾
    public static void DeleteFolder (string folderPath)
    {
        #if !UNITY_WEBPLAYER
        if (IsFolderExists (folderPath)) {
 
            Directory.Delete (GetFullPath (folderPath), true);
 
            AssetDatabase.Refresh ();
        }
        #endif
 
        #if UNITY_WEBPLAYER
        Debug.LogWarning("FileStaticAPI::DeleteFolder is innored under wep player platfrom");
        #endif
    }
 
    /// 返回Application.dataPath下完整目錄
    private static string GetFullPath (string srcName)
    {
        if (srcName.Equals (string.Empty)) {
            return Application.dataPath;
        }
 
        if (srcName [0].Equals ('/')) {
            srcName.Remove (0, 1);
        }
 
        return Application.dataPath + "/" + srcName;
    }
 
    /// 在Assets下創(chuàng)建目錄
    public static void CreateAssetFolder (string assetFolderPath)
    {
        if (!IsFolderExists (assetFolderPath)) {
            int index = assetFolderPath.IndexOf ("/");
            int offset = 0;
            string parentFolder = "Assets";
            while (index != -1) {
                if (!Directory.Exists (GetFullPath (assetFolderPath.Substring (0, index)))) {
                    string guid = AssetDatabase.CreateFolder (parentFolder, assetFolderPath.Substring (offset, index - offset));
                    // 將GUID(全局唯一標(biāo)識(shí)符)轉(zhuǎn)換為對(duì)應(yīng)的資源路徑鸵闪。
                    AssetDatabase.GUIDToAssetPath (guid);
                }
                offset = index + 1;
                parentFolder = "Assets/" + assetFolderPath.Substring (0, offset - 1);
                index = assetFolderPath.IndexOf ("/", index + 1);
            }
 
            AssetDatabase.Refresh ();
        }
    }
 
    /// 復(fù)制Assets下內(nèi)容
    public static void CopyAsset (string srcAssetName, string destAssetName)
    {
        if (IsFileExists (srcAssetName) && !srcAssetName.Equals (destAssetName)) {
            int index = destAssetName.LastIndexOf ("/");
            string filePath = string.Empty;
 
            if (index != -1) {
                filePath = destAssetName.Substring (0, index + 1);
                //Create asset folder if needed
                CreateAssetFolder (filePath);
            }
 
 
            AssetDatabase.CopyAsset (GetFullAssetPath (srcAssetName), GetFullAssetPath (destAssetName));
            AssetDatabase.Refresh ();
        }
    }
 
    /// 刪除Assets下內(nèi)容
    public static void DeleteAsset (string assetName)
    {
        if (IsFileExists (assetName)) {
            AssetDatabase.DeleteAsset (GetFullAssetPath (assetName));           
            AssetDatabase.Refresh ();
        }
    }
 
    /// 獲取Assets下完整路徑
    private static string GetFullAssetPath (string assetName)
    {
        if (assetName.Equals (string.Empty)) {
            return "Assets/";
        }
 
        if (assetName [0].Equals ('/')) {
            assetName.Remove (0, 1);
        }
 
        return "Assets/" + assetName;
    }
}
 
#endif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蹂午,一起剝皮案震驚了整個(gè)濱河市削祈,隨后出現(xiàn)的幾起案子缨伊,更是在濱河造成了極大的恐慌遗嗽,老刑警劉巖志鞍,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異家乘,居然都是意外死亡蝗羊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門仁锯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耀找,“玉大人,你說我怎么就攤上這事业崖∫懊ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵腻要,是天一觀的道長复罐。 經(jīng)常有香客問我,道長雄家,這世上最難降的妖魔是什么效诅? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮趟济,結(jié)果婚禮上乱投,老公的妹妹穿的比我還像新娘。我一直安慰自己顷编,他們只是感情好戚炫,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著媳纬,像睡著了一般双肤。 火紅的嫁衣襯著肌膚如雪施掏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天茅糜,我揣著相機(jī)與錄音七芭,去河邊找鬼。 笑死蔑赘,一個(gè)胖子當(dāng)著我的面吹牛狸驳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播缩赛,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼耙箍,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了酥馍?” 一聲冷哼從身側(cè)響起辩昆,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎物喷,沒想到半個(gè)月后卤材,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡峦失,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了术吗。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尉辑。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖较屿,靈堂內(nèi)的尸體忽然破棺而出隧魄,到底是詐尸還是另有隱情,我是刑警寧澤隘蝎,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布购啄,位于F島的核電站,受9級(jí)特大地震影響嘱么,放射性物質(zhì)發(fā)生泄漏狮含。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一曼振、第九天 我趴在偏房一處隱蔽的房頂上張望几迄。 院中可真熱鬧,春花似錦冰评、人聲如沸映胁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽解孙。三九已至坑填,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間弛姜,已是汗流浹背穷遂。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留娱据,地道東北人蚪黑。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像中剩,于是被迫代替她去往敵國和親忌穿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容