Unity3D编辑器:删掉MissingScirpt脚本

今天,突然,突然电脑死机,再次打开项目发现警告,有一些脚本丢失了,MissingScirpt!但是我忘记了,所以写个脚本自己删除吧。文章源自大腿Plus-https://www.shijunzh.com/archives/453

先贴个代码文章源自大腿Plus-https://www.shijunzh.com/archives/453

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.SceneManagement;

public class RemoveMissingScripts : Editor
{
    [MenuItem("Tools/移除丢失的脚本")]
    public static void RemoveMissingScript()
    {
        var gos = GameObject.FindObjectsOfType<GameObject>();
        foreach (var item in gos)
        {
            Debug.Log(item.name);
            SerializedObject so = new SerializedObject(item);
            var soProperties = so.FindProperty("m_Component");
            var components = item.GetComponents<Component>();
            int propertyIndex = 0;
            foreach (var c in components)
            {
                if (c == null)
                {
                    soProperties.DeleteArrayElementAtIndex(propertyIndex);
                }
                ++propertyIndex;
            }
            so.ApplyModifiedProperties();
        }
        
        AssetDatabase.Refresh();
        Debug.Log("清理完成!");
        //Debug.Log(gos.Length);
        //var r= Resources.FindObjectsOfTypeAll<GameObject>();
        //foreach (var item in r)
        //{
        //    Debug.Log(item.name);
        //}
        //Debug.Log(r.Length);

    }

}

没什么特别的地方,只是说一下碰到的坑。文章源自大腿Plus-https://www.shijunzh.com/archives/453

  • 我以为判断了组件是空,然后直接destroy掉就行,结果发现不起作用。后来查了一下,需要先序列化物体,然后得到它的序列化属性,将对应的属性根据索引删除掉。之后重新应用属性即可。最后刷新一下。
  • 我想直接把场景里所有的物体都执行,所以对比了一下GameObject.FindObjectsOfType<GameObject>()和Resources.FindObjectsOfTypeAll<GameObject>(),发现后者会多出很多东西,包括但不限于预制体,内置的CUBE之类的。大家可以自己查一下。

好了,就这样。下班!文章源自大腿Plus-https://www.shijunzh.com/archives/453

此时国足1:0韩国 !文章源自大腿Plus-https://www.shijunzh.com/archives/453

大腿Plus
  • 本文由 发表于 2017年3月23日20:55:05
  • 转载请务必保留本文链接:https://www.shijunzh.com/archives/453

发表评论