zoukankan      html  css  js  c++  java
  • 文件流方式 删除prefab空脚本

     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>.*?)
    ");  
    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 }  
  • 相关阅读:
    modf()函数
    面向对象编程五大原则
    .Net网络资源
    整理CentOS常用命令
    在RHEL5上安装oracle10gLinux
    strchr()函数
    swab函数
    Strstr()函数
    tmpnam函数
    strdup ()函数
  • 原文地址:https://www.cnblogs.com/AaronBlogs/p/7976054.html
Copyright © 2011-2022 走看看