Application.dataPath
Application.streamingAssetsPath
Application.persistentDataPath
Application.temporaryCachePath
根据测试,详细情况如下:
一.在项目根目录中创建Resources文件夹来保存图片。可以使用Resources.Load("文件名字,注:不包括文件后缀名");把文件夹中的对象加载出来。
注:此方可实现对文件实施“增删查改”等操作,但打包后不可以更改了。
二.直接放在项目根路径下来保存文件
在直接使用Application.dataPath来读取文件进行操作。
注:移动端是没有访问权限的。
三.在项目根目录中创建StreamingAssets文件夹来保存文件。
1.可以使用Application.dataPath来读取文件进行操作。
[C#]
#if UNITY_EDITOR
string filepath=Application.dataPath+"/StreamingAssets"+"/photo.jpg";
#elif UNITY_IPHONE
string filepath=Application.dataPath+"/Ray"+"/photo.jpg";
#elif UNITY_android
string filepath="jar:file://"+Application.dataPath+"!/assets/"+"/photo.jpg";
#endif
2.直接使用Application.streamingAssetsPath来读取文件进行操作。
注:此方法在pc/Mac电脑中可以实现对文件实施“增删查改”等操作,但在移动端只支持读取操作。
四.使用Application.streamingAssetsPath来操作文件(荐)。
改文件存在手机沙盒中,因为不能直接存放文件,
1.通过服务器直接下载保存到该位置,也可以通过Md5码比对下载更新新的资源
2.没有服务器的,只有间接通过文件流的方式从本地读取并写入Application.persistentDataPath文件下,然后在通过Application.persistentDataPath来读取操作。
注:在Pc/Mac电脑以及Android跟Ipad、iphone都可对文件进行任意操作,另外在IOS上对该目录下的东西可以被iCloud自动备份。
五.使用Application.temporaryCachePath来操作文件
操作方式跟上面Application.persistentDataPath类似。除了在IOS上不能被iCloud自动备份。
下图是几种文件在Pc中路径的具体位置
常用操作
unity路径的使用以及注意点都总结了, 常用操作:
1.对文件的操作类,主要就是文流读取操作的一些东西
//照片变量
public GameObject[] Images;
///
/// 协程为照片变量赋值(图片)
///
///
IEnumerator LoadWWWAllPicture()
{
//获取路径下的照片
string[] streamingPath = Directory.GetFiles(Application.persistentDataPath + "/");
for (int i = 0, j = 0; i < streamingPath.Length; i++, j++)
{
WWW www = new WWW("file:///" + streamingPath[i]);
yield return www;
if (www.isDone && www != null)
{
Images[j].GetComponent().mainTexture = www.texture;
//为获取到的图片赋上名字
Images[j].GetComponent().mainTexture.name = streamingPath[i];
Debug.Log(Images[j].GetComponent().mainTexture.name);
www.Dispose();
}
}
}
实例一:
void Awake()
{
string path =
#if UNITY_ANDROID && !UNITY_EDITOR
Application.streamingAssetsPath + "/Josn/modelname.json";
#elif UNITY_IPHONE && !UNITY_EDITOR
"file://" + Application.streamingAssetsPath + "/Josn/modelname.json";
#elif UNITY_STANDLONE_WIN||UNITY_EDITOR
"file://" + Application.streamingAssetsPath + "/Josn/modelname.json";
#else
string.Empty;
#endif
StartCoroutine(ReadData(path));
}
IEnumerator ReadData(string path)
{
WWW www = new WWW(path);
yield return www;
while (www.isDone == false)
{
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(0.5f);
string data = www.text;
yield return new WaitForEndOfFrame();
}
踩过的坑:
- 在移动平台下,Application.streamingAssetsPath是只读的,不能写入数据。Application.persistentDataPath 可以读取和写入数据。
- 在PC下,可以用File类API(如File.ReadAllText)读写StreamingAssets文件夹中的文件;在IOS和Android平台下,不能用File类API读取。
- 所有平台上都可以用www方式异步读取StreamingAssets文件夹,PC和IOS平台下,读取路径必须加上"file://",而安卓不需要。
- 在IOS和Android下,还能用AssetBundle.LoadFromFile来同步读取数据。
string path =
#if UNITY_ANDROID
Application.dataPath + "!assets"+ "/";
#else
Application.streamingAssetsPath + "/";
#endif
AssetBundle assetbundle = AssetBundle.LoadFromFile(path +"sprite.unity3d");
Sprite sprite = assetbundle.LoadAsset("test");
实例二:
参考: http://www.manew.com/thread-23491-1-1.html