Unity几个重要路径的区别及文件处理

一、Resources(只读)

• Resources文件夹下的资源无论使用与否都会被打包

• 资源会被压缩,转化成二进制

• 打包后文件夹下的资源只读

• 无法动态更改,无法做热更新

• 使用Resources.Load加载

二、 StreamingAssets(只读)

• 流数据的缓存目录

• 文件夹下的资源无论使用与否都会被打包

• 资源不会被压缩和加密

• 打包后文件夹下的资源只读,主要存放二进制文件

• 无法做热更新

• WWW类加载(一般用CreateFromFile ,若资源是AssetBundle,依据其打包方式看是否是压缩的来决定)

• 相对路径,具体路径依赖于实际平台

•Application.streamingAssetsPath

• IOS: Application.dataPath + “/Raw” 或Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw

 static void WriteLine()
        {
    
    
            StreamWriter writer;
            string filePath = "";
            filePath = Application.streamingAssetsPath + "/BuildJson.json";

            FileInfo fileInfo = new FileInfo(filePath);
            writer = new StreamWriter(filePath, false);
            writer.Write(JsonMapper.ToJson(buildJson));
            // 释放 流
            writer.Close();
            //writer.Flush();

        }

三、 Application.dataPath(只读)

• 游戏的数据文件夹的路径(例如在Editor中的Assets)

• 很少用到

• 无法做热更新

• IOS路径: Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data

四、Application.persistentDataPath(可读写)

• 持久化数据存储目录的路径( 沙盒目录,打包之前不存在 )

• 文件夹下的资源无论使用与否都会被打包

• 运行时有效,可读写

• 无内容限制,从StreamingAsset中读取二进制文件或从AssetBundle读取文件来写入PersistentDataPath中

• 适合热更新

• IOS路径: Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents

五、在unity代码中进行文件增删改查处理

 /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcPath">需要被拷贝的文件夹路径</param>
        /// <param name="tarPath">拷贝目标路径</param>
        private static void CopyFolder(string srcPath, string tarPath)
        {
    
    
            if (!Directory.Exists(srcPath))
            {
    
    
                Debug.Log("CopyFolder is null.");
                return;
            }

            if (!Directory.Exists(tarPath))
            {
    
    
                Directory.CreateDirectory(tarPath);//创建文件夹
            }

            //获得源文件下所有文件
            List<string> files = new List<string>(Directory.GetFiles(srcPath));
            files.ForEach(f =>
            {
    
    
                string destFile = Path.Combine(tarPath, Path.GetFileName(f));
                File.Copy(f, destFile, true); //覆盖模式
            });

            //获得源文件下所有目录文件
            List<string> folders = new List<string>(Directory.GetDirectories(srcPath));
            folders.ForEach(f =>
            {
    
    
                string destDir = Path.Combine(tarPath, Path.GetFileName(f));
                CopyFolder(f, destDir); //递归实现子文件夹拷贝
            });
        }

猜你喜欢

转载自blog.csdn.net/qq_43505432/article/details/120870580