UnityToLaya小插件-找出空格并替换

unity导出的文件中经常会出现带有空格的节点或者文件夹

而这些空格在本地开发测试过程中不会出现,当这些带有空格路径的文件需要放到网络上时,就出现问题了

所以这里写了一个简单的查找并清理空格的插件,

 

代码如下:

using System.Data.SqlTypes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class FindSpaceAndClear
{
    [MenuItem("GameObject/查找空格并替换", false, 11)]
    public static void FindSpaceInGameObject()
    {
        GameObject obj = Selection.activeObject as GameObject;
        Debug.Log(obj.name);
        Transform[] transforms = obj.transform.GetComponentsInChildren<Transform>(true);
        for (int i = 0; i < transforms.Length; i++)
        {
            string objname = transforms[i].gameObject.name;
            if (objname.Contains(" "))
            {
                Debug.Log(objname);
                string reobjname = objname.Replace(" ", "_");
                transforms[i].gameObject.name = reobjname;
            }
        }
    }
    [MenuItem("Assets/查找空格并替换", false, 11)]
    public static void FindSpaceInAssets()
    {
        Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);//选择的所以对象
        int Count=0;
        for (int i = 0; i < m_objects.Length; i++)
        {
             string path = AssetDatabase.GetAssetPath(m_objects[i]);
                string oldname = m_objects[i].name;
                if (oldname.Contains(" "))
                {
                     Count++;
                    string newname = oldname.Replace(" ", "_");
                    AssetDatabase.RenameAsset(path, newname);
                    Debug.Log("重命名文件:"+oldname+"    重命名文件个数:"+Count);
                }
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.Log("重命名文件总个数为:"+Count);
    }
}

 

 

演示预览: 

https://www.bilibili.com/video/BV1h64y1F7GZ/

posted @ 2020-09-11 14:52  nuolo  阅读(332)  评论(0)    收藏  举报