Ejemplo n.º 1
0
    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        bool bMovedSound  = false;
        bool bImportSound = false;

        //处理的是否是UI文件
        bool bImportUI = false;


        OnPreProcessScheme();

        for (int i = 0; i < importedAssets.Length; i++)
        {
            //看看是不是场景文件
            string s = importedAssets[i];
            AssetBundleManagerEditor.CheckAssetName(s);
            if (isSceneAssets(s))
            {
                MakeSceneAssetBundleInfo(s);
            }
            else if (isShaderAssets(s))
            {
                MakeShaderAssetBundle(s);
            }
            else if (isMeshAssets(s))
            {
                MakeMeshAssetBundle(s);
            }
            else if (isAudioAssets(s))
            {
                bImportSound = true;
                MakeSoundAssetBundleData(s);
            }
            else if (IsFontAsset(s))
            {
                MakeFontBundleData(AssetImporter.GetAtPath(s));
            }
            else if (IsFBXAsset(s))
            {
                MakeFBXAssetBundle(AssetImporter.GetAtPath(s) as ModelImporter);
            }
            else if (IsMaterialAsset(s))
            {
                MakeMaterialAssetBundle(s);
            }
            else if (IsAniClipAsset(s))
            {
                MakeAnimatorClipAssetBundleData(s);
            }
            else if (IsControllerAsset(s))
            {
                MakeAnimatorControllerAssetBundleData(s);
            }
            else if (IsTextureAsset(s))
            {
                MakeTextureAssetBundle(s);
            }
            else if (isSchemeAssets(s))
            {
                MakeSchemeAssetBundleInfo(s);
            }

            string path = importedAssets[i];
            //载入的话弄到表里
            if (path.Contains(UIDynamicAssetPatch))
            {
                if (!IsTextureAsset(path))
                {
                    continue;
                }

                bImportUI = true;
                UFileData data = new UFileData();

                data.configData = MakeTextureAssetBundleFromReference(path, string.Empty);
                if (null != data.configData)
                {
                    PostprocessUITextureToCSV.nEditorInstance.InsetConfig(data);
                }
                else
                {
                    Debug.LogError("取得UI资源失败:" + path);
                }
            }
        }

        //资源移动了
        List <string> movedGUID = new List <string>();

        for (int i = 0; i < movedAssets.Length; i++)
        {
            string str = movedAssets[i];
            AssetBundleManagerEditor.CheckAssetName(movedAssets[i]);
            //先清掉ab
            ClearAssetBundleData(AssetImporter.GetAtPath(str));

            //再根据资源类型来重新生成ab
            //是音效的
            if (isAudioAssets(str))
            {
                MakeSoundAssetBundleData(str);
                movedGUID.Add(AssetBundleManagerEditor.GetAssetGUIDFromMeta(str));
                bMovedSound = true;
            }
            //是贴图
            else if (IsTextureAsset(str))
            {
                MakeTextureAssetBundle(str);
            }
            //是场景
            else if (isSceneAssets(str))
            {
                MakeSceneAssetBundleInfo(str);
            }
            else if (IsFontAsset(str))
            {
                MakeFontBundleData(AssetImporter.GetAtPath(str));
            }
            else if (IsFBXAsset(str))
            {
                CheckFBXInImportTime(str);
                MakeFBXAssetBundle(AssetImporter.GetAtPath(str) as ModelImporter);
            }
            else if (IsMaterialAsset(str))
            {
                MakeMaterialAssetBundle(str);
            }
            else if (IsAniClipAsset(str))
            {
                MakeAnimatorClipAssetBundleData(str);
            }
            else if (IsControllerAsset(str))
            {
                MakeAnimatorControllerAssetBundleData(str);
            }
            else if (isSchemeAssets(str))
            {
                MakeSchemeAssetBundleInfo(str);
            }
            else if (isShaderAssets(str))
            {
                MakeShaderAssetBundle(str);
            }
            else if (isMeshAssets(str))
            {
                MakeMeshAssetBundle(str);
            }
            string path = movedAssets[i];
            if (path.Contains(UIDynamicAssetPatch))
            {
                bImportUI = true;
                AssetImporter assetImporter = AssetImporter.GetAtPath(path);

                PostprocessUITextureToCSV.nEditorInstance.RemoveConfig(PostprocessUITextureToCSV.GetGUID(assetImporter.assetPath));
            }
        }

        if (SoundSystem.SoundSystemEditorWindow.EditorInstance)
        {
            if (bMovedSound || bImportSound)
            {
                if (bMovedSound)
                {
                    SoundSystem.SoundSystemEditorWindow.RefrshAudioClipSource(movedGUID);
                }
                else
                {
                    SoundSystem.SoundSystemEditorWindow.RefrshAudioClipSource();
                }
            }
        }

        //UI的处理完保存配置
        if (bImportUI)
        {
            PostprocessUITextureToCSV.nEditorInstance.ExportConfig();
        }

        OnPostProcessScheme();

        if (!AssetBundleManagerEditor.isBuildingPackage && !AssetBundleManagerEditor.isCleaningAssets)
        {
            AssetBundleManager.LoadPrefabConfig();

            for (int i = 0; i < movedAssets.Length; i++)
            {
                string str = movedAssets[i];
                if (IsPrefabAssets(movedFromAssetPaths[i]))
                {
                    ClearSinglePrefab(str);
                }

                if (IsPrefabAssets(str))
                {
                    ImportSinglePrefab(str, true);
                }
            }


            for (int i = 0; i < importedAssets.Length; i++)
            {
                ImportSinglePrefab(importedAssets[i], true);
            }

            SchemeExport.Export_PrefabScheme();
            AssetBundleManager.ClearPrefabConfig();
        }


        //进入播放模式的时候会调用一次这个postallasets,所以要加载一次配置
        if (Application.isPlaying)
        {
            AssetBundleManager.LoadPrefabConfig();
        }

        if (movedAssets.Length > 0)
        {
            AssetDatabase.Refresh();
        }
        //移除无用的ab名
        AssetDatabase.RemoveUnusedAssetBundleNames();
    }
    private static void PostGeneratePrefabConfig()
    {
        List <string> fileList = new List <string>();

        fileList.AddRange(System.IO.Directory.GetFiles(AssetBundleManager.FullAssetPackagePathRoot, "*." + AssetBundleManager.PrefabVariant, SearchOption.AllDirectories));

        int    maxUnionCount     = 50;
        int    currentUnionCount = 0;
        int    curUnionIndex     = 0;
        string dstDir            = AssetBundleManager.FullAssetPackagePathRoot + AssetBundleManager.PrefabUnionVariant + "/";

        if (System.IO.Directory.Exists(dstDir))
        {
            System.IO.Directory.Delete(dstDir, true);
        }

        System.IO.Directory.CreateDirectory(dstDir);
        string     currFileName = dstDir + curUnionIndex + "." + AssetBundleManager.PrefabUnionVariant;
        FileStream unionFile    = File.Open(currFileName, FileMode.Append, FileAccess.Write);

        if (null == unionFile)
        {
            Debug.LogError("资源打包重大错误!--union");
            return;
        }
        int currDataOffset = 0;

        foreach (string filePath in fileList)
        {
            Byte[] srcData = File.ReadAllBytes(filePath);
            if (srcData.Length <= 0)
            {
                Debug.LogWarning("union + 读取文件失败:" + filePath);
                continue;
            }
            string abName;
            GetAssetBundNameFromAssetFilePath(filePath, out abName);
            if (!IsOnAssetBundle(abName))
            {
                Debug.LogWarning("IsOnAssetBundle失败:" + filePath);
                continue;
            }
            if (AssetBundleManager.SupplmentConfig(abName, curUnionIndex, currDataOffset, srcData.Length))
            {
                unionFile.Write(srcData, 0, srcData.Length);
                currDataOffset += srcData.Length;
                currentUnionCount++;
            }


            if (currentUnionCount >= maxUnionCount)
            {
                unionFile.Flush();
                unionFile.Close();

                curUnionIndex++;
                currentUnionCount = 0;
                currDataOffset    = 0;

                currFileName = dstDir + curUnionIndex + "." + AssetBundleManager.PrefabUnionVariant;
                unionFile    = File.Open(currFileName, FileMode.Append, FileAccess.Write);
            }
        }

        unionFile.Flush();
        unionFile.Close();

        SchemeExport.Export_PrefabScheme();
    }