【发布时间】:2011-11-23 18:00:12
【问题描述】:
我需要在 Unity 插件项目中本地访问 Android 和 iPhone 上的文件(从 C++ 或 Java 代码)。 Unity 是否提供任何方法来访问项目 Assets 中的文件?
【问题讨论】:
标签: android iphone unity3d assets
我需要在 Unity 插件项目中本地访问 Android 和 iPhone 上的文件(从 C++ 或 Java 代码)。 Unity 是否提供任何方法来访问项目 Assets 中的文件?
【问题讨论】:
标签: android iphone unity3d 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 项目中使用它从预定义的文本文件中构建关卡,它的作用就像一个魅力。
【讨论】: