zoukankan      html  css  js  c++  java
  • 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("
    ");
     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
  • 相关阅读:
    Session_End引发的性能问题!
    可能引发性能问题的几个写法,看看你占哪一个.
    优化你的DiscuzNT3.0,让它跑起来(2)发帖回帖篇
    什么是经济学
    生产可能性边界和机会成本
    九宫格的实现(转)
    LAMP的安装和配置
    iPhone 开发过程中的一些小技术的总结(转)
    有效的利用资源边际成本与边际利益
    Dijkstra算法(注:单源最短路径的贪心算法)和数学归纳法<转>
  • 原文地址:https://www.cnblogs.com/luxishi/p/6638534.html
Copyright © 2011-2022 走看看