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 = new Regex("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 }
时间: 2024-10-09 20:10:02