• The file 'MemoryStream' is corrupted! 的解决办法


    The file 'MemoryStream' is corrupted! Remove it and launch unity again!
    [Position out of bounds! 20 > 16]

    有时候我们会遇到这个报错,然后整个U3D就崩溃了,原因是在于某些Prefabs的脚本引用丢失了,这个时候,只要把项目的所有丢失引用的Prefabs问题都解决了就OK了。

    那么问题来了,有几万个Prefab也手动去解决吗,不!这里有个方便你检查丢失引用的脚本,用这个就可以检查出所有的丢失Prefab了,需要注意的是有一些被列举出来的Prefab虽然没有丢失引用,然后是它的子项目丢失引用了,这个脚本是无法把哪一个子game object指出来的,这个时候,迩只能把指定的丢失的prefab拖到U3D的Hierarchy下然后仔细把所有的丢失引用问题解决就好了。

    如果一个丢失引用的Prefab迩拖到Hierarchy时它是不是显示是蓝色的,如果迩修复了所有的引用,点一下Apply看看是否变成蓝色,如果是的话那证明这个Prefab的所有丢失引用问题已经解决了。

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

    http://forum.unity3d.com/threads/unity-4-5-memory-stream-is-corrupted.248356/

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

  • 相关阅读:
    docker vm 性能优劣
    Jeecg-Boot Spring Boot
    MyBatis-Plus
    Eclipse lombok java
    MySQL 高可用性—keepalived+mysql双主
    如何使用 Docker 来限制 CPU、内存和 IO等资源?
    Requires: libc.so.6(GLIBC_2.14)(64bit)
    项目管理、软件、禅道 VS JIRA
    解决Window安全中心对Kitematic-0.17.3-Ubuntu.zip提示病毒,但无法删除的问题。
    正则表达式 贪婪模式
  • 原文地址:https://www.cnblogs.com/klobohyz/p/4375766.html
Copyright © 2020-2023  润新知