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 字符串数组资源
  2. Android jni中数组参数的传递方式
  3. Android intent 传递数组对象序列化
  4. android 图片与byte数组间的转换
  5. android JNI java与C++传递String数组(引用类型)
  6. 2.Retrofit上传list数组给后端
  7. 在android中创建包含对象数组对象List 的Parcelable
  8. android解析二维数组对象key:value
  9. 【Android学习笔记】AutoCompleteTextView(数组在代码中)

随机推荐

  1. 选择器的使用和模块化组件思想
  2. PHP:【微信小程序】微信小程序API,微信小
  3. 请求与响应
  4. 微信小程序数据操作、自定义事件、微信AP
  5. ThinkPhp6.0 vscode 布署
  6. PHP:【微信小程序】微信小程序部分组件,微
  7. vscode 布署scratch3.0 GUI
  8. php检测扩展有没有加载成功的几种方法
  9. 【记账后台管理系统】用户列表、用户日志
  10. 0531实战作业