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 }
AlterAssetBundle

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM