问题描述:
最近返现建模那边给的资源名包含一些#字符,导致加载AB时,报空的情况,一些模型资源名字包含一些特殊字符和很多空格.
在网上找了几个文章,都不是很适合,看整合了一下代码.
原因分析:
不知道啥原因
解决方案:
1.在编辑器里写一个扩展,自动检测资源名,删除特殊字符,空格.
在Project里面点击要操作的文件夹,然后调用扩展方法,把脚本放到Editor里面,
static Dictionary<string , int> ReNameGaNameAndNum = new Dictionary<string , int>();
[MenuItem("资源管理/去除Gameobject名字特殊字符")]
public static void ReName()
{
UnityEngine.Object[] m_objects = UnityEditor.Selection.GetFiltered(typeof(UnityEngine.Object) , UnityEditor.SelectionMode.DeepAssets);
//选择资源对象
ReNameGaNameAndNum = new Dictionary<string , int>();
int index = 0;//序号
for (int i = 0; i < m_objects.Length; i++)
{
if (System.IO.Path.GetExtension(UnityEditor.AssetDatabase.GetAssetPath(m_objects[i])) != "")//判断路径是否为空
{
string newName = m_objects[i].name;
// Regex reg = new Regex(@"[^a-zA-Z0-9\u4e00-\u9fa5\s]");
// Match m = reg.Match(newName);
bool isRegex = newName.Contains("#");
bool isRename = newName.Contains(" ");
string path = UnityEditor.AssetDatabase.GetAssetPath(m_objects[i]);
if (isRegex || isRename)
{
if (isRegex)
{
newName = newName.Replace("#" , "");
}
if (isRename)
{
newName = newName.Replace(" " , "");
}
}
newName = GetName(newName);
if (newName != m_objects[i].name)
UnityEditor.AssetDatabase.RenameAsset(path , newName);
}
index++;
EditorUtility.DisplayProgressBar("设置Gameobject名字中..." , string.Format("请稍等({0}/{1}) " , index , m_objects.Length) , index / m_objects.Length);
}
EditorUtility.ClearProgressBar();
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
ReNameGaNameAndNum.Clear();
}
private static string GetName(string name)
{
string newName = "";
if (ReNameGaNameAndNum.ContainsKey(name))
{
int num = ReNameGaNameAndNum[name];
newName = name + "_" + num;
ReNameGaNameAndNum[name] = num + 1;
newName = GetName(newName);
}
else
{
ReNameGaNameAndNum.Add(name , 1);
newName = name;
}
return newName;
}