有时候项目中为了方便 想同步读取StreamingAssets里的文件,Android之前是个障碍。现在的版本Unity可以通过AssetBundle.LoadFromFile读取AssetBundle文件了,但读取其他类型的资源还是不行。参考一下雨松的 修改了一下。
参考 :
Unity3D研究院之Android同步方法读取streamingAssets(八十八)
需要的盆友懒得弄SDK可以直接下载
AssetLoadSDK.jar
,将他放到Plugins/Android目录下
AssetBundle.LoadFromFile
public class TestAssetBundle : MonoBehaviour {
void Start ()
string path;
if (Application.platform == RuntimePlatform.Android)
path = Application.dataPath + "!assets/Android/hero_20001-assetbundle";
path = Application.streamingAssetsPath + "/IOS/hero_20001-assetbundle";
AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
Sprite[] sprites = assetBundle.LoadAllAssets<Sprite>();
GetComponent<SpriteRenderer>().sprite = sprites[0];
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();
public static byte[] LoadFile(string path)
AndroidJavaClass m_AndroidJavaClass = new AndroidJavaClass("com.ihaiu.assetloadsdk.AssetLoad");
return m_AndroidJavaClass.CallStatic<byte[]>("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);
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>().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