[Unity Editor] MissingScript导致Go

2019-03-30  本文已影响0人  _Walker__

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

  昨天改了点东西,发现某个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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读