public static void BuildAB(bool isHotFix = false, string md5Path = "", string hotCount = "")
    {
        m_OtherABFiles.Clear();
        m_AssetFiles.Clear();
        m_PrefabABFiles.Clear();
        m_ConfigFiles.Clear();
        ABPathConfig config = AssetDatabase.LoadAssetAtPath <ABPathConfig>(m_ABConfigPath);

        if (config == null)
        {
            return;
        }

        //其他资源的AssetBundle
        foreach (var item in config.OtherAssetDirectorys)
        {
            if (m_OtherABFiles.ContainsKey(item.ABName))
            {
                Debug.Log("文件AB包名重复,请检查!!!!!!!!!!!!!!!!!!!!!!");
            }
            else
            {
                m_OtherABFiles.Add(item.ABName, item.Path);
                m_AssetFiles.Add(item.Path);
                m_ConfigFiles.Add(item.Path);
            }
        }

        //Prefab资源的AssetBundle
        //查找这个路径下的所有的prefab路径的GUID
        string[] prefabFileArr = config.PrefabAssetDirectorys.ToArray();
        string[] guidArray     = AssetDatabase.FindAssets("t:Prefab", prefabFileArr);

        // string[] allStr = AssetDatabase.FindAssets("t:Prefab", config.PrefabAssetDirectorys.ToArray());
        for (int i = 0; i < guidArray.Length; i++)
        {
            //根据GUID获取路径
            string prefabPath = AssetDatabase.GUIDToAssetPath(guidArray[i]);
            EditorUtility.DisplayProgressBar("查找prefab", "Path:" + prefabPath, (i + 1) * 1 / guidArray.Length);
            m_ConfigFiles.Add(prefabPath);
            //添加到要打包的路径
            if (!ContainAllAssetPath(prefabPath))
            {
                //获取依赖项
                GameObject    go = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);
                string[]      denpendAssePathtArr  = AssetDatabase.GetDependencies(prefabPath);
                List <string> denpendAssetPathList = new List <string>();
                for (int j = 0; j < denpendAssePathtArr.Length; j++)
                {
                    if (!ContainAllAssetPath(denpendAssePathtArr[j]) && !denpendAssePathtArr[j].EndsWith(".cs"))
                    {
                        m_AssetFiles.Add(denpendAssePathtArr[j]);
                        denpendAssetPathList.Add(denpendAssePathtArr[j]);
                    }
                }
                if (m_PrefabABFiles.ContainsKey(go.name))
                {
                    Debug.LogErrorFormat("%s 与 %s 的预制体重名 这个预制体将不会参与打包,请检查", prefabPath, m_PrefabABFiles[go.name]);
                }
                else
                {
                    m_PrefabABFiles.Add(go.name, denpendAssetPathList);
                }
            }
        }

        //设置AssetBundle的后缀
        foreach (var item in m_OtherABFiles)
        {
            SetABName(item.Key, item.Value);
        }

        foreach (var item in m_PrefabABFiles)
        {
            SetABName(item.Key, item.Value);
        }

        BuildAssetBundle();

        if (isHotFix)
        {
            ReadABMD5(md5Path, hotCount);
        }
        else
        {
            WriteABMd5();
        }


        //清除AB包名,这是因为在设置是更改AB包名,会造成资源的meta文件修改,不利于上传
        //把一个已经标记Bundle Name 资源移动到文件夹中的时候,下次打包会出错
        string[] oldNames = AssetDatabase.GetAllAssetBundleNames();
        for (int i = 0; i < oldNames.Length; i++)
        {
            AssetDatabase.RemoveAssetBundleName(oldNames[i], true);
        }


        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }
Esempio n. 2
0
    public static void Build()
    {
        //配置资源的ab路径可以有两种方式,一是:生成.asset去配置, 二是:直接在代码中配置
        ABPathConfig abPathConfig = AssetDatabase.LoadAssetAtPath <ABPathConfig>(PathConfig.ABCONFIGASSETSPATH);

        if (abPathConfig == null)
        {
            Debug.LogError("没有配置资源路径");
            return;
        }

        //ABPathConfig abPathConfig = new ABPathConfig();

        m_AllFileDir.Clear();
        m_filterPathList.Clear();
        m_singlePrefabAllDepPathDic.Clear();
        foreach (PathABNameClass namePath in abPathConfig.m_pathABNameList)
        {
            if (m_AllFileDir.ContainsKey(namePath.ABName))
            {
                Debug.LogError("名称重复,请检查!");
            }
            else
            {
                m_AllFileDir.Add(namePath.ABName, namePath.Path);
                m_filterPathList.Add(namePath.Path);
            }
        }

        //获取m_AllPrefabPath文件夹下的所有文件(包括子文件夹的)
        string[] prefabGuidArray = AssetDatabase.FindAssets("t:prefab", abPathConfig.m_prefabPathList.ToArray());
        for (int i = 0; i < prefabGuidArray.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(prefabGuidArray[i]);
            EditorUtility.DisplayProgressBar("查找path", "prefab:" + path, (float)i / prefabGuidArray.Length);

            GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>(path);
            //查找到prefab的路径后,检索出所有dependence(依赖项)

            #region 这里过滤文件(FilterPath)有个bug, todo
            //有这样一种情况,a.prefab, b.prefab同时使用了img1.png,img2.png这两张图片,
            //并且这两个prefab不在同一个文件下,两张图片也没有打成ab包,这样的话,
            //就会过滤掉b.prefab的依赖项,在加载b.prefab时就会出错
            //所以不能在这个位置过滤,需要在写进二进制或xml数据时过滤
            //因为在这里过滤的话,m_singlePrefabAllDepPathDic就不会有b.prefab
            //的依赖了

            #endregion

            string[]      tempDependenceArray = AssetDatabase.GetDependencies(path);
            List <string> tempList            = new List <string>();
            if (!FilterPath(path))
            {
                for (int j = 0; j < tempDependenceArray.Length; j++)
                {
                    if (!FilterPath(tempDependenceArray[j]) && !tempDependenceArray[j].EndsWith(".cs"))
                    {
                        m_filterPathList.Add(tempDependenceArray[j]);
                        tempList.Add(tempDependenceArray[j]);
                    }
                }
            }
            if (m_singlePrefabAllDepPathDic.ContainsKey(go.name))
            {
                Debug.LogError("存在相同名称的prefab:" + go.name);
            }
            else
            {
                m_singlePrefabAllDepPathDic.Add(go.name, tempList);
            }
        }

        foreach (string name in m_AllFileDir.Keys)
        {
            SetABName(name, m_AllFileDir[name]);
        }
        foreach (string name in m_singlePrefabAllDepPathDic.Keys)
        {
            SetABName(name, m_singlePrefabAllDepPathDic[name]);
        }

        BuildAssetBundle();
        //清空所有abname
        string[] names = AssetDatabase.GetAllAssetBundleNames();
        for (int i = 0; i < names.Length; i++)
        {
            AssetDatabase.RemoveAssetBundleName(names[i], true);
        }
        AssetDatabase.RemoveUnusedAssetBundleNames();

        //以下两行代码是刷新编辑器的,文件的meta文件会被刷新
        //AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        EditorUtility.ClearProgressBar();
    }