Unity 實現(xiàn)刪除missing腳本組件
通過Resources.FindObjectsOfTypeAll查找所有GameObject,然后通過.hideFlags == HideFlags.None判斷是否為存在于Hierarchy面板。(此為編輯器腳本)
詳細代碼:
/******************************************************************************* * 版本聲明: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(); } } } }
補充: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); } } }
建議采取方法二
已知兩種方法可能會出現(xiàn)某些錯誤,
方法二運行幾次會自動修復(fù)(方法一未測試)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#在MySQL大量數(shù)據(jù)下的高效讀取、寫入詳解
最近由于工作的原因,經(jīng)常需要對海量數(shù)據(jù)進行處理,做的數(shù)據(jù)爬蟲相關(guān),動輒千萬級別的數(shù)據(jù),單表幾十個G 都是都是家常便飯。 那么主要的開發(fā)語言是C#,數(shù)據(jù)庫使用的是MySQL。下面通過這篇文章我們來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實現(xiàn)2013-08-08使用C#調(diào)用百度地圖并實現(xiàn)坐標(biāo)點的設(shè)置以及讀取示例
這篇文章主要介紹了使用C#調(diào)用百度地圖并實現(xiàn)坐標(biāo)點的設(shè)置以及讀取示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07C#快速實現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個完整的猜拳游戲為例講述了C#面向?qū)ο蟪绦蛟O(shè)計的具體實現(xiàn)步驟,具有一定的學(xué)習(xí)與借鑒價值,需要的朋友可以參考下2014-11-11