文件流方式 刪除prefab空腳本
阿新 • • 發佈:2017-12-04
type bsp gui ++ 引用 [] friend gsp all
1 /// <summary> 2 /// 刪除一個Prefab上的空腳本 3 /// </summary> 4 /// <param name="path">prefab路徑 例Assets/Resources/FriendInfo.prefab</param> 5 private void DeleteNullScript(string path) 6 { 7 bool isNull = false; 8 string s = File.ReadAllText(path); 9 10 Regex regBlock = newRegex("MonoBehaviour"); 11 12 // 以"---"劃分組件 13 string[] strArray = s.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries); 14 15 for (int i = 0; i < strArray.Length; i++) 16 { 17 string blockStr = strArray[i]; 18 19 if (regBlock.IsMatch(blockStr))20 { 21 // 模塊是 MonoBehavior 22 Match guidMatch = Regex.Match(blockStr, "m_Script: {fileID: (.*), guid: (?<GuidValue>.*?), type:"); 23 if (guidMatch.Success) 24 { 25 // 獲取 MonoBehavior的guid 26 string guid = guidMatch.Groups["GuidValue"].Value; 27 //Debug.Log("Guid:" + guid); 28 29 if (string.IsNullOrEmpty(GetScriptPath(guid))) 30 { 31 // 工程中無此腳本 空腳本!!! 32 //Debug.Log("空腳本"); 33 isNull = true; 34 35 // 刪除操作 36 37 // 刪除MonoScript 38 s = s.Replace("---" + blockStr, ""); 39 40 Match idMatch = Regex.Match(blockStr, "!u!(.*) &(?<idValue>.*?)\r"); 41 if (idMatch.Success) 42 { 43 // 獲取 MonoBehavior的guid 44 string id = idMatch.Groups["idValue"].Value; 45 46 // 刪除MonoScript的引用 47 Regex quote = new Regex(" - (.*): {fileID: " + id + "}"); 48 s = quote.Replace(s, ""); 49 } 50 51 } 52 53 } 54 55 } 56 57 58 } 59 60 if (isNull) 61 { 62 // 有空腳本 寫回prefab 63 File.WriteAllText(path, s); 64 65 // 打印Log 66 Debug.Log(path); 67 } 68 }
文件流方式 刪除prefab空腳本