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' 生活在于学习,知识在于积累.
随机推荐
- Web Service进阶(三)HTTP-GET, HTTP-POST and SOAP的比较
XML Web Service支持三种协议来与用户交流数据.这三种协议分别是: 1.SOAP:Simple Object Access Protocol 2.HTTP-GET 3.HTTP-POST ...
- GitHub无法访问或访问缓慢解决办法
缘由 由于众所周知的原因,Github最近无法访问或访问很慢.由于Github支持https,因此此次屏蔽Github采用的方法是dns污染,用户访问github会返回一个错误的IPFQ当然是一种解决 ...
- FFmpeg与libx264接口源代码简单分析
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- JAVA面向对象-----接口的概述
接口的概述 **接口(interface):**usb接口,主要是使用来拓展笔记本的功能,那么在java中的接口主要是使用来拓展定义类的功能,可以弥补java中单继承的缺点. class Pencil ...
- 简单搭建iOS开发项目框架
今天我们来谈谈如何搭建框架,框架需要做一些什么. 第一步:找到我们的目标我们的目标是让其他开发人员拿到手后即可写页面,不再需要考虑其他的问题. 第二步:我们需要做哪些东西各位跟着我一步一步来进行. 假 ...
- 剑指offer面试题3 二维数组中的查找 (java)
注:java主要可以利用字符串的length方法求出长度解决这个问题带来方便 public class FindNum { public static void main(String[] args) ...
- UILabel设定行间距方法
NSString *textStr = @"iPhone规定:任何应用想访问麦克风,必须被授权麦克风服务.请进入"设置"->"隐私"->& ...
- Java基础---Java---IO流-----读取键盘录入、InputStreamReader、转换流、OutputStreamWriter、InputStreamReader
字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream Buffe ...
- scala学习笔记5 (隐式转化/参数/类)
隐式转化: 隐式参数: 隐式类:
- C语言实现4种常用排序
实在没事搞,反正面试也要用到,继续来写4种排序算法.因为那天用java写了排序,突然想到我是要面试IOS,起码也得用C写.C竟然忘干净了,方法都不会写了.囧啊! 下面用C实现4种排序算法:快速排序.冒 ...