美文网首页
[Unity Editor] MissingScript导致Go

[Unity Editor] MissingScript导致Go

作者: _Walker__ | 来源:发表于2019-03-30 13:59 被阅读0次

    代码参考: Unity3D编辑器:删掉MissingScirpt脚本
    问题环境:

    • Unity 5.4.6f3 (64-bit)


      问题现象

      昨天改了点东西,发现某个Prefab拖到Hierarchy后状态不对(见上图)。和朋友聊过后发现Prefab上使用着一个已经删掉的脚本。

    [MenuItem("GameObject/DMTools/移除MissingScripts")]
    static void RemoveMissingScripts()
    {
        if (null != Selection.activeTransform)
        {
            _RemoveMissScripts(Selection.activeTransform);
        }
    
        PrefabUtility.ReconnectToLastPrefab(Selection.activeGameObject);
    }
    
    private static void _RemoveMissScripts(Transform trans)
    {
        Component[] cmps = trans.GetComponents<Component>();
        SerializedObject serializedObject = new SerializedObject(trans.gameObject);
        SerializedProperty prop = serializedObject.FindProperty("m_Component");
        for (int i = cmps.Length-1; i >=0 ; --i)
        {
            if (null == cmps[i])
            {
                prop.DeleteArrayElementAtIndex(i);
            }
        }
        serializedObject.ApplyModifiedProperties();
    
        foreach (Transform child in trans)
        {
            _RemoveMissScripts(child);
        }
    }
    

    相关文章

      网友评论

          本文标题:[Unity Editor] MissingScript导致Go

          本文链接:https://www.haomeiwen.com/subject/ctzjiftx.html