• Unity3D读取assetbundle


    方法一、GreatFromFile方法加载模型

    优缺点:

    建议使用AssetBundle.CreatFromFile 它是一个同步方法。现在IOS 和 android 都支持了。打包的时候需要选择不压缩

    assetbundle打包方式:

     

     1 BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.CollectDependencies 

     读取方式:

     1  /// <summary>
     2     /// CreatFromFile 方法加载模型
     3     /// </summary>
     4     /// <param name="f_strPath"></param>
     5     /// <returns></returns>
     6     public AssetBundle getCreatFromFile(string f_strPath)
     7     {
     8         try
     9         {
    10         AssetBundle _AssetBundle =  AssetBundle.CreateFromFile(f_strPath);
    11         return _AssetBundle;
    12         }
    13         catch (Exception e)
    14         {
    15             Debug.LogError("加载模型文件失败 AssetBundle " + f_strPath);
    16         }
    17         return null;
    18     
    19     } 

    方法二、FromMemory方式加载模型

     1  /// <summary>
     2     /// FromMemory方式加载模型
     3     /// </summary>
     4     /// <param name="f_strPath"></param>
     5     /// <returns></returns>
     6     public AssetBundleCreateRequest getModelByResourcePath(string f_strPath)
     7     {
     8         AssetBundleCreateRequest _requestResult = null;
     9         try
    10         {
    11             byte[] _bs = File.ReadAllBytes(f_strPath);
    12             Debug.Log(" AssetBundle " + f_strPath);
    13             _requestResult = AssetBundle.CreateFromMemory(_bs);
    14            
    15         }
    16         catch (Exception e)
    17         {
    18             Debug.LogError("加载模型文件失败 AssetBundle " + f_strPath);
    19         }
    20         return _requestResult;
    21     }

    方法三、WWW方式加载模型

    WWW bundle = new WWW(path);

    这样的做法是通过一个路径进行下载(无论是服务器路径还是本地路径下载操作都一样)但是bundle只能保存在内存中,也就是退出游戏在进入还得重新下,很显然在游戏中我们不能使用这种方式。

     1 {
     2          WWW bundle = WWW.LoadFromCacheOrDownload(path,5);
     3  
     4          yield return bundle;
     5  
     6          //加载到游戏中
     7          yield return Instantiate(bundle.assetBundle.mainAsset);
     8  
     9          bundle.assetBundle.Unload(false);
    10     }

    getModelByResourcePathByWWW方法加载模型

    WWW.LoadFromCacheOrDownload 是异步方法,而且还占用内存。

     1  /// <summary>
     2     /// WWW加载模型
     3     /// </summary>
     4     /// <param name="f_strPath"></param>
     5     /// <returns></returns>
     6     public WWW getModelByResourcePathByWWW(string f_strPath)
     7     {
     8       //int _nCount=UnityEngine.Random.Range(1,10000);
     9         try
    10         {
    11 
    12 
    13             WWW _www = WWW.LoadFromCacheOrDownload("file://" + f_strPath, 1);
    14             Debug.Log("www加载模型文件成功:FilePath" + f_strPath);
    15             return _www;
    16         }
    17         catch (Exception e)
    18         {
    19             Debug.LogError("加载模型文件失败 AssetBundle " + f_strPath + " " + e.GetBaseException());
    20         }
    21         return null;
    22     }
  • 相关阅读:
    热爱工作 发财机会大增(这里不是选择软件还是硬件的问题,是自己的性格和追求的问题)——当你的老板不如你懂行的时候,还赚的盆满钵满的时候,你就可以考虑独立了 good
    TaintDroid:智能手机监控实时隐私信息流跟踪系统(三)
    Delphi读取文件属性
    创业者该如何看待腾讯的“跟进”?
    IBM总裁郭士纳总结的四类人
    迅雷程浩:企业外包服务,下一个大的风口?(2B业务一定要懂销售和营销的人,这点和2C 不一样)
    dddd
    android:minSdkVersion 之我见
    dddd
    请人不怕啦
  • 原文地址:https://www.cnblogs.com/dawn-cn/p/4238092.html
Copyright © 2020-2023  润新知