设置资源包名称
using UnityEditor;
using System.IO;
namespace Editor
{
public static class AssetBundleName
{
#region 字段
private static bool oneShader = true;
#endregion
#region 方法
[MenuItem("Assets/资源/设置资源包名称")]
private static void SetName()
{
Object[] objs = GetFiltered();
if (objs == null || objs.Length == 0) return;
string[] depends = GetDepends(objs);
int len = depends.Length;
for(int i=0;i<len;i++)
{
string path = depends[i];
Set(path);
}
EditorUtility.UnloadUnusedAssetsImmediate();
AssetDatabase.RemoveUnusedAssetBundleNames();
AssetDatabase.Refresh();
}
private static void Set(string path)
{
string sfx = Path.GetExtension(path);
if (!IsValidSfx(sfx)) return;
string name = Path.GetFileName(path);
if (name == Suffix.LightingDataName) return;
AssetImporter ai = AssetImporter.GetAtPath(path);
if (ai == null) return;
name = name.ToLower();
if(sfx==Suffix.Bytes)
{
var aName = Path.GetFileNameWithoutExtension(name);
var aSfx = Path.GetExtension(aName);
if(aSfx==Suffix.Lua)
{
SetNoABSfx(ai, Suffix.luaName);
}
else if(name==Suffix.fileName)
{
SetNoABSfx(ai, Suffix.fileName);
}
else
{
SetUnique(ai, name);
}
}
else if(sfx==Suffix.Shader)
{
if(oneShader)
{
Set(ai, Suffix.shaderName);
}
else
{
SetUnique(ai, name);
}
}
else
{
SetUnique(ai, name);
}
}
private static void Set(AssetImporter ai,string name)
{
if (ai == null) return;
if(ai.assetBundleName!=name)
{
ai.assetBundleName = name;
}
if(ai.assetBundleVariant!=Suffix.variant)
{
ai.assetBundleVariant = Suffix.variant;
}
}
private static Object[] GetFiltered()
{
Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
if(objs==null ||objs.Length==0)
{
string tip = "请选择文件夹或者文件";
EditorUtility.DisplayDialog("", tip, "确定");
}
return objs;
}
private static string[] GetDepends(Object[] objs)
{
if (objs == null || objs.Length == 0) return null;
List<string> paths = new List<string>();
int length = objs.Length;
for(int i=0;i<length;i++)
{
Object obj = objs[i];
if (obj == null) continue;
string path = AssetDatabase.GetAssetPath(obj);
if (string.IsNullOrEmpty(path)) continue;
if (paths.Contains(path)) continue;
paths.Add(path);
}
string[] pathArr = paths.ToArray();
string[] depends = AssetDatabase.GetDependencies(pathArr);
return depends;
}
private static void SetNoABSfx(AssetImporter ai,string name)
{
if (ai == null) return;
if(ai.assetBundleName!=name)
{
ai.assetBundleName = name;
}
ai.assetBundleVariant = null;
}
private static void SetUnique(AssetImporter ai,string name)
{
if(ai.assetBundleName!=name)
{
if (CheckUnique(name))
{
ai.assetBundleName = name;
}
else
{
string tip = string.Format("导入的文件:{0},和其他文件重名", ai.assetPath);
}
}
if(ai.assetBundleVariant!=Suffix.variant)
{
ai.assetBundleVariant = Suffix.variant;
}
}
private static bool CheckUnique(string name)
{
string[] paths = AssetDatabase.GetAssetPathsFromAssetBundle(name);
if (paths == null || paths.Length < 1) return true;
return false;
}
private static bool IsValidSfx(string sfx)
{
if (string.IsNullOrEmpty(sfx)) return false;
if (sfx == Suffix.CS) return false;
if (sfx == Suffix.Js) return false;
if (sfx == Suffix.Lua) return false;
if (sfx == Suffix.Zip) return false;
if (sfx == Suffix.AB) return false;
if (sfx == Suffix.Meta) return false;
if (sfx == Suffix.Manifest) return false;
if (sfx == Suffix.Psd) return false;
if (sfx == ".tbl") return false;
if (sfx == ".dll") return false;
return true;
}
#endregion
}
}
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Suffix
{
#region 字段
public const string None = "none";
public const string Scene = ".unity";
public const string Shader = ".shader";
public const string Png = ".png";
public const string Jpg = ".jpg";
public const string Tga = ".tga";
public const string Psd = ".psd";
public const string Mat = ".mat";
public const string PhysicMat = ".physicmaterial";
public const string Animation = ".anim";
public const string Animator = ".controller";
public const string AvatarMask = ".mask";
public const string Font = ".fontsetting";
public const string OTF = ".otf";
public const string TTF = ".ttf";
public const string GUISkin = ".skin";
public const string Fbx = ".fbx";
public const string Mb = ".mb";
public const string Prefab = ".prefab";
public const string Wav = ".wav";
public const string Mp3 = ".mp3";
public const string Ogg = ".ogg";
public const string Exr = ".exr";
public const string Asset = ".asset";
public const string AB = ".ab";
public const string CS = ".cs";
public const string Lua = ".lua";
public const string Js = ".js";
public const string Zip = ".zip";
public const string Meta = ".meta";
public const string Manifest = ".manifest";
public const string Txt = ".txt";
public const string Json = ".json";
public const string Xml = ".xml";
public const string Bytes = ".bytes";
public const string LightingDataName = "LightingData.asset";
public const string shaderName = "shader";
public const string luaName = "lua.bytes";
public const string libName = "CSHotfix";
public const string fileName = "png.bytes";
public static readonly string variant = AB.Replace(".", string.Empty);
}