Unity加载Android里的StreamingAssets里的文件

| 发布     | 分类 Unity  | 标签 Unity  Android  StreamingAssets 

有时候项目中为了方便 想同步读取StreamingAssets里的文件,Android之前是个障碍。现在的版本Unity可以通过AssetBundle.LoadFromFile读取AssetBundle文件了,但读取其他类型的资源还是不行。参考一下雨松的 修改了一下。
参考 : Unity3D研究院之Android同步方法读取streamingAssets(八十八)

需要的盆友懒得弄SDK可以直接下载 AssetLoadSDK.jar ,将他放到Plugins/Android目录下

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<Sprite>();
        GetComponent<SpriteRenderer>().sprite = sprites[0];
	}
}



package com.ihaiu.assetloadsdk;

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);
	}
	
}




using UnityEngine;
using System.Collections;

public class AndroidAssetLoadSDK 
{
    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);
    }
}


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>().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
    }
	
}



上一篇: 查看keystore的SHA1
下一篇: Unity 优化总结