Unity 文件复制

2 篇文章 0 订阅

Unity中实验文件复制(C#文件复制)

递归实现

/// <summary>
/// 拷贝文件夹到指定文件夹并更改文件夹名称
/// </summary>
/// <param name="SourcePath">源文件夹</param>
/// <param name="CopyToPathPath">目标文件夹+文件夹名称</param>
public static void CopyDir(string SourcePath, string CopyToPathPath)
{
    try
    {
        // 检查目标目录是否以目录分割字符结束如果不是则添加
        if (CopyToPathPath[CopyToPathPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
        {
            CopyToPathPath += System.IO.Path.DirectorySeparatorChar;
        }
        // 判断目标目录是否存在如果不存在则新建
        if (!System.IO.Directory.Exists(CopyToPathPath))
        {
            System.IO.Directory.CreateDirectory(CopyToPathPath);
        }
        // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
        // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
        //string[] fileList = Directory.GetFiles(srcPath);
        string[] fileList = Directory.GetFileSystemEntries(SourcePath);
        // 遍历所有的文件和目录
        foreach (string file in fileList)
        {
            // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
            if (System.IO.Directory.Exists(file))
            {
                CopyDir(file, CopyToPathPath + System.IO.Path.GetFileName(file));
            }
            // 否则直接Copy文件
            else
            {
                if (file.EndsWith(".meta") || file.EndsWith(".bak")) 
                {
                    continue;
                }
                System.IO.File.Copy(file, CopyToPathPath + System.IO.Path.GetFileName(file), true);
            }
        }
    }
    catch (Exception e)
    {
        Debug.Log(e);
    }
}

非递归实现

public static void CopyEntireDir(string SourcePath, string CopyToPath)
{
    //Now Create all of the directories
    var path = Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories);

    foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(SourcePath, CopyToPath));
    }

    //Copy all the files & Replaces any files with the same name
    foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
    {
        if (newPath.EndsWith(".meta") || newPath.EndsWith(".bak")) 
        {
            continue;
        }
        File.Copy(newPath, newPath.Replace(SourcePath, CopyToPath), true);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Unity中解压Android文件,可以使用Unity的AssetBundle功能来实现。 首先,在Unity的AssetBundle窗口中创建一个新的Asset Bundle,命名为"android_files"。 然后,在Unity中创建一个脚本,用于解压Android文件。以下是一个示例脚本: ```csharp using UnityEngine; using System.Collections; using System.IO; public class AndroidFileExtractor : MonoBehaviour { void Start () { // 检查Android路径 string path = Path.Combine(Application.streamingAssetsPath, "android_files"); // 如果在Android设备上 if(Application.platform == RuntimePlatform.Android){ // 从StreamingAssets目录拷贝文件到持久化数据目录 WWW www = new WWW(path); while(!www.isDone) {} // 解压文件 ExtractFile(www.bytes); } } // 解压文件 void ExtractFile(byte[] assetBytes){ string outputPath = Application.persistentDataPath + "/files"; // 创建输出路径 if (!Directory.Exists(outputPath)){ Directory.CreateDirectory(outputPath); } // 解压文件 string zipFilePath = Path.Combine(outputPath, "android_files.zip"); File.WriteAllBytes(zipFilePath, assetBytes); // 解压文件到输出路径 ZipUtils.UnzipFile(zipFilePath, outputPath); // 删除解压后的zip文件 File.Delete(zipFilePath); } } ``` 请注意,上述脚本中的`ZipUtils`是一个自定义工具类,用于解压ZIP文件。你可以根据自己的需求选择使用已有的ZIP解压工具类,或自己实现解压功能。 最后,在Unity中将该脚本附加到一个GameObject上,然后在Android设备上运行游戏。这样,Android文件将会被解压到应用程序的持久化数据路径中的"files"文件夹中。 希望以上信息对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DannySmith

创作不易,蟹蟹支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值