版权声明:转载 请说明 https://blog.csdn.net/qq2512667/article/details/83514629
ab包 加载有两种方式,一种是www 加载 一种是 调用UnityEngine下的Networking库里的
UnityWebRequest 方法;
下面 是添加一个菜单 按钮 可以把 所有prefabs 打包进 ab包·
using UnityEditor;
public class CreateAssetBundle
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetsBundles()
{
BuildPipeline.BuildAssetBundles("Assets/AssetsBundles",BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows);
}
}
IEnumerator Start()
{
url = @"file:///"+Application.dataPath+ "/AssetsBundles/" + "player.assetbundle";
//www 加载
using (WWW www = new WWW(url))
{
yield return www;
if (www.error != "" || www.error != null)
{
Debug.Log(www.error);
}
else
{
AssetBundle ab = www.assetBundle;
Object pre = ab.LoadAsset(prefabsName);
Instantiate(pre);
ab.Unload(false);
}
}
//第2种 UnitryWebRequest加载
// UnityWebRequest request = UnityWebRequest.GetAssetBundle(url, 0);
//yield return request.Send();
//AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
// Object prefab= bundle.LoadAsset(prefabsName);
// Instantiate(prefab);
// bundle.Unload(false);
}
从 manifest中取得所有 GameObject资源
void loadAssetBundleManifest()
{
AssetBundle assetBundle = AssetBundle.LoadFromFile("Assets/AssetBundle/AssetBundle");
AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//string[] depencies = manifest.GetAllDependencies("door.unity3d");
//foreach (var item in depencies)
//{
// print(item);
// AssetBundle shaderBundle = AssetBundle.LoadFromFile("Assets/AssetBundle/" + item);
//}
//加载包中 所有的资源
string[] depencies = manifest.GetAllAssetBundles();
foreach (string item in depencies)
{
Debug.Log(item);
AssetBundle bundle = AssetBundle.LoadFromFile("Assets/AssetBundle/" + item);
foreach (var obj in bundle.LoadAllAssets(typeof(GameObject)) )
{
Instantiate(obj);
}
}
}