#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
UnityEditor下文件操作方法匯總
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門仁锯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耀找,“玉大人,你說我怎么就攤上這事业崖∫懊ⅲ” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵腻要,是天一觀的道長复罐。 經(jīng)常有香客問我,道長雄家,這世上最難降的妖魔是什么效诅? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮趟济,結(jié)果婚禮上乱投,老公的妹妹穿的比我還像新娘。我一直安慰自己顷编,他們只是感情好戚炫,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著媳纬,像睡著了一般双肤。 火紅的嫁衣襯著肌膚如雪施掏。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼耙箍,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了酥馍?” 一聲冷哼從身側(cè)響起辩昆,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎物喷,沒想到半個(gè)月后卤材,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡峦失,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了术吗。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尉辑。...
- 正文 年R本政府宣布购啄,位于F島的核電站,受9級(jí)特大地震影響嘱么,放射性物質(zhì)發(fā)生泄漏狮含。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一曼振、第九天 我趴在偏房一處隱蔽的房頂上張望几迄。 院中可真熱鬧,春花似錦冰评、人聲如沸映胁。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽解孙。三九已至坑填,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間弛姜,已是汗流浹背穷遂。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像中剩,于是被迫代替她去往敵國和親忌穿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 一盯拱、 matlab對(duì)路徑的操作 filesep用于返回當(dāng)前平臺(tái)的目錄分隔符,Windows是反斜杠()例嘱,Linux...
- 出差回到家实柠,看到老婆一絲不掛地坐在床上,我連忙問道:“你咋光著身子呢善涨?”她說沒內(nèi)衣穿了窒盐。于是我捅開天花板草则,好多內(nèi)衣...