您好,登錄后才能下訂單哦!
這篇文章主要介紹“Unity刪除missing腳本組件的方法”,在日常操作中,相信很多人在Unity刪除missing腳本組件的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Unity刪除missing腳本組件的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
通過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ù)(方法一未測試)
到此,關(guān)于“Unity刪除missing腳本組件的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。