我需要在Unity插件项目中从C++或Java代码本地访问文件在Android和iPhone上。Unity是否提供了访问Assets目录下文件的方法?
我需要在Unity插件项目中从C++或Java代码本地访问文件在Android和iPhone上。Unity是否提供了访问Assets目录下文件的方法?
使用C#编写异步复制文件的小样例: 将此方法添加到继承MonoBehaviour的脚本中
IEnumerator CopyFileAsyncOnAndroid()
{
string fromPath = Application.streamingAssetsPath +"/";
//In Android = "jar:file://" + Application.dataPath + "!/assets/"
string toPath = Application.persistentDataPath +"/";
string[] filesNamesToCopy = new string[] { "a.txt" ,"b.txt"};
foreach (string fileName in filesNamesToCopy)
{
Debug.Log("copying from "+ fromPath + fileName +" to "+ toPath);
WWW www1 = new WWW( fromPath +fileName);
yield return www1;
Debug.Log("yield done");
File.WriteAllBytes(toPath+ fileName, www1.bytes);
Debug.Log("file copy done");
}
//ADD YOUR CALL TO NATIVE CODE HERE
//Note: 4 small files can take a second to finish copy. so do it once and
//set a persistent flag to know you don`t need to call it again
}
运行时访问文件的权限仅限于Assets/Resources目录。放置在该目录中的所有内容都可以通过Resources.Load
方法(doc)进行访问,但无法从文件夹外部获取任何内容。
在Assets/Resources文件夹内,您可以设置任意文件夹结构。我在iPhone项目中使用它来从预定义的文本文件构建关卡,效果非常好。