//此脚本放在“Asset/Editor”
using System.IO;
using UnityEditor;

public class CreateAB
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBuundles()
{
string dir = "AssetBundles";
if (Directory.Exists (dir)==false)
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
}

////////////////////////////////////////////////////////
//加载
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class LoadFormFile : MonoBehaviour
{
IEnumerator Start()//异步加载使用协同程序调用
//private void Start()
{
//string path1 = "AssetBundles / mt.wei";
string path = "AssetBundles/tree.wei";
//示例:通过本地路径加载AB包
/AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/mt.wei");//存在依赖包时,所依赖的资源包也需要加载
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/tree.wei");//文件路径
GameObject tree = ab.LoadAsset("Tree 02");//预制件名称
GameObject go = Instantiate(tree);
go.transform.SetParent(this.transform );
/

    //遍历生成AB报中所有物体并生成
    /*Object[] obj = ab.LoadAllAssets();
    foreach (Object item in obj)
    {
        Instantiate(item);
    }*/


    //第一种方式:通过内存加载AB包的方式:AssetBundle.LoadFromMemoryAsync
    //异步加载
    /*AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
    yield return request;
    AssetBundle ab = request.assetBundle;*/

    //同步加载
    //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));

    //第二种方式:通过本地文件加载AB包:
    //异步加载
    /*AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
    yield return request;
    AssetBundle ab = request.assetBundle;*/
    //同步加载(示例)
    //AssetBundle ab = AssetBundle.LoadFromFile(path);

    ////////////网络加载////////////

    //第三种方式:WWW.LoadFromCacheOrDownload(异步加载(IEnumerator))
    /*WWW www = WWW.LoadFromCacheOrDownload(@"file:///D:\715\3DFreeWord\AssetBundles\tree.wei",1);
    yield return www;
    AssetBundle ab = www.assetBundle;*/


    //第四种方式:UnityWebRequest(异步执行(IEnumerator))
    string uri = @"file:///D:\715\3DFreeWord\AssetBundles\tree.wei";
    UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
    yield return request.Send();
    //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);//获取方式一
    AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;//获取方式二


    //使用里面的资源
    GameObject tree = ab.LoadAsset<GameObject>("Tree 02");
    GameObject go = Instantiate(tree/*, transform.position,Quaternion.identity*/);
    go.transform.position = this.transform.position;

//通过Mainfest获取所有的依赖包
AssetBundle mainfestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");//加载AssetBundles文件
AssetBundleManifest mainfest = mainfestAB.LoadAsset("AssetBundleManifest");//通过AssetBundles获取AssetBundles.manifest文件
/foreach (string name in mainfest.GetAllAssetBundles())//获取所有的AB包,返回包名。
{
print(name);
}
/
string[] strs = mainfest.GetAllDependencies("tree.wei");//获取某个AB包所依赖的包,返回包名。
foreach (string name in strs)
{
print(name);
AssetBundle.LoadFromFile("AssetBundles/" + name);//加载所有的依赖包
}
}
}