您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。