解决Unity的The file 'MemoryStream' is corrupted! 崩溃问题

发表于2017-10-14
评论0 2.3k浏览

问题:   在项目平时删除资源或者脚本资源时产生的prefab的脚本引用丢失,特别是在场景scene中丢了
解决方案
/// 1、重新Clone项目
/// 2、删除项目的 Library 文件夹(推荐、解决紧急问题)
/// 3、使用这个脚本解决所有问题(这个当然推荐了)


http://forum.unity3d.com/threads/editor-want-to-check-all-prefabs-in-a-project-for-an-attached-monobehaviour.253149/#post-1673716

  1. using UnityEngine;  
  2. using UnityEditor;  
  3. using System.Collections;  
  4. using System.Collections.Generic;  
  5.   
  6. /// <summary>  
  7. /// 查看 prefab的脚本引用丢失  
  8. /// 像 : The file 'MemoryStream' is corrupted! Remove it and launch unity again!  [Position out of bounds! 20 > 16]  崩溃问题  
  9. ///   
  10. /// http://forum.unity3d.com/threads/unity-4-5-memory-stream-is-corrupted.248356/  
  11. /// http://forum.unity3d.com/threads/editor-want-to-check-all-prefabs-in-a-project-for-an-attached-monobehaviour.253149/#post-1673716  
  12. /// </summary>  
  13. public class SearchForComponents : EditorWindow  
  14. {  
  15.     [MenuItem("DajiaGame/Search For Components")]  
  16.     static void Init()  
  17.     {  
  18.         SearchForComponents window = (SearchForComponents)EditorWindow.GetWindow(typeof(SearchForComponents));  
  19.         window.Show();  
  20.         window.position = new Rect(20, 80, 550, 500);  
  21.     }  
  22.   
  23.     string[] modes = new string[] { "Search for component usage""Search for missing components" };  
  24.     string[] checkType = new string[] { "Check single component""Check all components" };  
  25.   
  26.     List<string> listResult;  
  27.     List<ComponentNames> prefabComponents, notUsedComponents, addedComponents, existingComponents, sceneComponents;  
  28.     int editorMode, selectedCheckType;  
  29.     MonoScript targetComponent;  
  30.     string componentName = "";  
  31.   
  32.     bool showPrefabs, showAdded, showScene, showUnused = true;  
  33.     Vector2 scroll, scroll1, scroll2, scroll3, scroll4;  
  34.   
  35.     class ComponentNames  
  36.     {  
  37.         public string componentName;  
  38.         public string namespaceName;  
  39.         public string assetPath;  
  40.         public List<string> usageSource;  
  41.         public ComponentNames(string comp, string space, string path)  
  42.         {  
  43.             this.componentName = comp;  
  44.             this.namespaceName = space;  
  45.             this.assetPath = path;  
  46.             this.usageSource = new List<string>();  
  47.         }  
  48.         public override bool Equals(object obj)  
  49.         {  
  50.             return ((ComponentNames)obj).componentName == componentName && ((ComponentNames)obj).namespaceName == namespaceName;  
  51.         }  
  52.         public override int GetHashCode()  
  53.         {  
  54.             return componentName.GetHashCode()   namespaceName.GetHashCode();  
  55.         }  
  56.     }  
  57.   
  58.     void OnGUI()  
  59.     {  
  60.         GUILayout.Label(position   "");  
  61.         GUILayout.Space(3);  
  62.         int oldValue = GUI.skin.window.padding.bottom;  
  63.         GUI.skin.window.padding.bottom = -20;  
  64.         Rect windowRect = GUILayoutUtility.GetRect(1, 17);  
  65.         windowRect.x  = 4;  
  66.         windowRect.width -= 7;  
  67.         editorMode = GUI.SelectionGrid(windowRect, editorMode, modes, 2, "Window");  
  68.         GUI.skin.window.padding.bottom = oldValue;  
  69.   
  70.         switch (editorMode)  
  71.         {  
  72.             case 0:  
  73.                 selectedCheckType = GUILayout.SelectionGrid(selectedCheckType, checkType, 2, "Toggle");  
  74.                 GUI.enabled = selectedCheckType == 0;  
  75.                 targetComponent = (MonoScript)EditorGUILayout.ObjectField(targetComponent, typeof(MonoScript), false);  
  76.                 GUI.enabled = true;  
  77.   
  78.                 if (GUILayout.Button("Check component usage"))  
  79.                 {  
  80.                     AssetDatabase.SaveAssets();  
  81.                     switch (selectedCheckType)  
  82.                     {  
  83.                         case 0:  
  84.                             componentName = targetComponent.name;  
  85.                             string targetPath = AssetDatabase.GetAssetPath(targetComponent);  
  86.                             string[] allPrefabs = GetAllPrefabs();  
  87.                             listResult = new List<string>();  
  88.                             foreach (string prefab in allPrefabs)  
  89.                             {  
  90.                                 string[] single = new string[] { prefab };  
  91.                                 string[] dependencies = AssetDatabase.GetDependencies(single);  
  92.                                 foreach (string dependedAsset in dependencies)  
  93.                                 {  
  94.                                     if (dependedAsset == targetPath)  
  95.                                     {  
  96.                                         listResult.Add(prefab);  
  97.                                     }  
  98.                                 }  
  99.                             }  
  100.                             break;  
  101.                         case 1:  
  102.                             List<string> scenesToLoad = new List<string>();  
  103.                             existingComponents = new List<ComponentNames>();  
  104.                             prefabComponents = new List<ComponentNames>();  
  105.                             notUsedComponents = new List<ComponentNames>();  
  106.                             addedComponents = new List<ComponentNames>();  
  107.                             sceneComponents = new List<ComponentNames>();  
  108.   
  109.                             if (EditorApplication.SaveCurrentSceneIfUserWantsTo())  
  110.                             {  
  111.                                 string projectPath = Application.dataPath;  
  112.                                 projectPath = projectPath.Substring(0, projectPath.IndexOf("Assets"));  
  113.   
  114.                                 string[] allAssets = AssetDatabase.GetAllAssetPaths();  
  115.   
  116.                                 foreach (string asset in allAssets)  
  117.                                 {  
  118.                                     int indexCS = asset.IndexOf(".cs");  
  119.                                     int indexJS = asset.IndexOf(".js");  
  120.                                     if (indexCS != -1 || indexJS != -1)  
  121.                                     {  
  122.                                         ComponentNames newComponent = new ComponentNames(NameFromPath(asset), "", asset);  
  123.                                         try  
  124.                                         {  
  125.                                             System.IO.FileStream FS = new System.IO.FileStream(projectPath   asset, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);  
  126.                                             System.IO.StreamReader SR = new System.IO.StreamReader(FS);  
  127.                                             string line;  
  128.                                             while (!SR.EndOfStream)  
  129.                                             {  
  130.                                                 line = SR.ReadLine();  
  131.                                                 int index1 = line.IndexOf("namespace");  
  132.                                                 int index2 = line.IndexOf("{");  
  133.                                                 if (index1 != -1 && index2 != -1)  
  134.                                                 {  
  135.                                                     line = line.Substring(index1   9);  
  136.                                                     index2 = line.IndexOf("{");  
  137.                                                     line = line.Substring(0, index2);  
  138.                                                     line = line.Replace(" """);  
  139.                                                     newComponent.namespaceName = line;  
  140.                                                 }  
  141.                                             }  
  142.                                         }  
  143.                                         catch  
  144.                                         {  
  145.                                         }  
  146.   
  147.                                         existingComponents.Add(newComponent);  
  148.   
  149.                                         try  
  150.                                         {  
  151.                                             System.IO.FileStream FS = new System.IO.FileStream(projectPath   asset, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);  
  152.                                             System.IO.StreamReader SR = new System.IO.StreamReader(FS);  
  153.   
  154.                                             string line;  
  155.                                             int lineNum = 0;  
  156.                                             while (!SR.EndOfStream)  
  157.                                             {  
  158.                                                 lineNum ;  
  159.                                                 line = SR.ReadLine();  
  160.                                                 int index = line.IndexOf("AddComponent");  
  161.                                                 if (index != -1)  
  162.                                                 {  
  163.                                                     line = line.Substring(index   12);  
  164.                                                     if (line[0] == '(')  
  165.                                                     {  
  166.                                                         line = line.Substring(1, line.IndexOf(')') - 1);  
  167.                                                     }  
  168.                                                     else if (line[0] == '<')  
  169.                                                     {  
  170.                                                         line = line.Substring(1, line.IndexOf('>') - 1);  
  171.                                                     }  
  172.                                                     else  
  173.                                                     {  
  174.                                                         continue;  
  175.                                                     }  
  176.                                                     line = line.Replace(" """);  
  177.                                                     line = line.Replace("\"""");  
  178.                                                     index = line.LastIndexOf('.');  
  179.                                                     ComponentNames newComp;  
  180.                                                     if (index == -1)  
  181.                                                     {  
  182.                                                         newComp = new ComponentNames(line, """");  
  183.                                                     }  
  184.                                                     else  
  185.                                                     {  
  186.                                                         newComp = new ComponentNames(line.Substring(index   1, line.Length - (index   1)), line.Substring(0, index), "");  
  187.                                                     }  
  188.                                                     string pName = asset   ", Line "   lineNum;  
  189.                                                     newComp.usageSource.Add(pName);  
  190.                                                     index = addedComponents.IndexOf(newComp);  
  191.                                                     if (index == -1)  
  192.                                                     {  
  193.                                                         addedComponents.Add(newComp);  
  194.                                                     }  
  195.                                                     else  
  196.                                                     {  
  197.                                                         if (!addedComponents[index].usageSource.Contains(pName)) addedComponents[index].usageSource.Add(pName);  
  198.                                                     }  
  199.                                                 }  
  200.                                             }  
  201.                                         }  
  202.                                         catch  
  203.                                         {  
  204.                                         }  
  205.                                     }  
  206.                                     int indexPrefab = asset.IndexOf(".prefab");  
  207.   
  208.                                     if (indexPrefab != -1)  
  209.                                     {  
  210.                                         string[] single = new string[] { asset };  
  211.                                         string[] dependencies = AssetDatabase.GetDependencies(single);  
  212.                                         foreach (string dependedAsset in dependencies)  
  213.                                         {  
  214.                                             if (dependedAsset.IndexOf(".cs") != -1 || dependedAsset.IndexOf(".js") != -1)  
  215.                                             {  
  216.                                                 ComponentNames newComponent = new ComponentNames(NameFromPath(dependedAsset), GetNamespaceFromPath(dependedAsset), dependedAsset);  
  217.                                                 int index = prefabComponents.IndexOf(newComponent);  
  218.                                                 if (index == -1)  
  219.                                                 {  
  220.                                                     newComponent.usageSource.Add(asset);  
  221.                                                     prefabComponents.Add(newComponent);  
  222.                                                 }  
  223.                                                 else  
  224.                                                 {  
  225.                                                     if (!prefabComponents[index].usageSource.Contains(asset)) prefabComponents[index].usageSource.Add(asset);  
  226.                                                 }  
  227.                                             }  
  228.                                         }  
  229.                                     }  
  230.                                     int indexUnity = asset.IndexOf(".unity");  
  231.                                     if (indexUnity != -1)  
  232.                                     {  
  233.                                         scenesToLoad.Add(asset);  
  234.                                     }  
  235.                                 }  
  236.   
  237.                                 for (int i = addedComponents.Count - 1; i > -1; i--)  
  238.                                 {  
  239.                                     addedComponents[i].assetPath = GetPathFromNames(addedComponents[i].namespaceName, addedComponents[i].componentName);  
  240.                                     if (addedComponents[i].assetPath == "") addedComponents.RemoveAt(i);  
  241.   
  242.                                 }  
  243.   
  244.                                 foreach (string scene in scenesToLoad)  
  245.                                 {  
  246.                                     EditorApplication.OpenScene(scene);  
  247.                                     GameObject[] sceneGOs = GetAllObjectsInScene();  
  248.                                     foreach (GameObject g in sceneGOs)  
  249.                                     {  
  250.                                         Component[] comps = g.GetComponentsInChildren<Component>(true);  
  251.                                         foreach (Component c in comps)  
  252.                                         {  
  253.   
  254.                                             if (c != null && c.GetType() != null && c.GetType().BaseType != null && c.GetType().BaseType == typeof(MonoBehaviour))  
  255.                                             {  
  256.                                                 SerializedObject so = new SerializedObject(c);  
  257.                                                 SerializedProperty p = so.FindProperty("m_Script");  
  258.                                                 string path = AssetDatabase.GetAssetPath(p.objectReferenceValue);  
  259.                                                 ComponentNames newComp = new ComponentNames(NameFromPath(path), GetNamespaceFromPath(path), path);  
  260.                                                 newComp.usageSource.Add(scene);  
  261.                                                 int index = sceneComponents.IndexOf(newComp);  
  262.                                                 if (index == -1)  
  263.                                                 {  
  264.                                                     sceneComponents.Add(newComp);  
  265.                                                 }  
  266.                                                 else  
  267.                                                 {  
  268.                                                     if (!sceneComponents[index].usageSource.Contains(scene)) sceneComponents[index].usageSource.Add(scene);  
  269.                                                 }  
  270.                                             }  
  271.                                         }  
  272.                                     }  
  273.                                 }  
  274.   
  275.                                 foreach (ComponentNames c in existingComponents)  
  276.                                 {  
  277.                                     if (addedComponents.Contains(c)) continue;  
  278.                                     if (prefabComponents.Contains(c)) continue;  
  279.                                     if (sceneComponents.Contains(c)) continue;  
  280.                                     notUsedComponents.Add(c);  
  281.                                 }  
  282.   
  283.                                 addedComponents.Sort(SortAlphabetically);  
  284.                                 prefabComponents.Sort(SortAlphabetically);  
  285.                                 sceneComponents.Sort(SortAlphabetically);  
  286.                                 notUsedComponents.Sort(SortAlphabetically);  
  287.                             }  
  288.                             break;  
  289.                     }  
  290.                 }  
  291.                 break;  
  292.             case 1:  
  293.                 if (GUILayout.Button("Search!"))  
  294.                 {  
  295.                     string[] allPrefabs = GetAllPrefabs();  
  296.                     listResult = new List<string>();  
  297.                     foreach (string prefab in allPrefabs)  
  298.                     {  
  299.                         UnityEngine.Object o = AssetDatabase.LoadMainAssetAtPath(prefab);  
  300.                         GameObject go;  
  301.                         try  
  302.                         {  
  303.                             go = (GameObject)o;  
  304.                             Component[] components = go.GetComponentsInChildren<Component>(true);  
  305.                             foreach (Component c in components)  
  306.                             {  
  307.                                 if (c == null)  
  308.                                 {  
  309.                                     listResult.Add(prefab);  
  310.                                 }  
  311.                             }  
  312.                         }  
  313.                         catch  
  314.                         {  
  315.                             Debug.Log("For some reason, prefab "   prefab   " won't cast to GameObject");  
  316.                         }  
  317.                     }  
  318.                 }  
  319.                 break;  
  320.         }  
  321.         if (editorMode == 1 || selectedCheckType == 0)  
  322.         {  
  323.             if (listResult != null)  
  324.             {  
  325.                 if (listResult.Count == 0)  
  326.                 {  
  327.                     GUILayout.Label(editorMode == 0 ? (componentName == "" ? "Choose a component" : "No prefabs use component "   componentName) : ("No prefabs have missing components!\nClick Search to check again"));  
  328.                 }  
  329.                 else  
  330.                 {  
  331.                     GUILayout.Label(editorMode == 0 ? ("The following prefabs use component "   componentName   ":") : ("The following prefabs have missing components:"));  
  332.                     scroll = GUILayout.BeginScrollView(scroll);  
  333.                     foreach (string s in listResult)  
  334.                     {  
  335.                         GUILayout.BeginHorizontal();  
  336.                         GUILayout.Label(s, GUILayout.Width(position.width / 2));  
  337.                         if (GUILayout.Button("Select", GUILayout.Width(position.width / 2 - 10)))  
  338.                         {  
  339.                             Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(s);  
  340.                         }  
  341.                         GUILayout.EndHorizontal();  
  342.                     }  
  343.                     GUILayout.EndScrollView();  
  344.                 }  
  345.             }  
  346.         }  
  347.         else  
  348.         {  
  349.             showPrefabs = GUILayout.Toggle(showPrefabs, "Show prefab components");  
  350.             if (showPrefabs)  
  351.             {  
  352.                 GUILayout.Label("The following components are attatched to prefabs:");  
  353.                 DisplayResults(ref scroll1, ref prefabComponents);  
  354.             }  
  355.             showAdded = GUILayout.Toggle(showAdded, "Show AddComponent arguments");  
  356.             if (showAdded)  
  357.             {  
  358.                 GUILayout.Label("The following components are AddComponent arguments:");  
  359.                 DisplayResults(ref scroll2, ref addedComponents);  
  360.             }  
  361.             showScene = GUILayout.Toggle(showScene, "Show Scene-used components");  
  362.             if (showScene)  
  363.             {  
  364.                 GUILayout.Label("The following components are used by scene objects:");  
  365.                 DisplayResults(ref scroll3, ref sceneComponents);  
  366.             }  
  367.             showUnused = GUILayout.Toggle(showUnused, "Show Unused Components");  
  368.             if (showUnused)  
  369.             {  
  370.                 GUILayout.Label("The following components are not used by prefabs, by AddComponent, OR in any scene:");  
  371.                 DisplayResults(ref scroll4, ref notUsedComponents);  
  372.             }  
  373.         }  
  374.     }  
  375.   
  376.     int SortAlphabetically(ComponentNames a, ComponentNames b)  
  377.     {  
  378.         return a.assetPath.CompareTo(b.assetPath);  
  379.     }  
  380.   
  381.     GameObject[] GetAllObjectsInScene()  
  382.     {  
  383.         List<GameObject> objectsInScene = new List<GameObject>();  
  384.         GameObject[] allGOs = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));  
  385.         foreach (GameObject go in allGOs)  
  386.         {  
  387.             //if ( go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave )  
  388.             //    continue;  
  389.   
  390.             string assetPath = AssetDatabase.GetAssetPath(go.transform.root.gameObject);  
  391.             if (!string.IsNullOrEmpty(assetPath))  
  392.                 continue;  
  393.   
  394.             objectsInScene.Add(go);  
  395.         }  
  396.   
  397.         return objectsInScene.ToArray();  
  398.     }  
  399.   
  400.     void DisplayResults(ref Vector2 scroller, ref List<ComponentNames> list)  
  401.     {  
  402.         if (list == nullreturn;  
  403.         scroller = GUILayout.BeginScrollView(scroller);  
  404.         foreach (ComponentNames c in list)  
  405.         {  
  406.             GUILayout.BeginHorizontal();  
  407.             GUILayout.Label(c.assetPath, GUILayout.Width(position.width / 5 * 4));  
  408.             if (GUILayout.Button("Select", GUILayout.Width(position.width / 5 - 30)))  
  409.             {  
  410.                 Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(c.assetPath);  
  411.             }  
  412.             GUILayout.EndHorizontal();  
  413.             if (c.usageSource.Count == 1)  
  414.             {  
  415.                 GUILayout.Label("   In 1 Place: "   c.usageSource[0]);  
  416.             }  
  417.             if (c.usageSource.Count > 1)  
  418.             {  
  419.                 GUILayout.Label("   In "   c.usageSource.Count   " Places: "   c.usageSource[0]   ", "   c.usageSource[1]   (c.usageSource.Count > 2 ? ", ..." : ""));  
  420.             }  
  421.         }  
  422.         GUILayout.EndScrollView();  
  423.   
  424.     }  
  425.   
  426.     string NameFromPath(string s)  
  427.     {  
  428.         s = s.Substring(s.LastIndexOf('/')   1);  
  429.         return s.Substring(0, s.Length - 3);  
  430.     }  
  431.   
  432.     string GetNamespaceFromPath(string path)  
  433.     {  
  434.         foreach (ComponentNames c in existingComponents)  
  435.         {  
  436.             if (c.assetPath == path)  
  437.             {  
  438.                 return c.namespaceName;  
  439.             }  
  440.         }  
  441.         return "";  
  442.     }  
  443.   
  444.     string GetPathFromNames(string space, string name)  
  445.     {  
  446.         ComponentNames test = new ComponentNames(name, space, "");  
  447.         int index = existingComponents.IndexOf(test);  
  448.         if (index != -1)  
  449.         {  
  450.             return existingComponents[index].assetPath;  
  451.         }  
  452.         return "";  
  453.     }  
  454.   
  455.     public static string[] GetAllPrefabs()  
  456.     {  
  457.         string[] temp = AssetDatabase.GetAllAssetPaths();  
  458.         List<string> result = new List<string>();  
  459.         foreach (string s in temp)  
  460.         {  
  461.             if (s.Contains(".prefab")) result.Add(s);  
  462.         }  
  463.         return result.ToArray();  
  464.     }  
  465. }  
http://blog.csdn.net/u010019717/article/details/47125795

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: