我试图在运行时从android本地存储中的下载assetbundle加载一个对象,但是没有找到结果。虽然资产捆绑存在于我的Android设备的本地存储中。
请有人帮我这样做,以下是我写的代码:
public GameObject obj;
IEnumerable LoadObject()
{
AssetBundle bundle = AssetBundle.CreateFromFile(Application.persistentDataPath + "/androidassetbundle4");
yield return bundle;
AssetBundleRequest request = bundle.LoadAssetAsync("boat", typeof(GameObject));
yield return request;
obj = request.asset as GameObject;
obj.transform.position = new Vector3(0.08f, -2.345f, 297.54f);
obj.transform.Rotate(350.41f,400f,20f);
obj.transform.localScale = new Vector3(1.0518f, 0.998f, 1.1793f);
Instantiate(obj);
bundle.Unload(false);
}//end of Method LoadObject
答案 0 :(得分:0)
CreateFromFile
已弃用。 LoadFromFile
和LoadFromFileAsync
是要使用的新功能。不确定这是不是问题,但下面的代码应该这样做。
IEnumerator LoadObject()
{
AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(System.IO.Path.Combine(Application.streamingAssetsPath, "assetbundlename"));
yield return bundle;
AssetBundle myLoadedAssetBundle = bundle.assetBundle;
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
yield break;
}
AssetBundleRequest request = myLoadedAssetBundle.LoadAssetAsync<GameObject>("boat");
yield return request;
obj = request.asset as GameObject;
obj.transform.position = new Vector3(0.08f, -2.345f, 297.54f);
obj.transform.Rotate(350.41f, 400f, 20f);
obj.transform.localScale = new Vector3(1.0518f, 0.998f, 1.1793f);
Instantiate(obj);
myLoadedAssetBundle.Unload(false);
}
答案 1 :(得分:0)
您无法通过Android中的CreateFromFile或LoadFromFile访问本地存储中的文件,而是使用LoadFromCacheOrDownload,而在android中,这是您的StreamingAssets的地址:"jar:file://" + Application.dataPath + "!/assets/"
正如统一文档中提到的那样:StreamingAssets