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' 生活在于学习,知识在于积累.
随机推荐
- 用户创建,删除and并发注册and系统登陆的API研究(学习汇总网上资料)
一.系统登陆链接实现 比如有一个外围支持系统,用户需要在外围系统登录之后点个link就可以登录到Oracle ERP系统中,那么我们需要先把外围系统的用户创建在Oracle ERP中,并且分配职责给他 ...
- For oracle databases, if the top showing the oracle database, then oracle process is using the top c
Note 805586.1 Troubleshooting Session Administration (Doc ID 805586.1)Note 822527.1 How To Find ...
- React native和原生之间的通信
RN中文网关于原生模块(Android)的介绍可以看到,RN前端与原生模块之 间通信,主要有三种方法: 1)使用回调函数Callback,它提供了一个函数来把返回值传回给JavaScript. 2)使 ...
- 开源协议介绍(GPL,LGPL,BSD,MIT,Apache)
http://blog.csdn.net/zhulinu/article/details/7419068 什么是许可协议? 什么是许可,当你为你的产品签发许可,你是在出让自己的权利,不过,你仍然拥 ...
- SYBASE的select into与insert into使用和区别
对于表的部分或全部字段的复制,Sybase数据库提供了两种方式:select into和insert into. select into: 语法:select value1, value2, val ...
- Runtime系列(一)-- 基础知识
众所周知,Objective-C 是一种运行时语言.运行时怎么来体现的呢?比如一个对象的类型确定,或者对象的方法实现的绑定都是推迟到软件的运行时才能确定的.而运行时的诸多特性都是由Runtime 来实 ...
- linux ubuntu系统下MySQL的安装及设置
debian下安装MySQL:1.构建源或使用光盘镜像,当然你插入光盘也没问题2.有源时本地文件的源配置:修改/etc/apt/sources.list文件, 示例:deb http://192.16 ...
- git提交代码到github
前言:转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52117504 git提交代码到github 命令汇总: git init git ...
- shell入门之流程控制语句
1.case 脚本: #!/bin/bash #a test about case case $1 in "lenve") echo "input lenve" ...
- quartz 时间设置(定时任务scheduler)
quartz用来设置定时任务的作业调度程序.在linux的crontab中用到. 格式为: * * * * * * * 其从左到右顺序代表 :[秒] [分] [小时] [日] [月] [周] [年] ...