猴子原创,欢迎转载。转载请注明: 转载自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开发之二十四)的更多相关文章

  1. Java开发学习(二十四)----SpringMVC设置请求映射路径

    一.环境准备 创建一个Web的Maven项目 参考Java开发学习(二十三)----SpringMVC入门案例.工作流程解析及设置bean加载控制中环境准备 pom.xml添加Spring依赖 < ...

  2. Auto Create Editable Copy Font(Unity3D开发之二十二)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/48318879 ...

  3. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...

  4. Unity Singleton 单例类(Unity3D开发之二十)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/47335197 ...

  5. 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...

  6. BizTalk开发系列(二十四) BizTalk项目框架建议

    Asp.NET有MVC框架,大部份的开发都是按照MVC进行的.BizTalk是面向消息的开发,不能完全采用分层的开发模式.而微软只提供了 BizTalk项目开发的基本策略,通过分析相关的Complex ...

  7. 仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)

    转载请说明原出处,谢谢~~ 花了两天时间把仿酷狗的选项设置窗体做出来了,当然了只是做了外观.现在开学了,写代码的时间减少,所以整个仿酷狗的工程开发速度减慢了.今天把仿酷狗的选项设置窗体的布局代码分享出 ...

  8. Android开发(二十四)——数据存储SharePreference、SQLite、File、ContentProvider

    Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/data/程 ...

  9. 网站开发进阶(二十四)HTML颜色代码表

    HTML颜色代码表 设置背景色:style='background-color:red' 设置字体颜色:style='color:red' 生活在于学习,知识在于积累.

随机推荐

  1. spark下使用submit提交任务后报jar包已存在错误

    使用spark submit进行任务提交,离线跑数据,提交后的一段时间内可以application可以正常运行.过了一段时间后,就抛出以下错误: org.apache.spark.SparkExcep ...

  2. Java的LinkedList详解,看源码之后的总结

    1. LinkedList实现了一个带表头的双向循环链表: 2. LinkedList是线程不同步的: 3. LinkedList中实现了push.pop.peek.empty等方法,因此Linked ...

  3. 1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览

     1 新建项目 A  修改pro中的内容如下: HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets prints ...

  4. Linux下yum安装MySQL yum安装MySQL指定版本

    yum安装MySQL 1. 查看有没有安装过     yum list installed MySQL* (有存在要卸载yum remove MySQL*)     rpm -qa | grep my ...

  5. Android简易实战教程--第六话《开发一键锁屏应用2·完成》

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51885687点击打开链接 上一篇,初步开发了这个应用,功能都有了(见http:// ...

  6. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...

  7. XMLTABLE

     XMLTABLE Syntax Description of the illustration xmltable.gif XMLnamespaces_clause::= Description ...

  8. Java正则表达式小记

    http://blog.csdn.net/pipisorry/article/details/51059500 正则表达式的一般规则都一样,见[python正则表达式] java正则表达式中的特殊字符 ...

  9. Dynamics CRM2011/2013 删除个人视图

    这里以2013为例,2011同理.个人视图的功能很人性化,可以设置自己常看数据列表形式而不会去影响别人,但创建容易怎么删除还真不一定能找得到地,具体见下方截图.

  10. 网站开发进阶(三十八)Web前端开发规范文档你需要知道的事

    Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...