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

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

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

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

 //Assets/Editor/SearchForComponents.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic; public class SearchForComponents : EditorWindow {
[MenuItem( "EDITORS/Search For Components" )]
static void Init () {
SearchForComponents window = (SearchForComponents) EditorWindow.GetWindow( typeof( SearchForComponents ) );
window.Show();
window.position = new Rect( , , , );
} string[] modes = new string[] { "Search for component usage", "Search for missing components" };
string[] checkType = new string[] { "Check single component", "Check all components" }; List<string> listResult;
List<ComponentNames> prefabComponents,notUsedComponents, addedComponents, existingComponents, sceneComponents;
int editorMode, selectedCheckType;
MonoScript targetComponent;
string componentName = ""; bool showPrefabs, showAdded, showScene, showUnused = true;
Vector2 scroll, scroll1, scroll2, scroll3, scroll4; class ComponentNames {
public string componentName;
public string namespaceName;
public string assetPath;
public List<string> usageSource;
public ComponentNames ( string comp, string space, string path ) {
this.componentName = comp;
this.namespaceName = space;
this.assetPath = path;
this.usageSource = new List<string>();
}
public override bool Equals ( object obj ) {
return ( (ComponentNames) obj ).componentName == componentName && ( (ComponentNames) obj ).namespaceName == namespaceName;
}
public override int GetHashCode () {
return componentName.GetHashCode() + namespaceName.GetHashCode();
}
} void OnGUI () {
GUILayout.Label(position+"");
GUILayout.Space( );
int oldValue = GUI.skin.window.padding.bottom;
GUI.skin.window.padding.bottom = -;
Rect windowRect = GUILayoutUtility.GetRect( , );
windowRect.x += ;
windowRect.width -= ;
editorMode = GUI.SelectionGrid( windowRect, editorMode, modes, , "Window" );
GUI.skin.window.padding.bottom = oldValue; switch ( editorMode ) {
case :
selectedCheckType = GUILayout.SelectionGrid( selectedCheckType, checkType, , "Toggle" );
GUI.enabled = selectedCheckType == ;
targetComponent = (MonoScript) EditorGUILayout.ObjectField( targetComponent, typeof( MonoScript ), false );
GUI.enabled = true; if ( GUILayout.Button( "Check component usage" ) ) {
AssetDatabase.SaveAssets();
switch ( selectedCheckType ) {
case :
componentName = targetComponent.name;
string targetPath = AssetDatabase.GetAssetPath( targetComponent );
string[] allPrefabs = GetAllPrefabs();
listResult = new List<string>();
foreach ( string prefab in allPrefabs ) {
string[] single = new string[] { prefab };
string[] dependencies = AssetDatabase.GetDependencies( single );
foreach ( string dependedAsset in dependencies ) {
if ( dependedAsset == targetPath ) {
listResult.Add( prefab );
}
}
}
break;
case :
List<string> scenesToLoad = new List<string>();
existingComponents = new List<ComponentNames>();
prefabComponents = new List<ComponentNames>();
notUsedComponents = new List<ComponentNames>();
addedComponents = new List<ComponentNames>();
sceneComponents = new List<ComponentNames>(); if ( EditorApplication.SaveCurrentSceneIfUserWantsTo() ) {
string projectPath = Application.dataPath;
projectPath = projectPath.Substring( , projectPath.IndexOf( "Assets" ) ); string[] allAssets = AssetDatabase.GetAllAssetPaths(); foreach ( string asset in allAssets ) {
int indexCS = asset.IndexOf( ".cs" );
int indexJS = asset.IndexOf( ".js" );
if ( indexCS != - || indexJS != - ) {
ComponentNames newComponent = new ComponentNames( NameFromPath( asset ), "", asset );
try {
System.IO.FileStream FS = new System.IO.FileStream( projectPath + asset, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite );
System.IO.StreamReader SR = new System.IO.StreamReader( FS );
string line;
while ( !SR.EndOfStream ) {
line = SR.ReadLine();
int index1 = line.IndexOf( "namespace" );
int index2 = line.IndexOf( "{" );
if ( index1 != - && index2 != - ) {
line = line.Substring( index1 + );
index2 = line.IndexOf( "{" );
line = line.Substring( , index2 );
line = line.Replace( " ", "" );
newComponent.namespaceName = line;
}
}
} catch {
} existingComponents.Add( newComponent ); try {
System.IO.FileStream FS = new System.IO.FileStream( projectPath + asset, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite );
System.IO.StreamReader SR = new System.IO.StreamReader( FS ); string line;
int lineNum = ;
while ( !SR.EndOfStream ) {
lineNum++;
line = SR.ReadLine();
int index = line.IndexOf( "AddComponent" );
if ( index != - ) {
line = line.Substring( index + );
if ( line[] == '(' ) {
line = line.Substring( , line.IndexOf( ')' ) - );
} else if ( line[] == '<' ) {
line = line.Substring( , line.IndexOf( '>' ) - );
} else {
continue;
}
line = line.Replace( " ", "" );
line = line.Replace( "\"", "" );
index = line.LastIndexOf( '.' );
ComponentNames newComp;
if ( index == - ) {
newComp = new ComponentNames( line, "", "" );
} else {
newComp = new ComponentNames( line.Substring( index + , line.Length - ( index + ) ), line.Substring( , index ), "" );
}
string pName = asset + ", Line " + lineNum;
newComp.usageSource.Add( pName );
index = addedComponents.IndexOf( newComp );
if ( index == - ) {
addedComponents.Add( newComp );
} else {
if ( !addedComponents[index].usageSource.Contains( pName ) ) addedComponents[index].usageSource.Add( pName );
}
}
}
} catch {
}
}
int indexPrefab = asset.IndexOf( ".prefab" ); if ( indexPrefab != - ) {
string[] single = new string[] { asset };
string[] dependencies = AssetDatabase.GetDependencies( single );
foreach ( string dependedAsset in dependencies ) {
if ( dependedAsset.IndexOf( ".cs" ) != - || dependedAsset.IndexOf( ".js" ) != - ) {
ComponentNames newComponent = new ComponentNames( NameFromPath( dependedAsset ), GetNamespaceFromPath( dependedAsset ), dependedAsset );
int index = prefabComponents.IndexOf( newComponent );
if ( index == - ) {
newComponent.usageSource.Add( asset );
prefabComponents.Add( newComponent );
} else {
if ( !prefabComponents[index].usageSource.Contains( asset ) ) prefabComponents[index].usageSource.Add( asset );
}
}
}
}
int indexUnity = asset.IndexOf( ".unity" );
if ( indexUnity != - ) {
scenesToLoad.Add( asset );
}
} for ( int i = addedComponents.Count - ; i > -; i-- ) {
addedComponents[i].assetPath = GetPathFromNames( addedComponents[i].namespaceName, addedComponents[i].componentName );
if ( addedComponents[i].assetPath == "" ) addedComponents.RemoveAt( i ); } foreach ( string scene in scenesToLoad ) {
EditorApplication.OpenScene( scene );
GameObject[] sceneGOs = GetAllObjectsInScene();
foreach ( GameObject g in sceneGOs ) {
Component[] comps = g.GetComponentsInChildren<Component>( true );
foreach ( Component c in comps ) { if ( c != null && c.GetType() != null && c.GetType().BaseType != null && c.GetType().BaseType == typeof( MonoBehaviour ) ) {
SerializedObject so = new SerializedObject( c );
SerializedProperty p = so.FindProperty( "m_Script" );
string path = AssetDatabase.GetAssetPath( p.objectReferenceValue );
ComponentNames newComp = new ComponentNames( NameFromPath( path ), GetNamespaceFromPath( path ), path );
newComp.usageSource.Add( scene );
int index = sceneComponents.IndexOf( newComp );
if ( index == - ) {
sceneComponents.Add( newComp );
} else {
if ( !sceneComponents[index].usageSource.Contains( scene ) ) sceneComponents[index].usageSource.Add( scene );
}
}
}
}
} foreach ( ComponentNames c in existingComponents ) {
if ( addedComponents.Contains( c ) ) continue;
if ( prefabComponents.Contains( c ) ) continue;
if ( sceneComponents.Contains( c ) ) continue;
notUsedComponents.Add( c );
} addedComponents.Sort( SortAlphabetically );
prefabComponents.Sort( SortAlphabetically );
sceneComponents.Sort( SortAlphabetically );
notUsedComponents.Sort( SortAlphabetically );
}
break;
}
}
break;
case :
if ( GUILayout.Button( "Search!" ) ) {
string[] allPrefabs = GetAllPrefabs();
listResult = new List<string>();
foreach ( string prefab in allPrefabs ) {
UnityEngine.Object o = AssetDatabase.LoadMainAssetAtPath( prefab );
GameObject go;
try {
go = (GameObject) o;
Component[] components = go.GetComponentsInChildren<Component>( true );
foreach ( Component c in components ) {
if ( c == null ) {
listResult.Add( prefab );
}
}
} catch {
Debug.Log( "For some reason, prefab " + prefab + " won't cast to GameObject" );
}
}
}
break;
}
if ( editorMode == || selectedCheckType == ) {
if ( listResult != null ) {
if ( listResult.Count == ) {
GUILayout.Label( editorMode == ? ( componentName == "" ? "Choose a component" : "No prefabs use component " + componentName ) : ( "No prefabs have missing components!\nClick Search to check again" ) );
} else {
GUILayout.Label( editorMode == ? ( "The following prefabs use component " + componentName + ":" ) : ( "The following prefabs have missing components:" ) );
scroll = GUILayout.BeginScrollView( scroll );
foreach ( string s in listResult ) {
GUILayout.BeginHorizontal();
GUILayout.Label( s, GUILayout.Width( position.width / ) );
if ( GUILayout.Button( "Select", GUILayout.Width( position.width / - ) ) ) {
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath( s );
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
}
} else {
showPrefabs = GUILayout.Toggle( showPrefabs, "Show prefab components" );
if ( showPrefabs ) {
GUILayout.Label( "The following components are attatched to prefabs:" );
DisplayResults( ref scroll1, ref prefabComponents );
}
showAdded = GUILayout.Toggle( showAdded, "Show AddComponent arguments" );
if ( showAdded ) {
GUILayout.Label( "The following components are AddComponent arguments:" );
DisplayResults( ref scroll2, ref addedComponents );
}
showScene = GUILayout.Toggle( showScene, "Show Scene-used components" );
if ( showScene ) {
GUILayout.Label( "The following components are used by scene objects:" );
DisplayResults( ref scroll3, ref sceneComponents );
}
showUnused = GUILayout.Toggle( showUnused, "Show Unused Components" );
if ( showUnused ) {
GUILayout.Label( "The following components are not used by prefabs, by AddComponent, OR in any scene:" );
DisplayResults( ref scroll4, ref notUsedComponents );
}
}
} int SortAlphabetically ( ComponentNames a, ComponentNames b ) {
return a.assetPath.CompareTo( b.assetPath );
} GameObject[] GetAllObjectsInScene () {
List<GameObject> objectsInScene = new List<GameObject>();
GameObject[] allGOs = (GameObject[]) Resources.FindObjectsOfTypeAll( typeof( GameObject ) );
foreach ( GameObject go in allGOs ) {
//if ( go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave )
// continue; string assetPath = AssetDatabase.GetAssetPath( go.transform.root.gameObject );
if ( !string.IsNullOrEmpty( assetPath ) )
continue; objectsInScene.Add( go );
} return objectsInScene.ToArray();
} void DisplayResults ( ref Vector2 scroller, ref List<ComponentNames> list ) {
if ( list == null ) return;
scroller = GUILayout.BeginScrollView( scroller );
foreach ( ComponentNames c in list ) {
GUILayout.BeginHorizontal();
GUILayout.Label( c.assetPath, GUILayout.Width( position.width / * ) );
if ( GUILayout.Button( "Select", GUILayout.Width( position.width / - ) ) ) {
Selection.activeObject = AssetDatabase.LoadMainAssetAtPath( c.assetPath );
}
GUILayout.EndHorizontal();
if ( c.usageSource.Count == ) {
GUILayout.Label( " In 1 Place: " + c.usageSource[] );
}
if ( c.usageSource.Count > ) {
GUILayout.Label( " In " + c.usageSource.Count + " Places: " + c.usageSource[] + ", " + c.usageSource[] + ( c.usageSource.Count > ? ", ..." : "" ) );
}
}
GUILayout.EndScrollView(); } string NameFromPath ( string s ) {
s = s.Substring( s.LastIndexOf( '/' ) + );
return s.Substring( , s.Length - );
} string GetNamespaceFromPath ( string path ) {
foreach ( ComponentNames c in existingComponents ) {
if ( c.assetPath == path ) {
return c.namespaceName;
}
}
return "";
} string GetPathFromNames ( string space, string name ) {
ComponentNames test = new ComponentNames( name, space, "" );
int index = existingComponents.IndexOf( test );
if ( index != - ) {
return existingComponents[index].assetPath;
}
return "";
} public static string[] GetAllPrefabs () {
string[] temp = AssetDatabase.GetAllAssetPaths();
List<string> result = new List<string>();
foreach ( string s in temp ) {
if ( s.Contains( ".prefab" ) ) result.Add( s );
}
return result.ToArray();
}
}

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

The file 'MemoryStream' is corrupted! 的解决办法的更多相关文章

  1. QT5.1在Windows下 出现QApplication: No such file or directory 问题的解决办法

    QT5.0.1在Windows下 出现QApplication: No such file or directory 问题的解决办法 分类: 编程语言学习 软件使用 QT编程学习2013-03-07 ...

  2. linux下解压大于4G文件提示error: Zip file too big错误的解决办法

    error: Zip file too big (greater than 4294959102 bytes)错误解决办法.zip文件夹大于4GB,在centos下无法正常unzip,需要使用第三方工 ...

  3. PHP连接MySQL报错"No such file or directory"的解决办法

    好下面说一下连接MYSQL数据库时报错的解决办法. 1,首先确定是mysql_connect()和mysql_pconnect()的问题,故障现象就是函数返回空,而mysql_error()返回“No ...

  4. Mac下PHP连接MySQL报错"No such file or directory"的解决办法

    首先做个简短的介绍. [说明1]MAC下MYSQL的安装路径: /usr/local/mysql-5.1.63-osx10.6-x86_64 数据库的数据文件在该目录的data文件夹中: 命令文件在b ...

  5. Linux运行shell脚本提示No such file or directory错误的解决办法

    Linux执行.sh文件,提示No such file or directory的问题: 原因:在windows中写好shell脚本测试正常,但是上传到 Linux 上以脚本方式运行命令时提示No s ...

  6. QT5.0.1在Windows下 出现QApplication: No such file or directory 问题的解决办法

    第一个Qt 程序 环境window ,ide qt creator 新建一个 C++ 项目 > 新建一个main.cpp 输入如下代码 #include<QApplication> ...

  7. iOS之报错“Cannot create __weak reference in file using manual reference counting”解决办法

    解决的办法:在Build Settings--------->Aplle LLVM8.0 - Language - Objectibe-C------------->Weak Refere ...

  8. CentOS7使用yum时File contains no section headers.解决办法

    本文转载于  https://blog.csdn.net/trokey/article/details/84908838 安装好CenOS7后,自带的yum不能直接使用,使用会出现如下问题: 原因是没 ...

  9. Qt 5 在Windows下 出现QApplication: No such file or directory 问题的解决办法

    解决方法是:在*.pro工程项目文件中添加一行QT += widgets,然后再编译运行就OK了.

随机推荐

  1. [ACM_图论] 棋盘问题 (棋盘上放棋子的方案数)

    不能同行同列,给定形状和大小的棋盘,求摆放k个棋子的可行方案 Input 2表示是2X2的棋盘,1表示k,#表示可放,点不可放(-1 -1 结束) Output 输出摆放的方案数目C Sample I ...

  2. [ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)

    Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...

  3. JQuery官方学习资料(译):使用JQuery的.index()方法

        .index()是一个JQuery对象方法,一般用于搜索JQuery对象上一个给定的元素.该方法有四种不同的函数签名,接下来将讲解这四种函数签名的具体用法. 无参数的.index() < ...

  4. paip.python php的未来预测以及它们的比较优缺点

    paip.python php的未来预测以及它们的比较优缺点 跟个php比..python有下列的优点: 1.桌面gui 功能强大. 主要是pyqt很好...而ruby qt 则好像不更新了..php ...

  5. 如何实现LBS轨迹回放功能?含多平台实现代码

    本篇文章告诉您,如何实现轨迹回放.并且提供了web端,iOS端,Android端3个平台的轨迹回放代码.拷贝后可以直接使用.另外,文末有小彩蛋,算是开发者的福利. Web端/JavaScript 实现 ...

  6. mysql5.5 uuid做主键与int做主键的性能实测

    数据库:mysql5.5 表类型:InnoDB 数据量:100W条 第一种情况: 主键采用uuid 32位. 运行查询语句1:SELECT COUNT(id) FROM test_varchar; 运 ...

  7. java 调用 .net webservice 示例

    String url="http://IP:端口/LisService.asmx"; String methodName="GetLisResultForBlood&qu ...

  8. 便宜有好货:Oracle免费的便捷Web应用开发框架

    APEX 总体来说,APEX是我见过最便捷最高效的开发框架,用起来比PHP还舒服.上手简单,学习成本极低,曾经有个做行政的小女生,在我指导下两天就可以开发出简单的审批管理站点.如果企业要做一些内部应用 ...

  9. Scala 深入浅出实战经典 第81讲:Scala中List的构造是的类型约束逆变、协变、下界详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-97讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  10. Scala 深入浅出实战经典 第60讲:Scala中隐式参数实战详解以及在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...