Unity 實(shí)現(xiàn)刪除missing腳本組件
通過Resources.FindObjectsOfTypeAll查找所有GameObject,然后通過.hideFlags == HideFlags.None判斷是否為存在于Hierarchy面板。(此為編輯器腳本)
詳細(xì)代碼:
/*******************************************************************************
* 版本聲明:v1.0.0
* 類 名 稱:DeleteMissingScripts
* 創(chuàng)建日期:8/10/2019 5:04:13 PM
* 作者名稱:末零
* 功能描述:刪除所有Miss的腳本
******************************************************************************/
using UnityEngine;
using UnityEditor;
public class DeleteMissingScripts
{
[MenuItem("MyTools/Delete Missing Scripts")]
static void CleanupMissingScript()
{
GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
int r;
int j;
for (int i = 0; i < pAllObjects.Length; i++)
{
if (pAllObjects[i].hideFlags == HideFlags.None)//HideFlags.None 獲取Hierarchy面板所有Object
{
var components = pAllObjects[i].GetComponents<Component>();
var serializedObject = new SerializedObject(pAllObjects[i]);
var prop = serializedObject.FindProperty("m_Component");
r = 0;
for (j = 0; j < components.Length; j++)
{
if (components[j] == null)
{
prop.DeleteArrayElementAtIndex(j - r);
r++;
}
}
serializedObject.ApplyModifiedProperties();
}
}
}
}
補(bǔ)充:Unity中一鍵刪除所有已失效的腳本
如下所示:
//刪除所有Miss的腳本
using UnityEngine;
using UnityEditor;
public class DeleteMissingScripts
{
[MenuItem("MyTools/Delete Missing Scripts")]
static void CleanupMissingScript()
{
GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
int r;
int j;
for (int i = 0; i < pAllObjects.Length; i++)
{
if (pAllObjects[i].hideFlags == HideFlags.None)//HideFlags.None 獲取Hierarchy面板所有Object
{
var components = pAllObjects[i].GetComponents<Component>();
var serializedObject = new SerializedObject(pAllObjects[i]);
var prop = serializedObject.FindProperty("m_Component");
r = 0;
for (j = 0; j < components.Length; j++)
{
if (components[j] == null)
{
prop.DeleteArrayElementAtIndex(j - r);
r++;
}
}
serializedObject.ApplyModifiedProperties();
}
}
}
}
此為編輯器腳本
使用方法:

方法二:
using UnityEngine;
using UnityEditor;
public class DeleteMissingScripts
{
[MenuItem("Edit/Cleanup Missing Scripts")]
static void CleanupMissingScripts()
{
for (int i = 0; i < Selection.gameObjects.Length; i++)
{
var gameObject = Selection.gameObjects[i];
// We must use the GetComponents array to actually detect missing components
var components = gameObject.GetComponents<Component>();
// Create a serialized object so that we can edit the component list
var serializedObject = new SerializedObject(gameObject);
// Find the component list property
var prop = serializedObject.FindProperty("m_Component");
// Track how many components we've removed
int r = 0;
// Iterate over all components
for (int j = 0; j < components.Length; j++)
{
// Check if the ref is null
if (components[j] == null)
{
// If so, remove from the serialized component array
prop.DeleteArrayElementAtIndex(j - r);
// Increment removed count
r++;
}
}
// Apply our changes to the game object
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(gameObject);
}
}
}
建議采取方法二
已知兩種方法可能會(huì)出現(xiàn)某些錯(cuò)誤,
方法二運(yùn)行幾次會(huì)自動(dòng)修復(fù)(方法一未測試)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#在MySQL大量數(shù)據(jù)下的高效讀取、寫入詳解
最近由于工作的原因,經(jīng)常需要對海量數(shù)據(jù)進(jìn)行處理,做的數(shù)據(jù)爬蟲相關(guān),動(dòng)輒千萬級別的數(shù)據(jù),單表幾十個(gè)G 都是都是家常便飯。 那么主要的開發(fā)語言是C#,數(shù)據(jù)庫使用的是MySQL。下面通過這篇文章我們來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實(shí)現(xiàn)2013-08-08
使用C#調(diào)用百度地圖并實(shí)現(xiàn)坐標(biāo)點(diǎn)的設(shè)置以及讀取示例
這篇文章主要介紹了使用C#調(diào)用百度地圖并實(shí)現(xiàn)坐標(biāo)點(diǎn)的設(shè)置以及讀取示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個(gè)完整的猜拳游戲?yàn)槔v述了C#面向?qū)ο蟪绦蛟O(shè)計(jì)的具體實(shí)現(xiàn)步驟,具有一定的學(xué)習(xí)與借鑒價(jià)值,需要的朋友可以參考下2014-11-11
C# SQLite序列操作實(shí)現(xiàn)方法詳解
這篇文章主要介紹了C# SQLite序列操作實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了C#實(shí)現(xiàn)SQLite序列操作的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
c# 如何實(shí)現(xiàn)一個(gè)簡單的json解析器
這篇文章主要介紹了c# 如何實(shí)現(xiàn)一個(gè)簡單的json解析器,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

