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ù)(方法一未測(cè)試)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
這篇文章主要介紹了C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件),需要的朋友可以參考下2015-09-09
Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟
unity編輯器中打開C#腳本的時(shí)候發(fā)現(xiàn)Visual Studio沒有連接unity編輯器,本文主要介紹了Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-11-11
C#遍歷文件夾及其子目錄的完整實(shí)現(xiàn)方法
這篇文章主要介紹了C#遍歷文件夾及其子目錄的方法,涉及C#文件與目錄的基本操作技巧,簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
Win10 系統(tǒng)下VisualStudio2019 配置點(diǎn)云庫(kù) PCL1.11.0的圖文教程
這篇文章主要介紹了Win10 系統(tǒng)下VisualStudio2019 配置點(diǎn)云庫(kù) PCL1.11.0的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
C#集合數(shù)據(jù)去重的5種方式及對(duì)比分析
今天我們一起來討論一下關(guān)于C#集合數(shù)據(jù)去重的5種方式并且使用BenchmarkDotNet對(duì)這5種方式進(jìn)行性能基準(zhǔn)對(duì)比測(cè)試分析,每種方法都有其特點(diǎn)和適用場(chǎng)景,我們可以根據(jù)具體需求和執(zhí)行效率選擇一種進(jìn)行使用,需要的朋友可以參考下2024-11-11

