10月9日AssetBundle学习笔记

来源:互联网 发布:网络逗逗迪迪爱探险 编辑:程序博客网 时间:2024/06/05 08:53


//创建AB

using UnityEditor;
using System.IO;
public class CreateAssetBundle
{
    /// <summary>
    /// 建立AssetBundle包
    /// </summary>
    [MenuItem("Assets/Build AssetBundles")]
    public static void BuildAllAssetBundle()
    {
        string str = "AssetBundles";
        //在项目根文件下创建文件夹
        if (Directory.Exists(str) == false)
        {
            Directory.CreateDirectory(str);
        }
        //建立AssetBundle包
        BuildPipeline.BuildAssetBundles(str, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
    }
}


//记载AB

/*
 * https://docs.unity3d.com/Manual/AssetBundles-Native.html
*/


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;


public class LoadFromFileExample : MonoBehaviour
{
    string path = "AssetBundles/scene/cubewall.ab";


    void Start ()
{
   
        //加载AssetBundle
        //AssetBundle material = AssetBundle.LoadFromFile("AssetBundles/share.ab");
        //AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/scene/cubewall.ab");
        //GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");
        //Instantiate(wallPrefab);
        //Object[] objs = ab.LoadAllAssets();
        //foreach (Object temp in objs)
        //{
        //    Instantiate(temp);
        //}


   StartCoroutine("LoadAssetsFromFileAsync");
        LoadABManifest();






}


    #region  第一种加载AssetBundle的方式 AsseyBundle.LoadFromMemory


    /// <summary>
    /// 第一种加载AssetBundle的方式 LoadFromMemoryAsync异步加载
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadAsstesFromMemoryAsnyc()
    {
        //第一种加载AssetBundle的方式 LoadFromMemoryAsync异步
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return request;
        AssetBundle ab = request.assetBundle;


        //使用资源
        GameObject obj = ab.LoadAsset<GameObject>("cubewall");
        Instantiate(obj);
    }
    /// <summary>
    /// LoadFromMemoryAsync同步加载
    /// </summary>
    void LoadAssetsFromMemory()
    {
        AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));


        //使用资源
        GameObject obj = ab.LoadAsset<GameObject>("cubewall");
        Instantiate(obj);
    }


    #endregion






    #region 第二种加载AssetBundle的方式 AsseyBundle.LoadFromFile


    /// <summary>
    /// 第二种加载AssetBundle的方式 LoadFromFileAsync异步
    /// </summary>
    IEnumerator LoadAssetsFromFileAsync()
    {
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        yield return request;


        AssetBundle ab = request.assetBundle;
        //使用资源
        GameObject obj = ab.LoadAsset<GameObject>("cubewall");
        Instantiate(obj);
    }
    /// <summary>
    /// 第二种加载AssetBundle的方式 LoadFromFile 同步
    /// </summary>
    void LoadAssetsFromFile()
    {
        AssetBundle ab = AssetBundle.LoadFromFile(path);


        //使用资源
        GameObject obj = ab.LoadAsset<GameObject>("cubewall");
        Instantiate(obj);
    }


    #endregion




    #region  第三种种加载AssetBundle的方式 WWW.LoadFromCacheOrDownload


    /// <summary>
    /// WWW本地文件记载
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadAssetsByWWW()
    {
        while (!Caching.ready)
        {
            yield return null;
        }
        //1 前缀file:// 或 file:///
        //2 路径前加@ 为强制不转义 的符号,在里面的转义字符无效
        //WWW www = WWW.LoadFromCacheOrDownload("file:///D:\\UserData\\My Documents\\U3D Projects\\Test\\AssetBundle\\AssetBundles\\scene\\cubewall.ab", 1);
        WWW www = WWW.LoadFromCacheOrDownload(@"file:///D:\UserData\My Documents\U3D Projects\Test\AssetBundle\AssetBundles\scene\cubewall.ab", 1);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }


        AssetBundle ab = www.assetBundle;


        Instantiate(ab.LoadAsset<GameObject>("cubewall"));
    }


    /// <summary>
    /// WWW服务器文件加载
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadAssetsByWWW2()
    {
        while (!Caching.ready)
        {
            yield return null;
        }
       
        WWW www = WWW.LoadFromCacheOrDownload(@"http://Localhost/AssetBundles\scene\cubewall.ab", 1);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }


        AssetBundle ab = www.assetBundle;


        Instantiate(ab.LoadAsset<GameObject>("cubewall"));
    }


    #endregion




    #region 第四种种加载AssetBundle的方式 UnityWebRequest


    IEnumerator LoadAssetsByWeb()
    {
        //本地
        //string url = @"file:///D:\UserData\My Documents\U3D Projects\Test\\AssetBundle\AssetBundles\scene\cubewall.ab";


        //服务器
        string url = @"http://Localhost/AssetBundles\scene\cubewall.ab";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
        yield return request.Send();
        //1
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        //2
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;


        Instantiate(ab.LoadAsset<GameObject>("cubewall"));
    }




    #endregion




    #region 通过Manifest文件得到某个包的依赖


    void LoadABManifest()
    { 
        AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");


        //foreach (string name in manifest.GetAllAssetBundles())
        //{
        //    Debug.Log(name);
        //}
        string[] str = manifest.GetAllDependencies("scene/cubewall.ab");
        foreach (string name in str)
        {
            print(name);
            AssetBundle.LoadFromFile("AssetBundles/" + name);
        }


       


    }


    #endregion
}

原创粉丝点击