猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/50595585
最近经常需要些一个编译工作脚本,经常操作一个文件。下面是一个汇总了的文件操作方法。
using UnityEngine;
#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目录下创建文件
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
}
}
/// 写入数据到对应文件
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 ();