猴子原创,欢迎转载。转载请注明: 转载自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. 剑指Offer——完美+今日头条笔试题+知识点总结

    剑指Offer--完美+今日头条笔试题+知识点总结 情景回顾 时间:2016.9.28 16:00-18:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:完美世界笔试 ...

  2. K均值聚类的失效性分析

    K均值聚类是一种应用广泛的聚类技术,特别是它不依赖于任何对数据所做的假设,比如说,给定一个数据集合及对应的类数目,就可以运用K均值方法,通过最小化均方误差,来进行聚类分析. 因此,K均值实际上是一个最 ...

  3. Android动态换肤(三、安装主题apk方式)

    相比之前免安装的方式,这种方法需要用户下载并安装皮肤apk,程序写起来比免安装的要简单很多,像很多系统主题就是通过这种方式实现的. 这种方式的思路是,从所有已安装的应用程序中遍历出皮肤程序(根据特定包 ...

  4. JDBC存储和读取二进制数据

    以下JSP文件用common-fileupload组件实现文件上传,并将文件以二进制文件的形式存入数据库 <% if("POST".equalsIgnoreCase(requ ...

  5. RecyclerView下拉刷新上拉加载(三)—对Adapter的封装

    RecyclerView下拉刷新上拉加载(一) http://blog.csdn.net/baiyuliang2013/article/details/51506036 RecyclerView下拉刷 ...

  6. Dynamics CRM 2015Online Update1 new feature之表单页Tabs切换

    CRM2011的界面相对于CRM4.0进行了比较大的改动,N久没见过4.0的界面了所以忘了表单是什么样子的了,但2011的表单中若含有多个tab的话,是可以通过左侧栏进行切换,话说2013的界面相对2 ...

  7. 【一天一道LeetCode】#206. Reverse Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

  8. 【一天一道LeetCode】#242. Valid Anagram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  9. UNIX网络编程——TCP输出,UDP输出

    TCP输出 每一个TCP套接字有一个发送缓冲区,我们可以使用SO_SNDBUF套接字选项来更改该缓冲区的大小.当某个应用进程调用write时,内核从该应用进程的缓冲区中复制所有数据到(或是应用程序的缓 ...

  10. 【Android 应用开发】 Android APK 反编译 混淆 反编译后重编译

    反编译工具 : 总结了一下 linux, windows, mac 上的版本, 一起放到 CSDN 上下载; -- CSDN 下载地址 : http://download.csdn.net/detai ...