您好,登錄后才能下訂單哦!
這篇文章主要介紹“Unity刪除missing腳本組件的方法”,在日常操作中,相信很多人在Unity刪除missing腳本組件的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Unity刪除missing腳本組件的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
通過Resources.FindObjectsOfTypeAll查找所有GameObject,然后通過.hideFlags == HideFlags.None判斷是否為存在于Hierarchy面板。(此為編輯器腳本)
/******************************************************************************* * 版本聲明:v1.0.0 * 類 名 稱:DeleteMissingScripts * 創建日期: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); } } }
建議采取方法二
已知兩種方法可能會出現某些錯誤,
方法二運行幾次會自動修復(方法一未測試)
到此,關于“Unity刪除missing腳本組件的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。