unity 去除资源中的特殊字符

问题描述:

最近返现建模那边给的资源名包含一些#字符,导致加载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;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值