unity 在移动平台中,文件操作路径详解

转载于http://www.unitymanual.com/thread-23491-1-1.html
一.在项目根目录中创建Resources文件夹来保存文件。
可以使用Resources.Load(“文件名字,注:不包括文件后缀名”);把文件夹中的对象加载出来。
注:此方可实现对文件实施“增删查改”等操作,但打包后不可以更改了。

二.直接放在项目根路径下来保存文件
在直接使用Application.dataPath来读取文件进行操作。
注:移动端是没有访问权限的。

三.在项目根目录中创建StreamingAssets文件夹来保存文件。
1.可使用Application.dataPath来读取文件进行操作。

# if UNITYEDITOR
string filepath = Application.dataPath +"/StreamingAssets"+"/my.xml";
#elif UNITY_IPHONE
 string filepath = Application.dataPath +"/Raw"+"/my.xml";
#elif UNITY_ANDROID
 string filepath = "jar:file://" + Application.dataPath + "!/assets/"+"/my.xml;
#endif

2.直接使用Application.streamingAssetsPath来读取文件进行操作。
注:此方法在pc/Mac电脑中可实现对文件实施“增删查改”等操作,但在移动端只支持读取操作。

四.使用Application.persistentDataPath来操作文件(荐)
该文件存在手机沙盒中,因为不能直接存放文件,
1.通过服务器直接下载保存到该位置,也可以通过Md5码比对下载更新新的资源
2.没有服务器的,只有间接通过文件流的方式从本地读取并写入Application.persistentDataPath文件下,然后再通过Application.persistentDataPath来读取操作。
注:在Pc/Mac电脑 以及Android跟Ipad、ipone都可对文件进行任意操作,另外在iOS上该目录下的东西可以被iCloud自动备份。
五.使用Application.temporaryCachePath来操作文件

操作方式跟上面Application.persistentDataPath类似。除了在ios上不能被iCloud自动备份。

最近使用unity读取各个平台路径,特别对移动平台路径的处理
简直是各种蛋疼 各种坑
在此与大家分享下
IOS:
Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches

android:
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath : /data/data/xxx.xxx.xxx/cache

Windows Web Player:
Application.dataPath : file:///D:/MyGame/WebPlayer (即导包后保存的文件夹,html文件所在文件夹)
Application.streamingAssetsPath :
Application.persistentDataPath :
Application.temporaryCachePath :

注意:
Application.persistentDataPath 才是移动端可用的保存生成文件的地方
放到resource中打包后不可以更改了
放到Application .dataPath中移动端是没有访问权限的

===================================================
如何查看iPhone文件存放目录?首先需要越狱,越狱后打开iPhone手机目录,找到/Applications目录下就是iPhone所有软件目录。

iPhone文件目录介绍

1、/Applications

常用软件的安装目录。

  1. /private ar/ mobile/Media /iphone video Recorder

iphone video Recorder录像文件存放目录。

  1. /private ar/ mobile/Media /DCIM

相机拍摄的照片文件存放目录。

4、/privatear/ mobile /Media/iTunes_Control/Music

iTunes上传的多媒体文件(例如MP3、MP4等)存放目录,文件没有被修改,但是文件名字被修改了,直接下载到电脑即可读取。

5、/private ar/root/Media/EBooks

熊猫看书存放目录。

6、/Library/Ringtones

系统自带的来电铃声存放目录。

7、/System/Library/Audio/UISounds

短信记其它系统默认效果铃声(m4r铃声文件改扩展名为.caf)短信铃声文件名为sms-received开头的caf文件。

8、/privatear/ mobile /Library/AddressBook

系统电话本的存放目录。

/privatear/mobile/Library/SMS

短信存放目录

9、/private ar/ mobile/Media /iphone Recorder

iPhone Recorder录音软件文件存放目录

10、/Applications/Preferences.app/zh_CN.lproj

软件Preferences.app的中文汉化文件存放目录

11、/Library/Wallpaper

系统q1ang纸的存放目录

12、/System/Library/Audio/UISounds

系统声音文件的存放目录

13、/privatear/root/Media/PXL

ibrickr上传安装程序建立的一个数据库,估计和windows的uninstall记录差不多。

14、/bin

Linux系统差不多,是系统执行指令的存放目录。

15、/privatear/ mobile /Library/SMS

系统短信的存放目录

16、/privatear/run

系统进程运行的临时目录?(查看这里可以看到系统启动的所有进程)

17、/privatear/logs/CrashReporter

系统错误记录报。

