Unity之 AssetBundle应用

来源:互联网 发布:管家婆 端口 编辑:程序博客网 时间:2024/05/19 04:05

把以下这个脚本放入工程中    自动 打包已经设置了AssetBundleName的资源

using UnityEngine;
using UnityEditor;
using System.IO;


public class NewAssetBundleEditor : Editor
{
    [MenuItem("AssetBundle/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        //如果文件夹不存在,则创建文件夹
        string outPath = Application.dataPath + "/AssetBundles";
        if (!Directory.Exists(outPath))
        {
            Directory.CreateDirectory(outPath);
        }
        //BuildAssetBundleOptions.ForceRebuildAssetBundle:用于强制重打所有AssetBundle文件
        //BuildTarget.StandaloneWindows:适应平台:Windows
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneWindows);
        AssetDatabase.Refresh();
    }
}

以上是资源打包


下面是资源加载脚本

using UnityEngine.UI ;
using System.Collections;
using UnityEngine;
public class ReadAB : MonoBehaviour {
    string path ;


// Use this for initialization
void Start () {
        path = "file:///" + Application.dataPath + "/AssetBundles/cube";
       transform .Find ("Button").GetComponent<Button>().onClick .AddListener (delegate(){
       StartCoroutine (loadabbywww1(path));}); 
}

// Update is called once per frame
void Update () {

}
    public IEnumerator loadabbywww1(string path) 
    {
      //  AssetBundle ab = AssetBundle.LoadFromFile(path);
        WWW www = new WWW(path);
        yield return www;
       while (!www.isDone )
        {
            yield return null;
        }
       AssetBundle ab = www.assetBundle;
        GameObject cube = ab.LoadAsset("cube")as GameObject ;
        if (cube!=null)
        {
            Debug.Log(cube.name);
        }
        
        Instantiate(cube);
        yield return new WaitForSeconds(0.1f);
        ab.Unload(false);
    }








    public IEnumerator loadabbywww(string path)
    {
        string murl = "file:///" + Application.dataPath + "/AssetBundles";
        WWW mwww = WWW.LoadFromCacheOrDownload(murl,0);
        yield return mwww;
        while (!mwww.isDone)
        {
            yield return null;
        }
        AssetBundle mab = mwww.assetBundle;
        AssetBundleManifest mainfest = (AssetBundleManifest)mab.LoadAsset("AssetBundlesManifest");
        mab.Unload(false);


        string[] dps = mainfest.GetAllDependencies("cube");
        AssetBundle[] abs = new AssetBundle[dps.Length];
        for (int i = 0; i < dps.Length;i++)
        {
            string durl = "file:///" + Application.dataPath + dps[i];
            WWW dwww = WWW.LoadFromCacheOrDownload(durl,mainfest.GetAssetBundleHash (dps[i]));
            yield return dwww;
            abs[i] = dwww.assetBundle;
        }
        WWW www = WWW.LoadFromCacheOrDownload(path,mainfest.GetAssetBundleHash("firstab"+".ab"),0);
        yield return www;
        AssetBundle ab = www.assetBundle;
        GameObject cube = ab.LoadAsset("firstab") as GameObject;
        if (cube!=null)
        {
            Instantiate(cube);
            ab.Unload(false);
        }
    }
}


原创粉丝点击