• AssetBundle的使用初涉


      前几天看的AssetBundle的使用,今天把它先整理一下,在unity3d中的资源的打包和加载基本都是通过AssetBundle 来实现的,

    AssetBundle配合WWW一起实现从服务器端或者内存中动态加载资源,下面是我看刘国柱老师书借鉴着写的一段AssetBundle加载本地中

    资源的例子。代码部分如下:

    using UnityEngine;
    using System.Collections;
    
    public class Load01 : MonoBehaviour
    {
        //首先加载总的manifest文件
        private string manifestName ="StreamingAssets";
    
        //加载assetsBudles文件
        private string assetBundleName = "test";
    
        IEnumerator Start()
        {
    
            Debug.Log(Application.dataPath);
        //{
        //    //WWW www = new WWW("http://baike.baidu.com/");
        //    WWW www = WWW.LoadFromCacheOrDownload("http://www.baidu.com/",5);
        //    yield return www;
        //    Debug.Log(www.GetType().ToString());
           
            //AssetBundle所在的路径
            string assetBundlePath = "file:D:/Lianxi_File/Unity/Message/MessageDemo/Assets/StreamingAssets/";
            ////string assetBundlePath = "file:D:/Lianxi_File/Unity/Message/MessageDemo/Assets/StreamingAssets/StreamingAssets";
    
            //manifest 文件的路径
            string manifestPath = assetBundlePath + manifestName;
            //string manifestPath="file:D:/Lianxi_File/Unity/Message/MessageDemo/Assets/StreamingAssets/StreamingAssets";
    
    
            //首先加载manifest文件
            WWW wwwManifest = WWW.LoadFromCacheOrDownload(manifestPath,0);
    
            yield return wwwManifest;
    
            //判断是否加载到资源
            if(!string.IsNullOrEmpty(wwwManifest.error))    //wwwManifest.error==null
            {
                Debug.Log(wwwManifest.error);
                yield break;
            }
    
            else
            {
                AssetBundle manifestBundles = wwwManifest.assetBundle;
    
                AssetBundleManifest manifest = (AssetBundleManifest)manifestBundles.LoadAsset("AssetBundleManifest");
    
                manifestBundles.Unload(false);
    
                //获取依赖文件列表
                string[] dependentAssetBundles = manifest.GetAllDependencies(assetBundleName);
                AssetBundle[] abs = new AssetBundle[dependentAssetBundles.Length];
                Debug.Log(dependentAssetBundles.Length);
                for (int i = 0; i < dependentAssetBundles.Length;i++ )
                {
                    //加载所有的依赖文件(第一次加载)
                    WWW www1 = WWW.LoadFromCacheOrDownload(assetBundlePath+dependentAssetBundles[i],0);
                    yield return www1;
                    Debug.Log("1111");
                }
    
                //加载需要的文件(从缓存中加载出来,第二次加载)
                WWW www2 = WWW.LoadFromCacheOrDownload(assetBundlePath+assetBundleName,0);
    
                yield return www2;
                AssetBundle assetBundle = www2.assetBundle;
                Debug.Log(assetBundle.ToString());
                Debug.Log("finish");
            }
        }
    }
     

    以上是写的加载的例子,过程中遇到一些问题,后续会更加完善

    ........未完待续

  • 相关阅读:
    WIN32程序挂钩SetLastError,输出错误描述到控制台
    设置c#windows服务描述及允许服务与桌面交互的几种方法(作者博客还有一大堆C#创建服务的文章)
    VC内存溢出一例 –- 调用约定不一致 (_CRT_DEBUGGER_HOOK(_CRT_DEBUGGER_GSFAILURE)
    QtWaitingSpinner
    美国独立战争真相:英法全球争霸战之北美战场
    asp.net的ajax以及json
    HashTable和HashSet中的类型陷阱
    C语言的一些常见细节
    堆排序详解
    Core Animation编程指南
  • 原文地址:https://www.cnblogs.com/springword/p/5951276.html
Copyright © 2020-2023  润新知