上代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
public class ReadAssetbundles : MonoBehaviour
{
//不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
/*public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
#else
string.Empty;
#endif*/
//设置需要读取的AssetBundle文件名
public static string fileName = "scene/model.ab";
/*
备注知识点
⑴Path.Combine(string, string) 连接两个字符串
⑵ Application 访问应用程序运行时数据
Application.streamingAssetsPath输出
E:/UnityProject/ARVR/Workspace/MyCharacter/Assets/StreamingAssets
Application.dataPath输出
E:/UnityProject/ARVR/Workspace/MyCharacter/Assets
值得注意的是如果在PC上使用时需要在每个文件夹前加入斜杠,如:
string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
*/
private void Start()
{
//Debug.Log("Start!");
//从文件夹里加载包
//将本地文件放到项目下的StreamingAssets文件夹中
//var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "model.ab"));
//加载绝对文件
string myPath = "D:/AB/model.ab";
var myLoadedAssetBundle = AssetBundle.LoadFromFile(myPath);
if (myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle!");
return;
}
//从Bundle包中加载名字为:XXXX的资源,加载为GameObject
var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("Obj1");
//实例化
Instantiate(prefab);
//卸载包中资源的内存
myLoadedAssetBundle.Unload(false);
}
}