UnityEditor下文件操作方法汇总(Unity3D开发之二十四)
猴子原创,欢迎转载。转载请注明: 转载自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 ();
}
/// 从对应文件读取数据
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
}
/// 复制文件
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));
}
/// 创建文件夹
public static void CreateFolder (string folderPath)
{
if (!IsFolderExists (folderPath)) {
Directory.CreateDirectory (GetFullPath (folderPath));
AssetDatabase.Refresh ();
}
}
/// 复制文件夹
public static void CopyFolder (string srcFolderPath, string destFolderPath)
{
#if !UNITY_WEBPLAYER
if (!IsFolderExists (srcFolderPath)) {
return;
}
CreateFolder (destFolderPath);
srcFolderPath = GetFullPath (srcFolderPath);
destFolderPath = GetFullPath (destFolderPath);
// 创建所有的对应目录
foreach (string dirPath in Directory.GetDirectories(srcFolderPath, "*", SearchOption.AllDirectories)) {
Directory.CreateDirectory (dirPath.Replace (srcFolderPath, destFolderPath));
}
// 复制原文件夹下所有内容到目标文件夹,直接覆盖
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下创建目录
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(全局唯一标识符)转换为对应的资源路径。
AssetDatabase.GUIDToAssetPath (guid);
}
offset = index + 1;
parentFolder = "Assets/" + assetFolderPath.Substring (0, offset - 1);
index = assetFolderPath.IndexOf ("/", index + 1);
}
AssetDatabase.Refresh ();
}
}
/// 复制Assets下内容
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下内容
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下文件操作方法汇总(Unity3D开发之二十四)的更多相关文章
- Java开发学习(二十四)----SpringMVC设置请求映射路径
一.环境准备 创建一个Web的Maven项目 参考Java开发学习(二十三)----SpringMVC入门案例.工作流程解析及设置bean加载控制中环境准备 pom.xml添加Spring依赖 < ...
- Auto Create Editable Copy Font(Unity3D开发之二十二)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/48318879 ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...
- Unity Singleton 单例类(Unity3D开发之二十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/47335197 ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...
- BizTalk开发系列(二十四) BizTalk项目框架建议
Asp.NET有MVC框架,大部份的开发都是按照MVC进行的.BizTalk是面向消息的开发,不能完全采用分层的开发模式.而微软只提供了 BizTalk项目开发的基本策略,通过分析相关的Complex ...
- 仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)
转载请说明原出处,谢谢~~ 花了两天时间把仿酷狗的选项设置窗体做出来了,当然了只是做了外观.现在开学了,写代码的时间减少,所以整个仿酷狗的工程开发速度减慢了.今天把仿酷狗的选项设置窗体的布局代码分享出 ...
- Android开发(二十四)——数据存储SharePreference、SQLite、File、ContentProvider
Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/data/程 ...
- 网站开发进阶(二十四)HTML颜色代码表
HTML颜色代码表 设置背景色:style='background-color:red' 设置字体颜色:style='color:red' 生活在于学习,知识在于积累.
随机推荐
- Scheme call/cc 研究
目前尚不清楚实质,但已经能够从形式上理解它的某些好处,有个很简单的连乘函数可以说明: 为了展示究竟发生了什么,我包装了下乘法函数,将其变为mul. 我们将比较product和xproduct的区别. ...
- Android 高级控件(七)——RecyclerView的方方面面
Android 高级控件(七)--RecyclerView的方方面面 RecyclerView出来很长时间了,相信大家都已经比较了解了,这里我把知识梳理一下,其实你把他看成一个升级版的ListView ...
- Team Foundation Server 2015 Update 2.1 发布日志
微软在 2016年5月5日发布了Visual Studio Team Foundation Server 2015 update 2.1. 下面我们来看看Update2.1中给我们带来了哪些新功能. ...
- 2.Cocos2d-x-3.2编写3d打飞机,项目代码总结
1.AppDelete中applicationDidFinishLaunching代码示范 2.当电话来了时,停止恢复游戏声音的代码(在AppDelegate中加入下面代码) boolAppDel ...
- 解决Xshell显示中文乱码的问题
执行echo $LANG命令输出的是当前的编码方式,执行locale命令得到系统中所有可用的编码方式.要让Xshell不显示乱码,则要将编码方式改为UTF-8. 在Xshell中[file]-> ...
- FFmpeg示例程序合集-Git批量获取脚本
此前做了一系列有关FFmpeg的示例程序,组成了<FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player: 最简单的 ...
- Java-IO之FileReader和FileWriter
FileReader是用于读取字符流的类,它继承于InputStreamReader,要读取原始字节流,考虑使用FileInputStream:FileWriter是用于写入字符流的类,继承于Outp ...
- 打Patch实践
一.找到相应PATCH 确认系统已安装模块版本. SELECTapp.application_short_name, app.application_name, pi.patch_level FR ...
- java方法重写和super关键字
//java方法重写和super关键字 //在继承中,其实就是子类定义了和父类同名的方法 //就是方法,属性都是相通的 //重写限制: //被子类重写的方法不能拥有比父类方法更加严格的权限 //sup ...
- Unity UGUI基础之Toggle
Toggle组合按钮(单选框),可以将多个Toggle按钮加入一个组,则他们之间只能有一个处于选中状态(Toggle组合不允许关闭的话). 一.Toggle组件: Toggle大部分属性等同于Butt ...