예제 #1
0
    //资源特殊文件显示
    public override void OnInspectorGUI()
    {
        if (mIsValidFolder)
        {
            //assetbundle文件夹
            mGui.fontSize         = 24;
            mGui.normal.textColor = Color.green;
            GUILayout.Label(ResConfig.ASSETBUNDLE + "文件夹", mGui);

            if (mIsHeadFolder)
            {
                GUI.enabled           = mIsModify && (!Application.isPlaying);
                mGui.fontSize         = 16;
                mGui.normal.textColor = Color.green;
                var pmode = Enum.GetValues(typeof(FPackageEditor.PackType));
                foreach (var fp in pmode)
                {
                    FPackageEditor.PackType tempType = (FPackageEditor.PackType)fp;
                    if (tempType != FPackageEditor.PackType.none)
                    {
                        bool isSelect = IsHaveSameWinType(mPackType.type, (int)tempType);
                        bool rSelect  = EditorGUILayout.Toggle("当前模式状态:[" + tempType.ToString() + "]", isSelect);
                        if (mIsModify)
                        {
                            if (rSelect)
                            {
                                mPackType.type |= (int)tempType;
                            }
                            else
                            {
                                mPackType.type &= (~(int)tempType);
                            }
                        }
                        if (isSelect && tempType == FPackageEditor.PackType.micro)
                        {
                            //微端额外配置
                            mPackType.microEx = EditorGUILayout.TextField(mPackType.microEx);
                        }
                    }
                }

                if (mIsModify)
                {
                    if (GUI.changed)
                    {
                        PackFileConfig.SetPackType(mPathFile, mPackType);
                        if (mTextureTypes.ContainsKey(mPathFile))
                        {
                            mTextureTypes.Remove(mPathFile);
                        }
                    }
                }
                GUI.enabled = false;
            }
            else
            {
                mGui.fontSize         = 14;
                mGui.normal.textColor = Color.red;
                GUILayout.Label(ResConfig.ASSETBUNDLE + ":为资源文件夹,放入里面的资源文件将进入打包系统", mGui);
                mGui.normal.textColor = Color.green;
                GUILayout.Label("标签:[" + FPackageEditor.PackType.part + "]:资源分开打包", mGui);
                GUILayout.Label("标签:[" + FPackageEditor.PackType.asy + "]:资源用于异步加载", mGui);
                GUILayout.Label("标签:[" + FPackageEditor.PackType.nb + "]:资源不打包", mGui);
                GUILayout.Label("标签:[" + FPackageEditor.PackType.only + "]:资源只有被引用才打包", mGui);
                GUILayout.Label("标签:[" + FPackageEditor.PackType.micro + "]:微端资源,排除关键字文件,多个以;隔开", mGui);
                GUILayout.Label("标签:[" + FPackageEditor.PackType.cache + "]:缓存标记", mGui);
            }
        }
    }
