AssetBundle.LoadFromFile

public class TestAssetBundle : MonoBehaviour {void Start ()     {        string path;        if (Application.platform == RuntimePlatform.Android)        {            path = Application.dataPath + "!assets/Android/hero_20001-assetbundle";        }        else        {            path = Application.streamingAssetsPath + "/IOS/hero_20001-assetbundle";        }        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);        Sprite[] sprites = assetBundle.LoadAllAssets();        GetComponent().sprite = sprites[0];}}

通过在Java中编写Jar包读取  放到Plugins/Android目录下

import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import com.unity3d.player.UnityPlayer;import android.util.Log;public class AssetLoad {    private static byte[] readtextbytes(InputStream inputStream) {   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  //长度这里暂时先写成1024  byte buf[] = new byte [1024];   int len;   try {    while ((len = inputStream.read(buf)) != -1) {     outputStream.write(buf, 0, len);    }    outputStream.close();    inputStream.close();   } catch (IOException e) {   }  return outputStream.toByteArray();} //读取assetbund并且返回字节数组public static byte[] loadFile(String path){ InputStream inputStream = null ;   try {    inputStream = UnityPlayer.currentActivity.getAssets().open(path);   } catch (IOException e)   {   Log.e("ihaiu.com", e.getMessage());   }   return readtextbytes(inputStream);}}

Unity工具类

using UnityEngine;using System.Collections;public class AndroidAssetLoadSDK {    public static byte[] LoadFile(string path)    {        AndroidJavaClass    m_AndroidJavaClass = new AndroidJavaClass("包名");        return m_AndroidJavaClass.CallStatic("loadFile", path);    }    public static string LoadTextFile(string path)    {        byte[] bytes = LoadFile(path);        if (bytes == null)            return "Error bytes=null";                return System.Text.Encoding.UTF8.GetString ( bytes );    }    public static AssetBundle LoadAssetBundle(string path)    {        byte[] bytes = LoadFile(path);        if (bytes == null)            return null;                return AssetBundle.LoadFromMemory(bytes);    }}

测试脚本

using UnityEngine;using System.Collections;using System.IO;using UnityEngine.UI;public class TestLoadText : MonoBehaviour{void Start ()     {                Test();}    void OnGUI()    {        if (GUILayout.Button("Test", GUILayout.MinWidth(200), GUILayout.MinHeight(100)))        {            Test();        }    }    public void Test()    {        string path = "game_const.json";        string str = LoadText(path);        GetComponent().text = string.IsNullOrEmpty(str) ? "Load Empty" : str;    }    public string LoadText(string path)    {        #if UNITY_ANDROID && !UNITY_EDITOR        return AndroidAssetLoadSDK.LoadTextFile(path);        #else        return File.ReadAllText(Application.streamingAssetsPath + "/" +  path);        #endif    }}

 

更多相关文章

  1. 第一章:初入Android大门(Gallery拖动相片特效)
  2. android 网络图片与网页读取
  3. Android(安卓)File 数据存储
  4. Android(安卓)Log Analysis
  5. android 图片压缩
  6. 任务栏一键删除 Recent APP
  7. Android(安卓)Log Analysis bugreport
  8. Android手机开发:ImageView使用和从内存读取图片显示
  9. Android读取联系人的姓名及号码

随机推荐

  1. nginx牛逼tomcat弱鸡探索之道
  2. 数据结构与算法—队列(搞懂最常用数据结
  3. java后端学习路线建议
  4. 从阶乘、斐波那契、汉诺塔剖析彻底搞懂递
  5. java开发者linux入门
  6. 数据结构与算法—小白也能搞懂二叉排序(
  7. csdn账号密码登录剖析(模拟登录)
  8. 爬虫实现csdn文章一键(批量)更换阅读类型
  9. 数据结构与算法—绪论
  10. 数据结构于算法—线性表详解(顺序表、链