如何在 Android 或 iPhone 上本地访问 Unity 资产? [英] How to access Unity assets natively on Android or iPhone?
本文介绍了如何在 Android 或 iPhone 上本地访问 Unity 资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 Unity 插件项目中在 Android 和 iPhone 上本地访问文件(来自 C++ 或 Java 代码).Unity 是否提供任何方法来访问项目资产中的文件?
I need to access a file natively (from C++ or Java code) on Android and iPhone in a Unity plugin project. Does Unity provide any method to access files in the project Assets?
推荐答案
- 我的解决方法是将文件从 Application.streamingAssetsPath(它在 jar 中,大多数本地库无法访问)复制到 Application.persistentDataPath(完全没问题),然后将该路径传递给本机代码.
- 项目中的源文件应该在AssetsStreamingAssets下
c# 中异步复制文件的小示例代码:将此方法添加到继承 MonoBehaviour 的脚本中
Small sample code in c# to copy files async: Add this method to a script which inherits 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
}
这篇关于如何在 Android 或 iPhone 上本地访问 Unity 资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文