如何访问统一的资产本身在Android或iPhone?资产、Android、iPhone

由网友(愿与你不期而遇亦以笑相迎)分享简介:我需要在Android和iPhone本机访问文件(从C ++或Java code)在统一的插件项目。有没有统一提供任何访问项目资产文件的方法?解决方案 我的解决方法是拷贝从Application.streamingAssetsPath(这是一个罐子内,不能老是由大多数本机库被访问)到Application.pers...

我需要在Android和iPhone本机访问文件(从C ++或Java code)在统一的插件项目。有没有统一提供任何访问项目资产文件的方法?

解决方案 我的解决方法是拷贝从Application.streamingAssetsPath(这是一个罐子内,不能老是由大多数本机库被访问)到Application.persistentDataPath(这是完全正常)中的文件,然后通过该路径来本机code。 在项目的源文件应在资产 StreamingAssets

小样本code在C#中复制文件异步: 添加此方法的脚本继承MonoBehaviour

 的IEnumerator CopyFileAsyncOnAndroid()
    {
     字符串fromPath = Application.streamingAssetsPath +/;
     //在安卓=JAR:文件://!/资产/+ Application.dataPath +
     字符串toPath = Application.persistentDataPath +/;

     字符串[] filesNamesToCopy =新的String [] {a.txt中,b.txt};
     的foreach(在filesNamesToCopy字符串文件名)
     {
       的debug.log(+ fromPath +文件名+到+ toPath从复制);
       WWW WWW1 =新的WWW(fromPath +文件名);
       得到的回报WWW1;
       的debug.log(产量完成);
       File.WriteAllBytes(toPath +文件名,www1.bytes);
       的debug.log(文件拷贝完成);
      }
      //增加您的来电NATIVE code HERE
      //注:4的小文件可以花一秒钟来完成复制。所以做一次,
      //设置一个永久标志,就知道你不`吨需要再次调用它
     }
 

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?

解决方案 国外团队成功在iPhone上刷入安卓系统,可开机实操

My workaround was to copy the files from the Application.streamingAssetsPath (which is inside a jar and can`t be accessed by most native libraries) to the Application.persistentDataPath (which is totally fine)and then to pass that path to the native code. The source files in the project should be under AssetsStreamingAssets

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
     }    

阅读全文

相关推荐

最新文章