找回密码
 立即注册
查看: 475|回复: 0

Unity AB加载笔记

[复制链接]
发表于 2021-3-31 18:30 | 显示全部楼层 |阅读模式
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.IO;using UnityEngine.Networking;public class LoadFromFileExample : MonoBehaviour{    // Start is called before the first frame update    void Start()    {        //1.第二种加载AB的方式LoadFromFile同步加载(本地加载)        //AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");        //AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/cubewall.unity3d");        //GameObject wallPrefab = ab.LoadAsset<GameObject>("cubeWall");        //Instantiate(wallPrefab);        ////第一种加载AB的方式LoadFromMemory同步加载(当AB包使用UDP或者TCP协议的时候可以运用此方法)        //string path = "AssetBundles/cubewall.unity3d";        //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));        //GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");        //Instantiate(wallPrefab);        StartCoroutine(LoadUnityWebRequest());    }    IEnumerator StartFromMemoryAsync()    {        //第一种加载AB的方式LoadFromMemoryAsync异步加载(当AB包使用UDP或者TCP协议的时候可以运用此方法)        string path = "AssetBundles/cubewall.unity3d";        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));        yield return request;        AssetBundle ab = request.assetBundle;        //使用里面的资源        GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");        Instantiate(wallPrefab);    }    IEnumerator StartFromFileAsync()    {        //1.第二种加载的方式LoadFromFileAsync异步加载(本地加载)        string path = "AssetBundles/cubewall.unity3d";        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);        yield return request;        AssetBundle ab = request.assetBundle;        //使用资源        GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");        Instantiate(wallPrefab);    }    IEnumerator WWWLoadFrom()    {        //此方法已弃用        string path = "AssetBundles/cubewall.unity3d";        //第三种加载AB的方式WWW        while (Caching.ready == false)        {            yield return null;        }        //fiel://    fiel:///        //WWW www = WWW.LoadFromCacheOrDownload(@"file:///O:\测试\AssetBundleTestProject\AssetBundles\cubewall.unity3d", 1);        WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);                       //访问服务器资源        yield return www;        if (string.IsNullOrEmpty(www.error) == false)        {            Debug.Log(www.error);            yield break;        }        AssetBundle ab = www.assetBundle;        //使用资源        GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");        Instantiate(wallPrefab);    }    //第四种方式 使用UnityWebRequest    IEnumerator LoadUnityWebRequest()    {        //string uri = @"file:///O:\测试\AssetBundleTestProject\AssetBundles\cubewall.unity3d";        string uri = @"http://localhost/AssetBundles/cubewall.unity3d";        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);        yield return request.SendWebRequest();        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);        //使用资源        GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");        Instantiate(wallPrefab);    }}加载Manifest文件 检查加载依赖包

IEnumerator LoadUnityWebRequest()    {        //string uri = @"file:///O:\测试\AssetBundleTestProject\AssetBundles\cubewall.unity3d";        string uri = @"http://localhost/AssetBundles/cubewall.unity3d";        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);        yield return request.SendWebRequest();        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);        //使用资源        GameObject wallPrefab = ab.LoadAsset<GameObject>("cubewall");        Instantiate(wallPrefab);        //第一种方式本地加载Manifest文件 检查加载依赖包        //AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");        //AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");        //string[] strs = manifest.GetDirectDependencies("cubewall.unity3d");        //foreach (string name in strs)        //{        //    print(name);        //    AssetBundle.LoadFromFile("AssetBundles/"+name);        //}        //第二种方式加载服务器Manifest文件 检查加载依赖包        UnityWebRequest request_1 = UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundles/AssetBundles");        yield return request_1.SendWebRequest();        AssetBundle ab_1 = DownloadHandlerAssetBundle.GetContent(request_1);        AssetBundleManifest manifest = ab_1.LoadAsset<AssetBundleManifest>("AssetBundleManifest");        string[] strs = manifest.GetDirectDependencies("cubewall.unity3d");        foreach (string name in strs)        {            print(name);            UnityWebRequest request_2 = UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundles/"+name);            yield return request_2.SendWebRequest();            AssetBundle ab_2 = DownloadHandlerAssetBundle.GetContent(request_2);               }    }
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-20 20:51 , Processed in 0.123278 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表