示例#1
0
        public static void ResetBundleName(Dictionary <string, string> assetToBundle)
        {
            long elapsedMS = MDebug.GetMillisecondsSinceStartup();

            string[] assetPaths       = AssetDatabase.GetAllAssetPaths();
            int      modifyAssetCount = 0;

            for (int iAsset = 0; iAsset < assetPaths.Length; iAsset++)
            {
                if (iAsset % 100 == 0 &&
                    EditorUtility.DisplayCancelableProgressBar("AssetBundle"
                                                               , $"Reset bundleName {iAsset}/{assetPaths.Length}. Modified {modifyAssetCount}", iAsset / (float)assetPaths.Length))
                {
                    throw new CancelException();
                }

                string iterAssetPath = assetPaths[iAsset];
                // 没有meta文件的不需要设置bundleName
                if (!File.Exists(iterAssetPath + ".meta"))
                {
                    continue;
                }
                AssetImporter iterAssetImporter = AssetImporter.GetAtPath(iterAssetPath);
                if (!assetToBundle.TryGetValue(iterAssetPath, out string bundleName))
                {
                    bundleName = string.Empty;
                }

                if (iterAssetImporter.assetBundleName != bundleName)
                {
                    modifyAssetCount++;
                    iterAssetImporter.assetBundleName = bundleName;
                    EditorUtility.SetDirty(iterAssetImporter);
                }

                //1000间隔,执行一次IO操作,提高性能,如果中断可能会出现问题,比如BuildPipling获取不到一些已经赋值的BuildName
                if (iAsset % 1000 == 0)
                {
                    AssetDatabase.SaveAssets();
                    Resources.UnloadUnusedAssets();
                }
            }

            EditorUtility.ClearProgressBar();
            AssetDatabase.SaveAssets();
            Resources.UnloadUnusedAssets();
            AssetDatabase.Refresh();
            elapsedMS = MDebug.GetMillisecondsSinceStartup() - elapsedMS;
            MDebug.Log("AssetBundle"
                       , $"Modify bundleName count ({modifyAssetCount}) elapsed {MDebug.FormatMilliseconds(elapsedMS)}");
        }