UnityToLaya小外掛-找出空格並替換
阿新 • • 發佈:2020-09-11
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); } }