对文件的操作类,主要就是文流读取操作的一些东西(包括Assetbundle)FileHelper.cs

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
/// <summary>
/// 使用Application.persistentDataPath方式来创建文件,读写Xml文件.
/// 注Application.persistentDataPath末尾没有“/”符号
/// </summary>
public class FileHelper: MonoBehaviour
{
/// <summary>
/// 动态创建文件夹.
/// </summary>
/// <returns>The folder.</returns>
/// <param name="path">文件创建目录.</param>
/// <param name="FolderName">文件夹名(不带符号).</param>
public string CreateFolder(string path,string FolderName)
{
string FolderPath = path+FolderName;
if(!Directory.Exists(FolderPath))
{ 
Directory.CreateDirectory(FolderPath);
} 
return FolderPath;
}

/// <summary>
/// 创建文件.
/// </summary>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">文件的名称.</param>
/// <param name="info">写入的内容.</param>
public void CreateFile(string path,string name,string info)
{
//文件流信息
StreamWriter sw;
FileInfo t = new FileInfo(path+name);
if(!t.Exists)
{
//如果此文件不存在则创建
sw = t.CreateText();
}
else
{
//如果此文件存在则打开
sw = t.AppendText();
}
//以行的形式写入信息
sw.WriteLine(info);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
} 

/// <summary>
/// 读取文件.
/// </summary>
/// <returns>The file.</returns>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">读取文件的名称.</param>
public ArrayList LoadFile(string path,string name)
{
//使用流的形式读取
StreamReader sr =null;
try{
sr = File.OpenText(path+name);
}catch(Exception e)
{
//路径与名称未找到文件则直接返回空
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
//一行一行的读取
//将每一行的内容存入数组链表容器中
arrlist.Add(line);
}
//关闭流
sr.Close();
//销毁流
sr.Dispose();
//将数组链表容器返回
return arrlist;
} 
//写入模型到本地
IEnumerator loadassetbundle(string url)
{
WWW w = new WWW(url);
yield return w;
if (w.isDone)
{
byte[] model = w.bytes;
int length = model.Length;
//写入模型到本地
CreateassetbundleFile(Application.persistentDataPath, "Model.assetbundle", model,length);
}
}
/// <summary>
/// 获取文件下所有文件大小
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public int GetAllFileSize(string filePath)
{
int sum = 0;
if (!Directory.Exists(filePath))
{
return 0;
}

DirectoryInfo dti = new DirectoryInfo(filePath);

FileInfo[] fi = dti.GetFiles();

foreach (FileInfo f in fi)
{

sum += Convert.ToInt32(f.Length / 1024);
}

DirectoryInfo[] di = dti.GetDirectories();

if (di.Length > 0)
{
for (int i = 0; i < di.Length; i++)
{
sum += GetAllFileSize(di[i].FullName);
}
}
return sum;
}
/// <summary>
/// 获取指定文件大小
/// </summary>
/// <param name="FilePath"></param>
/// <param name="FileName"></param>
/// <returns></returns>
public int GetFileSize(string FilePath, string FileName)
{
int sum = 0;
if (!Directory.Exists(FilePath))
{
return 0;
}
else
{
FileInfo Files = new FileInfo(@FilePath + FileName);
sum += Convert.ToInt32(Files.Length / 1024);
}
return sum;
}
void CreateassetbundleFile(string path, string name, byte[] info, int length)
{
//文件流信息
//StreamWriter sw;
Stream sw;
FileInfo t = new FileInfo(path + "//" + name);
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.Create();
}
else
{
//如果此文件存在则打开
//sw = t.Append();
return;
}
//以行的形式写入信息
sw.Write(info, 0, length);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
//读取本地AssetBundle文件
IEnumerator LoadAssetbundleFromLocal(string path, string name)
{
print("file:///" + path + "/" + name);

WWW w = new WWW("file:///"+path + "/" + name);

yield return w;

if (w.isDone)

{
Instantiate(w.assetBundle.mainAsset);
}
}

/// <summary>
/// 删除文件.
/// </summary>
/// <param name="path">删除完整文件夹路径.</param>
/// <param name="name">删除文件的名称.</param>
public void DeleteFile(string path, string name)
{
File.Delete(path + name);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="path"></param>
/// <param name="filesName"></param>
/// <returns></returns>
public bool DeleteFiles(string path, string filesName)
{
bool isDelete = false;
try
{
if (Directory.Exists(path))
{
if (File.Exists(path + "\\" + filesName))
{
File.Delete(path + "\\" + filesName);
isDelete = true;
}
}
}
catch
{
return isDelete;
}
return isDelete;
}
}

//”android中”
//android首次安装app后运行,文件是不存在的,需要从streamingAssetsPath中解压出来,可以解压到你想要的路径,下列其中之一
//persistentDataPath = “/mnt/sdcard/Android/data/com.qg.lxy.dx/files/”//可读写路径,运行时才能写入,打包前是不行的;ProjectSetting中有个选项Write Access可设置它的路径是沙盒还是sdcard//
//dataPath = “/data/app/com.qg.lxy.dx-1.apk”
//streamingAssetsPath = “jar:file:///data/app/com.qg.lxy.dx-1.apk!/assets”//只读路径//
//temporaryCachePath = “/data/data/com.qg.lxy.dx/cache”

//”pc中”
//persistentDataPath = “C:/Users/xiangyan/AppData/LocalLow/DefaultCompany/ztest”//可读写路径//
//dataPath = “F:/unityworkspace/ztest/Assets”
//streamingAssetsPath = “F:/unityworkspace/ztest/Assets/StreamingAssets”//只读路径//
//temporaryCachePath = “C:/Users/xiangyan/AppData/Local/Temp/DefaultCompany/ztest”

//iphone中//
//dataPath = “/var/mobile/Applications/860B998C-C72A-4F6C-9D0F-D0B119F900EB/lxy.app/Data” 这个路径有权限问题,不能访问,错误如下//
//System.UnauthorizedAccessException: Access to the path “/var/mobile/Applications/860B998C-C72A-4F6C-9D0F-D0B119F900EB/lxy.app/Data/Raw/temp/reslist.xml” is denied.
//UnauthorizedAccessException: Access to the path “/var/mobile/Applications/3C05D961-8B8A-4678-AFC8-CB9186440F56/lxy.app/Data/Raw/character.xml” is denied.
//建议用持久数据路径//
//persistentDataPath = “/var/mobile/Applications/3C05D961-8B8A-4678-AFC8-CB9186440F56/Documents”//可读写路径//

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值