unity批量修改AssetBundleName与Variant
批量修改指定路径下的资源的AssetBundleName与Variant。
脚本代码如下:

1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEditor; 5 using System.IO; 6 /// <summary> 7 /// AlterAssetBundle类为修改批量修改AssetBundle的Name与Variant的编辑器窗口 8 /// </summary> 9 public class AlterAssetBundle : EditorWindow 10 { 11 12 [MenuItem("AssetsManager/批量修改AssetBundle")] 13 static void AddWindow() 14 { 15 //创建窗口 16 AlterAssetBundle window = (AlterAssetBundle)EditorWindow.GetWindow(typeof(AlterAssetBundle), false, "批量修改AssetBundle"); 17 window.Show(); 18 19 } 20 21 //输入文字的内容 22 private string Path = "Assets/Resources/", AssetBundleName="", Variant=""; 23 private bool IsThisName = true; 24 25 void OnGUI() 26 { 27 GUIStyle text_style = new GUIStyle(); 28 text_style.fontSize = 15; 29 text_style.alignment = TextAnchor.MiddleCenter; 30 31 EditorGUILayout.BeginHorizontal(); 32 GUILayout.Label("默认使用源文件名", GUILayout.MinWidth(120)); 33 IsThisName = EditorGUILayout.Toggle(IsThisName); 34 EditorGUILayout.EndHorizontal(); 35 36 EditorGUILayout.BeginHorizontal(); 37 GUILayout.Label("AssetBundleName:", GUILayout.MinWidth(120)); 38 if(IsThisName) 39 GUILayout.Label("源文件名.unity3d", GUILayout.MinWidth(120)); 40 else 41 AssetBundleName = EditorGUILayout.TextField(AssetBundleName.ToLower()); 42 EditorGUILayout.EndHorizontal(); 43 44 EditorGUILayout.BeginHorizontal(); 45 GUILayout.Label("Variant:", GUILayout.MinWidth(120)); 46 Variant = EditorGUILayout.TextField(Variant.ToLower()); 47 EditorGUILayout.EndHorizontal(); 48 49 GUILayout.Label("\n"); 50 51 EditorGUILayout.BeginHorizontal(); 52 GUILayout.Label("文件夹路径", GUILayout.MinWidth(60)); 53 if (GUILayout.Button("浏览", GUILayout.MinWidth(60))) { OpenFolder(); } 54 Path = EditorGUILayout.TextField(Path); 55 EditorGUILayout.EndHorizontal(); 56 if (GUILayout.Button("修改该文件夹下的AssetName及Variant")) { SetSettings(); } 57 if (GUILayout.Button("清除所有未被引用的AssetName及Variant")) { 58 AssetDatabase.RemoveUnusedAssetBundleNames(); 59 } 60 if (GUILayout.Button("清空所有AssetName及Variant")) 61 { 62 ClearAssetBundlesName(); 63 } 64 } 65 /// <summary> 66 /// 此函数用来打开文件夹修改路径 67 /// </summary> 68 void OpenFolder() 69 { 70 string m_path = EditorUtility.OpenFolderPanel("选择文件夹", "", ""); 71 if (!m_path.Contains(Application.dataPath)) 72 { 73 Debug.LogError("路径应在当前工程目录下"); 74 return; 75 } 76 if (m_path.Length != 0) 77 { 78 int firstindex = m_path.IndexOf("Assets"); 79 Path = m_path.Substring(firstindex) + "/"; 80 EditorUtility.FocusProjectWindow(); 81 } 82 } 83 /// <summary> 84 /// 此函数用来修改AssetBundleName与Variant 85 /// </summary> 86 void SetSettings() 87 { 88 if (Directory.Exists(Path)) 89 { 90 DirectoryInfo direction = new DirectoryInfo(Path); 91 FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories); 92 93 94 for (int i = 0; i < files.Length; i++) 95 { 96 if (files[i].Name.EndsWith(".meta")) 97 { 98 continue; 99 } 100 AssetImporter ai = AssetImporter.GetAtPath(files[i].FullName.Substring(files[i].FullName.IndexOf("Assets"))); 101 if(IsThisName) 102 ai.SetAssetBundleNameAndVariant(files[i].Name.Replace(".","_")+".unity3d", Variant); 103 else 104 ai.SetAssetBundleNameAndVariant(AssetBundleName, Variant); 105 } 106 AssetDatabase.Refresh(); 107 } 108 } 109 110 /// <summary> 111 /// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包 112 /// 工程中只要设置了AssetBundleName的,都会进行打包 113 /// </summary> 114 static void ClearAssetBundlesName() 115 { 116 int length = AssetDatabase.GetAllAssetBundleNames().Length; 117 string[] oldAssetBundleNames = new string[length]; 118 for (int i = 0; i < length; i++) 119 { 120 oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i]; 121 } 122 123 for (int j = 0; j < oldAssetBundleNames.Length; j++) 124 { 125 AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true); 126 } 127 } 128 void OnInspectorUpdate() 129 { 130 this.Repaint();//窗口的重绘 131 } 132 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步