zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    第二节:如何正确使用WebApi和使用过程中的一些坑
    nodejs中function*、yield和Promise的示例
    关于nodejs访问mysql的思考
    nodejs使用log4js记录日志
    nodejs初识
    Spring学习笔记(入门)
    mybatis使用注解开发
    MyBatis配置文件中的常用配置
    using 自动释放资源示例
    Java将byte[]和int的互相转换
  • 原文地址:https://www.cnblogs.com/dawn-cn/p/4238092.html
Copyright © 2011-2022 走看看