Exemple #1
0
    public static void Pack()
    {
        //AB包放在项目工程外 避免生成mate文件
        if (!Directory.Exists(BundleTargetPath))
        {
            Directory.CreateDirectory(BundleTargetPath);
        }
        AssetBunldePathConfig config = AssetDatabase.LoadAssetAtPath <AssetBunldePathConfig>(ABCONFIGPATH);

        //所有xml转二进制
        DataEditor.AssetAllXmlToBinary();
        m_AllFileDir.Clear();
        m_AllPrefabDir.Clear();
        m_VaildDir.Clear();
        //普通文件(没有依赖的)
        foreach (AssetBunldePathConfig.NameAndPath item in config.m_AllFileABPath)
        {
            if (m_AllFileDir.ContainsKey(item.name))
            {
                Debug.LogWarning("Bundle:m_AllFileDir中存在重复key值," + item.name);
                return;
            }
            else
            {
                m_AllFileDir.Add(item.name, item.path);
                m_VaildDir.Add(item.path);
            };
        }
        //Prefab(有依赖的)
        //得到文件夹里的预制guid
        string[] guid = AssetDatabase.FindAssets("t:Prefab", config.m_AllPrefabPath.ToArray());
        for (int i = 0; i < guid.Length; i++)
        {
            //prefab的path(相对路径)
            string        path        = AssetDatabase.GUIDToAssetPath(guid[i]);
            GameObject    obj         = AssetDatabase.LoadAssetAtPath <GameObject>(path);
            string[]      all_depend  = AssetDatabase.GetDependencies(path);
            List <string> depend_path = new List <string>();
            m_VaildDir.Add(path);
            for (int j = 0; j < all_depend.Length; j++)
            {
                // 过滤掉依赖的脚本(打包脚本没用 而且会占用空间) 并且不在普通文件下 避免重复打包资源
                if (!all_depend[j].EndsWith(".cs") && !LogTool.IsPathContains(all_depend[j], m_VaildDir))
                {
                    depend_path.Add(all_depend[j]);
                }
            }
            // 预制体的依赖信息(不包括普通文件)
            if (!m_AllPrefabDir.ContainsKey(obj.name))
            {
                m_AllPrefabDir.Add(obj.name, depend_path);
            }
            else
            {
                Debug.LogWarning("Bundle:m_AllPrefabDir中存在重复key值," + obj.name);
            }
            EditorUtility.DisplayProgressBar("查找prefab", "Prefab:" + path, i * 1.0f / guid.Length);
        }
        //设置普通文件AB名称
        foreach (string name in m_AllFileDir.Keys)
        {
            SetABName(name, m_AllFileDir[name]);
        }
        //设置预制体AB名称
        foreach (string name in m_AllPrefabDir.Keys)
        {
            SetABName(name, m_AllPrefabDir[name]);
        }
        //打包
        BiuldAssetBundle();
        //设置完成后 把ABName删除,避免manifest文件改变
        string[] ab_names = AssetDatabase.GetAllAssetBundleNames();
        for (int i = 0; i < ab_names.Length; i++)
        {
            //forceRemove 是否强制删除
            AssetDatabase.RemoveAssetBundleName(ab_names[i], true);
            EditorUtility.DisplayProgressBar("清除ABName", "Name:" + ab_names[i], i * 1.0f / ab_names.Length);
        }
        AssetDatabase.Refresh();//刷新界面
        EditorUtility.ClearProgressBar();
    }
Exemple #2
0
    //将需要的配置生成xml 和 binary
    static void WriteData(Dictionary <string, string> path_dic)
    {
        AssetBundleConfig ab_config = new AssetBundleConfig();

        ab_config.ABList = new List <AssetBundleBase>();
        foreach (string path in path_dic.Keys)
        {
            //path在有效目录下
            if (!LogTool.IsPathContains(path, m_VaildDir))
            {
                continue;
            }
            AssetBundleBase ab_base = new AssetBundleBase();
            ab_base.Path       = path;
            ab_base.ABName     = path_dic[path];
            ab_base.AssetName  = path.Remove(0, path.LastIndexOf('/') + 1);
            ab_base.Crc        = Crc32.GetCrc32(path);
            ab_base.ABDependce = new List <string>();
            string[] all_depend = AssetDatabase.GetDependencies(path);
            for (int i = 0; i < all_depend.Length; i++)
            {
                if (path == all_depend[i] || all_depend[i].EndsWith(".cs"))
                {
                    continue;
                }
                string name;
                if (path_dic.TryGetValue(all_depend[i], out name))
                {
                    if (name == ab_base.ABName)//ABName已经添加过了
                    {
                        continue;
                    }
                    if (!ab_base.ABDependce.Contains(name))
                    {
                        ab_base.ABDependce.Add(name);
                    }
                }
            }
            ab_config.ABList.Add(ab_base);
        }
        //写入xml
        string xml_path = Application.dataPath + "/Script/7-Frame/2-Bundle/AssetBundleConfig.xml";

        if (File.Exists(xml_path))
        {
            File.Delete(xml_path);
        }
        FileStream    fs = new FileStream(xml_path, FileMode.Create);
        StreamWriter  sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        XmlSerializer xs = new XmlSerializer(ab_config.GetType());

        xs.Serialize(sw, ab_config);
        sw.Close();
        fs.Close();
        //写入binary
        foreach (AssetBundleBase ab_base in ab_config.ABList)
        {
            ab_base.Path = "";//使用CRC计算出path,所以不需要path
        }
        fs = new FileStream(BINARY_PATH, FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();

        bf.Serialize(fs, ab_config);
        fs.Close();
    }