예제 #2
0
    public static void CreatePackage(bool isCreateBundle = true)
    {
        List <string> NoBundlesFiles = new List <string>();
        string        path           = SceneManager.STREAMINGPATH.Replace("file://", "") + ResConfig.FDESASSETBUNDLE;                                                                          //正式路径
        string        tempPath       = FEPath.GetDirectoryName(SceneManager.STREAMINGPATH.Replace("file://", "")).Replace("Assets/StreamingAssets", "FPartPack/") + ResConfig.FDESASSETBUNDLE; //打包缓存路径

        //删除上次数据
        if (FEPath.Exists(path))
        {
            MyEdior.DeletFile(path);
        }

        if (File.Exists(path + ResConfig.FZIPNAMEEX))
        {
            File.Delete(path + ResConfig.FZIPNAMEEX);
        }

        AssetDatabase.Refresh();

        string onlyKey = "[" + PackType.only + "]";

        SelectPrefabEditor spe = new SelectPrefabEditor(".", "/" + ResConfig.ASSETBUNDLE + "/", "", true);

        EditorDay day = new EditorDay();

        day.SaveFile();
        bool IsSpeedPack = day.IsSpeedPack; //连续打包,不用清空,快速打包模式

        day.IsSpeedPack = true;
        day.SaveFile();

        if (!IsSpeedPack)
        {
            if (isCreateBundle)
            {
                IsSpeedPack = !EditorUtility.DisplayDialog("打包提示", "是否强制清空缓存打包?\n(提示:差异包不建议清空,可能产生Md5码的变化)", "清空", "不清空");
            }
        }

        if (isCreateBundle && !IsSpeedPack)
        {
            if (!ClearAssetBundleName())
            {
                Debug.LogError("强制终止打包");
                EditorUtility.ClearProgressBar();
                return;
            }
        }

        if (!IsSpeedPack)
        {
            if (FEPath.Exists(tempPath))
            {
                MyEdior.DeletFile(tempPath);
            }
        }


        EditorUtility.ClearProgressBar();
        FEPath.CreateDirectory(path);
        FEPath.CreateDirectory(tempPath);

        BundleManager.BundleConfig bundleConfig = new BundleManager.BundleConfig();
        Dictionary <string, Dictionary <string, BundleManager.BundleConfig.BundleFileData> > mBundleFileDatas = new Dictionary <string, Dictionary <string, BundleManager.BundleConfig.BundleFileData> >();
        Dictionary <string, int> mBundleEncrypts           = new Dictionary <string, int>();
        List <EditorScripts.NoPackAsset.PackData> packData = new List <EditorScripts.NoPackAsset.PackData>();
        List <AssetImporter> Importers = new List <AssetImporter>();


        var pmode = Enum.GetValues(typeof(FPackageEditor.PackType));
        Dictionary <string, int> PackTypeDic = new Dictionary <string, int>();

        foreach (var fp in pmode)
        {
            FPackageEditor.PackType tempType = (FPackageEditor.PackType)fp;
            PackTypeDic[tempType.ToString()] = (int)tempType;
        }

        ScriptsTime.Begin();
        //先处理文件夹一下得到打包需要的数据
        Dictionary <string, int>           HeadPath         = new Dictionary <string, int>();
        Dictionary <string, ProPathHandle> datas            = new Dictionary <string, ProPathHandle>();
        Dictionary <string, int>           allMushResouress = new Dictionary <string, int>();//所有引用的资源
        List <PackFileData>         packFiles   = new List <PackFileData>();
        Dictionary <string, string> WarningFile = new Dictionary <string, string>();

        for (int i = 0; i < spe.tempAllStrs.Length; i++)
        {
            PackFileData pfd = new PackFileData();
            packFiles.Add(pfd);
            string realName = spe.tempAllStrs[i];
            pfd.realFileName = realName;
            string        pathFile  = FEPath.GetDirectoryName(realName);
            ProPathHandle ProHandle = null;
            if (!datas.TryGetValue(pathFile, out ProHandle))
            {
                ProHandle       = new ProPathHandle();
                datas[pathFile] = ProHandle;
                ProHandle.path  = pathFile;
                int    headPos       = pathFile.IndexOf("/" + ResConfig.ASSETBUNDLE + "/");
                string otherFilePath = "";
                if (headPos == -1)
                {
                    //纯路经
                    otherFilePath = pathFile + "/";
                }
                else
                {
                    otherFilePath = pathFile.Substring(0, headPos + 1);
                }

                string[] tempHeadPaths = otherFilePath.Split('/');
                string   tString       = "";
                for (int t = 0; t < tempHeadPaths.Length - 1; t++)
                {
                    HeadPath[tString + tempHeadPaths[t]] = 1;
                    tString += (tempHeadPaths[t] + "/");
                }
                HeadPath[tString + ResConfig.ASSETBUNDLE] = 0;

                //文件夹打包类型获取
                int packType = 0;
                if (headPos != -1)
                {
                    string validPath  = pathFile.Substring(headPos + ResConfig.ASSETBUNDLE.Length + 2);
                    var    keyEncrypt = FUniversalFunction.GetChunk(validPath, "[", "]");
                    ProHandle.realPath = keyEncrypt[0].ToLower();
                    if (ProHandle.realPath != "")
                    {
                        if (WarningFile.ContainsKey(ProHandle.realPath))
                        {
                            Debug.Log("<color=#660000>存在相同的相对路径打包文件</color>:" + WarningFile[ProHandle.realPath] + "<color=#660000>****</color>" + pathFile);
                        }
                        WarningFile[ProHandle.realPath] = pathFile;
                    }

                    string[] proPaths  = validPath.Split('/');
                    string   startFile = pathFile.Substring(0, headPos + ResConfig.ASSETBUNDLE.Length + 1);
                    for (int p = 0; p < proPaths.Length; p++)
                    {
                        startFile = startFile + "/" + proPaths[p];
                        var parms = PackFileEditor.PackFileConfig.GetPackType(startFile);
                        if (parms != null)
                        {
                            packType |= parms.type;
                        }
                    }

                    for (int p = 1; p < keyEncrypt.Count; p++)
                    {
                        packType |= PackTypeDic[keyEncrypt[p]];
                    }
                }
                ProHandle.packType = packType;
            }

            int    lastIndex = realName.LastIndexOf("/");
            string fileName  = realName.Substring(lastIndex + 1).ToLower();
            pfd.budleName = string.IsNullOrEmpty(ProHandle.realPath) ? fileName : (ProHandle.realPath + "/" + fileName);
            pfd.ProHandle = ProHandle;
            if (!PackFileEditor.IsHaveSameWinType(ProHandle.packType, (int)PackType.only))
            {
                string[] paths = AssetDatabase.GetDependencies(realName);
                for (int p = 0; p < paths.Length; p++)
                {
                    allMushResouress[paths[p]] = 1;
                }
            }
        }


        int AssetsNum = "Assets".Length + 1;

        for (int i = 0; i < packFiles.Count; i++)
        {
            PackFileData  pf       = packFiles[i];
            ProPathHandle pHandle  = pf.ProHandle;
            string        spePath  = pf.realFileName;
            string        pathName = pHandle.path;

            if (EditorUtility.DisplayCancelableProgressBar("打包数据资源", "转换进度: " + spePath, (float)i / spe.tempAllStrs.Length))
            {
                EditorUtility.ClearProgressBar();
                return;
            }

            EditorScripts.NoPackAsset.PackData enp = new EditorScripts.NoPackAsset.PackData();
            enp.allName = spePath;

            AssetImporter ai       = AssetImporter.GetAtPath(spePath);
            int           packType = pf.ProHandle.packType;//PackFileEditor.IsHaveSameWinType(ProHandle.packType, (int)PackType.only)

            if (ai != null)
            {
                Importers.Add(ai);
                string houPro = "";

                string budleName   = pf.budleName;
                string encryptFile = budleName;

                //需要智能排除
                if (PackFileEditor.IsHaveSameWinType(packType, (int)PackType.only))
                {
                    if (!allMushResouress.ContainsKey(spePath))
                    {
                        ai.assetBundleName = "";
                        continue;
                    }
                }
                else
                {
                    packData.Add(enp);
                }

                bool isNoBundle = PackFileEditor.IsHaveSameWinType(packType, (int)PackType.nb);

                int    headPos  = budleName.LastIndexOf(".");
                string typeName = BundleManager.PrefabType;
                if (headPos != -1)
                {
                    string hou = budleName.Substring(headPos);
                    if (hou != "" && BundleManager.AssetType.IndexOf(hou) != -1)
                    {
                        houPro = hou;
                    }
                    budleName = budleName.Substring(0, headPos);
                    typeName  = hou.ToLower();
                }

                string pathFile = "";
                headPos = budleName.LastIndexOf("/");
                if (headPos != -1)
                {
                    pathFile += (budleName.Substring(0, headPos));
                }

                Dictionary <string, BundleManager.BundleConfig.BundleFileData> bmbfds = new Dictionary <string, BundleManager.BundleConfig.BundleFileData>();
                mBundleEncrypts[pathFile] = packType;
                if (mBundleFileDatas.ContainsKey(pathFile))
                {
                    bmbfds = mBundleFileDatas[pathFile];
                }
                else
                {
                    if (!isNoBundle)
                    {
                        mBundleFileDatas[pathFile] = bmbfds;
                    }
                }
                enp.key = (budleName + houPro).ToLower();
                string assetbundleName = budleName + ResConfig.ASSETBUNDLESUFFIXES;
                //路径打包规则
                if (pathFile != "")
                {
                    string txtName = pathFile.ToLower();
                    int    filePos = pathFile.LastIndexOf("/");
                    if (filePos != -1)
                    {
                        txtName = txtName.Substring(filePos + 1);
                    }
                    if (!PackFileEditor.IsHaveSameWinType(packType, (int)PackType.part))
                    {
                        assetbundleName = pathFile + "/" + txtName + ResConfig.ASSETBUNDLESUFFIXES;
                    }
                }

                enp.type = packType;
                if (isNoBundle)
                {
                    string CopyBunld = encryptFile;
                    string copyPath  = tempPath + "/" + encryptFile;//修改
                    string dir       = FEPath.GetDirectoryName(copyPath);
                    if (isCreateBundle)
                    {
                        if (!FEPath.Exists(dir))
                        {
                            FEPath.CreateDirectory(dir);
                        }

                        if (File.Exists(copyPath))
                        {
                            File.Copy(spePath, copyPath, true);
                        }
                        else
                        {
                            File.Copy(spePath, copyPath);
                        }
                        NoBundlesFiles.Add(copyPath);
                    }
                    enp.key       = CopyBunld.ToLower();
                    enp.bunldName = enp.key;
                }
                else
                {
                    ai.assetBundleName = assetbundleName.ToLower();
                    enp.bunldName      = ai.assetBundleName;
                    //特殊处理
                    if (ai is TextureImporter)
                    {
                        TextureImporter ti = ai as TextureImporter;
                        if (ti.textureType == TextureImporterType.Sprite)
                        {
                            typeName = BundleManager.SpriteType;
                        }
                        else
                        {
                            typeName = BundleManager.TextureType;
                        }

                        string tagName = ai.assetBundleName.Replace(ResConfig.ASSETBUNDLESUFFIXES, ".PackTag");

                        if (PackFileEditor.IsHaveSameWinType(packType, (int)PackType.part))
                        {
                            tagName = "";
                        }

                        if (tagName != ti.spritePackingTag)
                        {
                            ti.spritePackingTag = tagName;
                            ti.SaveAndReimport();
                        }
                    }

                    BundleManager.BundleConfig.BundleFileData fd = new BundleManager.BundleConfig.BundleFileData();
                    fd.bundleFile          = assetbundleName.ToLower();
                    fd.resFileName         = (budleName.Replace(pathFile + "/", "") + houPro).ToLower();
                    fd.resType             = typeName;
                    fd.mIsCache            = PackFileEditor.IsHaveSameWinType(packType, (int)PackType.cache);
                    bmbfds[fd.resFileName] = fd;
                }
            }
        }

        ScriptsTime.Debug("打包资源完成");

        //创建免打包配置
        EditorScripts.CreateNoPackAsset(packData);
        //创建文件夹外观表现
        PackFileEditor.PackFileConfig.SetHeadFile(HeadPath);

        ScriptsTime.ShowTime("计算打包配置文件");
        if (isCreateBundle)
        {
            foreach (var ke in mBundleFileDatas)
            {
                var encrypts = mBundleEncrypts[ke.Key];
                BundleManager.BundleConfig.PathBundles bbf = new BundleManager.BundleConfig.PathBundles();
                bbf.path        = ke.Key;
                bbf.fileBundles = ke.Value;
                bbf.mIsAsync    = PackFileEditor.IsHaveSameWinType(encrypts, (int)PackType.asy);
                bbf.mIsOnly     = !PackFileEditor.IsHaveSameWinType(encrypts, (int)PackType.part);
                bundleConfig.pathBundles[ke.Key] = bbf;
            }

            //按时间设置更新版本
            bundleConfig.versionId = MyEdior.GetTimeVersion();
            PlayerPrefs.SetInt("versionId", bundleConfig.versionId);//记录版本号
            FSaveHandle sd = FSaveHandle.Create(tempPath + "/" + ResConfig.ASSETBUNDLECONFIFILE, FFilePath.FP_Abs, FOpenType.OT_Write);
            sd.PushObject(bundleConfig);
            sd.Save();

            if (IsSpeedPack)
            {
                BuildPipeline.BuildAssetBundles(tempPath, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
            }
            else
            {
                BuildPipeline.BuildAssetBundles(tempPath, BuildAssetBundleOptions.ForceRebuildAssetBundle | BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
            }
        }
        //拷贝
        MyEdior.CopyDirectory(tempPath, path);

        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
        CreateStreamingConfig();

        //清空配置文件
        if (NoBundlesFiles.Count != 0)
        {
            for (int i = 0; i < NoBundlesFiles.Count; i++)
            {
                File.Delete(NoBundlesFiles[i]);
                Debug.Log("清空了其他未知文件:" + NoBundlesFiles[i]);
            }
            NoBundlesFiles.Clear();
        }
    }