首页 > 代码库 > [Unity3d][NGUI]打包NGUI预制件成Assetbundle 两种思路.

[Unity3d][NGUI]打包NGUI预制件成Assetbundle 两种思路.

http://www.58player.com/blog-2537-85030.html

接上文,项目中因为需要UI热更新,所以我使用了AssetBundle这个解决方案. 
          
        一般来说,我们使用AssetBundle生成资源包常用的方案是如下这么用: 
         
        using UnityEngine;
        using UnityEditor;
        
        /// <summary>
        /// 导出资源类
        /// </summary>
        public class ExportGameResources
        {
            static BuildAssetBundleOptions m_option = BuildAssetBundleOptions.CollectDependencies |  // 收集所有依赖关系
                                                      BuildAssetBundleOptions.DeterministicAssetBundle;
            /// <summary>
            /// 导出NGUI成Assetbundle
            /// </summary>
            [MenuItem("Assets/导出/资源")]
            static public void ExportNGUI()
            {
                // 获取编辑器中选择的项
                Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        
                // 没有选中项
                if (objs.Length == 0)
                {
                    return;
                }
        
                string _savepath = null;
                // 判断是多选还是单选
                if (objs.Length == 1)
                {
                    // 获取保存路径
                    _savepath = EditorUtility.SaveFilePanel("Save Resource", "", objs[0].name, "assetbundle");
        
                    // 生成assetbundle
                    BuildPipeline.BuildAssetBundle(objs[0], null,
                           _savepath,
                           m_option,
                           EditorUserBuildSettings.activeBuildTarget);
                }
                else
                {
                    // 获取保存路径
                    _savepath = EditorUtility.SaveFolderPanel("Save Resources", "", "");
        
                    // 生成assetbundle
                    for (int i = 0; i < objs.Length; ++i)
                    {
                        BuildPipeline.BuildAssetBundle(objs[i], null,
                            _savepath + "/" + objs[i].name + ".assetbundle",
                            m_option,
                            EditorUserBuildSettings.activeBuildTarget);
                    }
                }
            }
        }
         
        这就是我最开始写的将NGUI预制件打包成Assetbundle的方法. 这个方法看起来没啥问题,使用WWW流加载也没有问题. 但是真的如此吗?   
        其实我一开始也没有注意到AssetBundle中的资源依赖关系这个问题. 
        因为我最开始生成的几个Assetbundle0均是ngui演示中的几个预制件,生成出来的大小比较正常一般几K到几十K. 
          
        于是我天真的以为就这么用就好了. 
        直到我开始做几个需要中文文本支持的界面时,才发现我跳进了一个小坑. 
          
        TODO:................. 
          
        PS:这个Blog嘛 不算啥专业的技术Blog,只是随笔. 

[Unity3d][NGUI]打包NGUI预制件成Assetbundle 两种思路.