[MenuItem("Assets/预制体处理/CleanMissingScript", false, 6)]
static public void RemoveMissComponent()
{
//此处地址为根地址,代码会从这个地址的文件夹下开始遍历里面的所有文件夹的所有预置体以及它的子物体
foreach (var item in ResPathTool.GetSelectResPathLst("*.prefab"))
{
CheckMissMonoBehavior(item);
};
}
/// <summary>
/// 删除一个Prefab上的空脚本
/// </summary>
/// <param name="path">prefab路径 例Assets/Resources/FriendInfo.prefab</param>
static void CheckMissMonoBehavior(string path)
{
//先截取路径,使路径从ASSETS开始
int index = path.IndexOf("Assets/", StringComparison.CurrentCultureIgnoreCase);
string newPath = path.Substring(index);
GameObject obj = AssetDatabase.LoadAssetAtPath(newPath, typeof(GameObject)) as GameObject;
//实例化物体
GameObject go = PrefabUtility.InstantiatePrefab(obj) as GameObject;
//递归删除
searchChild(go);
// 将数据替换到asset
PrefabUtility.SaveAsPrefabAsset(go, newPath);
go.hideFlags = HideFlags.HideAndDontSave;
//删除掉实例化的对象
UnityEngine.Object.DestroyImmediate(go);
}
//递归物体的子物体
static void searchChild(GameObject gameObject)
{
int number = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObject);
if (gameObject.transform.childCount > 0)
{
for (int i = 0; i < gameObject.transform.childCount; i++)
{
searchChild(gameObject.transform.GetChild(i).gameObject);
}
}
}
清除Unity的missing脚本
最新推荐文章于 2024-06-27 18:54:11 发布