• assetbundle


    ---恢复内容开始---

    此文源于unityManul(高版本)

    1)生成assetbundle

        [MenuItem("Assets/Build AssetBundles")]
        static void BuildAllAssetBundles()
        {
            string dir = "AB";
            if (Directory.Exists(dir) == false)
            {
                Directory.CreateDirectory(dir);
            }
            BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
        }

    2)同步本地加载

            var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));//可以实全路径也可以是相对路径如 "AB/cc.dd"
            if (myLoadedAssetBundle == null) {
                Debug.Log("Failed to load AssetBundle!");
                return;
            }
            var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
            Instantiate(prefab);

    3)异步本地加载

        IEnumerator LoadFromMemoryAsync(string path)
        {
            AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));//
            yield return createRequest;
            AssetBundle bundle = createRequest.assetBundle;
            var prefab = bundle.LoadAsset<GameObject>("MyObject");
            Instantiate(prefab);
        }

    问题:AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path))LoadFromMemoryAsync只要传入byte[]类型参数即可,而且在android中如果assetbundle放在streamingassets下file类是无法读取的,所以可以通过其他方法异步加载,如www类。加载到byte[]然后传给LoadFromMemoryAsync方法调用。异步方法还有更直接的方法如下。

    4)WWW异步加载

    此加载assetbundle的方法将被弃用

                while (!Caching.ready)
                        yield return null;
    
            var www = WWW.LoadFromCacheOrDownload("http://myserver.com/myassetBundle", 5);
            yield return www;
            if(!string.IsNullOrEmpty(www.error))
            {
                Debug.Log(www.error);
                yield return;
            }
            var myLoadedAssetBundle = www.assetBundle;
    
            var asset = myLoadedAssetBundle.mainAsset;

    如果存在本地,则LoadFromCacheOrDownload("http://myserver.com/myassetBundle", 5)中的http链接改为本地路径,但是路径前要加http://,或者采用类new System.Uri(本地路径).absoluteUri;获取相关相关uri路径。

     5)UnityWebRequest方法

            string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;       
     UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
            yield return request.Send();
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
            GameObject cube = bundle.LoadAsset<GameObject>("Cube");
            GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
            Instantiate(cube);
            Instantiate(sprite);

    6)加载Assetbundle内容

    官方api中提供了众多方法,最基本的方法为abCube.LoadAsset<T>方法

            AssetBundle abMat = AssetBundle.LoadFromFile(mat);
            AssetBundle abCube = AssetBundle.LoadFromFile(cubep);
            AssetBundle abSphere = AssetBundle.LoadFromFile(spherep);
    
            GameObject cube = abCube.LoadAsset<GameObject>("Cube");
            Instantiate(cube);
            GameObject sphere = abSphere.LoadAsset<GameObject>("Sphere");
            Instantiate(sphere);

    7)通过打包的总文件获取依赖

    AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath);//打包的总文件位置
    AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    string[] dependencies = manifest.GetAllDependencies("cc.dd"); //获取cc.dd的所有依赖文件.
    foreach(string dependency in dependencies)
    {
        AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency));
    }

    ---恢复内容结束---

  • 相关阅读:
    CRC16冗余循环检测计算器好用。modbus RTU
    WINCC 应用与提高(78讲15.98G)视频教程网盘下载
    MFC win32 API串口同步模式代码示范
    arduino连接12864LCD方法ST7920
    arduino连接LCD1602LCD方法
    WIN10下如何解决PL2303驱动不可用的问题或者com口显示黄色感叹号usbtoserial
    SQL Server类型与C#类型对应关系
    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问 .
    sql中如何调用另一台服务器的数据库查询数据呢?
    SQL Server 触发器
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9496593.html
Copyright © 2020-2023  润新知