如何访问统一的资产本身在Android或iPhone? [英] How to access Unity assets natively on Android or iPhone?

查看:112
本文介绍了如何访问统一的资产本身在Android或iPhone?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

  1. 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.
  2. The source files in the project should be under Assets\StreamingAssets

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