Unity移除丢失的脚本

    GameObjectUtility.RemoveMonoBehavioursWithMissingScript(item.gameObject);
using UnityEditor;
using UnityEngine;

public class DeleteCom : MonoBehaviour
{
    //2018.4.22能行,但常闪退
    [MenuItem("Assets/DeleteAAA 2018.4.22")]
    public static void DeleteAAA()
    {
        if (Selection.activeGameObject == null)
        {
            return;
        }
        var gos = Selection.activeGameObject.GetComponentsInChildren<Transform>(true);
        foreach (var item in gos)
        {
            Debug.Log(item.name);
            SerializedObject so = new SerializedObject(item.gameObject);
            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("清理完成!");


    }

    //2018.4.28
    [MenuItem("Assets/DeleteBBB 2018.4.28")]
    public static void DeleteTTT()
    {
        if (Selection.activeGameObject == null)
        {
            return;
        }
        var gos = Selection.activeGameObject.GetComponentsInChildren<Transform>(true);
        foreach (var item in gos)
        {
            GameObjectUtility.RemoveMonoBehavioursWithMissingScript(item.gameObject);
        }
        AssetDatabase.Refresh();
        Debug.Log("清理完成!");
    }
}

小工具.GetMonoBehavioursWithMissingScriptCount,RemoveMonoBehavioursWithMissingScript

public class ScriptNullHelpTools : GetAssetHelpTools
{
    private Dictionary<GameObject, List<string>> mDicLookPrefabs = new Dictionary<GameObject, List<string>>();
    private Vector2 scrollPosition = Vector2.zero;
    private string mSearchPath = @"Assets\_Resource\Effect";
    //private string mSearchPath = @"Assets\_Resource\UI\Prefabs";

    void OnGUI()
    {
        mSearchPath = EditorGUILayout.TextField("搜索路径", mSearchPath);
        if (GUILayout.Button("查看所有prefab的脚本的引用", GUILayout.Height(30)))
        {
            mDicLookPrefabs = new Dictionary<GameObject, List<string>>();
            if (string.IsNullOrEmpty(mSearchPath) || mSearchPath.StartsWith("Assets") == false)
            {
                ShowMsg("路径不对,Assets/开头");
                return;
            }
            var tAllPrefabs = GetPrefabs(mSearchPath);
            var tPathCount = 0;
            for (int iii = 0; iii < tAllPrefabs.Count; iii++)//
            {
                var tGo = tAllPrefabs[iii];
                List<string> tListSelectGos = new List<string>();
                string tPath = "";
                var tTrans = tGo.GetComponentsInChildren<Transform>(true);
                for (int jjj = 0; jjj < tTrans.Length; jjj++)
                {
                    var tComs = tTrans[jjj].gameObject.GetComponents<Component>();
                    for (int bbb = 0; bbb < tComs.Length; bbb++)
                    {
                        if (tComs[bbb] == null)
                        {
                            tPath = FindPath(tGo.transform, tTrans[jjj].gameObject.transform);
                            tPathCount++;
                            if (tListSelectGos.Contains(tPath) == false)
                            {
                                tListSelectGos.Add(tPath);
                            }
                        }
                    }
                }
                if (tListSelectGos.Count > 0)
                    mDicLookPrefabs[tGo] = tListSelectGos;
            }
            ShowMsg("空脚本,共 " + mDicLookPrefabs.Keys.Count.ToString() + " 个prefab,共引用" +tPathCount.ToString() + "个脚本引用");

        }

        if (GUILayout.Button("移除弃用的组件", GUILayout.Height(20)))
        {
            foreach (var item in mDicLookPrefabs)
            {
                var tPrefab = item.Key;
                var tOldHierarchyPrefab = PrefabUtility.InstantiatePrefab(tPrefab) as GameObject;
                var tTrans = tOldHierarchyPrefab.GetComponentsInChildren<Transform>(true);
                for (int jjj = 0; jjj < tTrans.Length; jjj++)
                {
                    SerializedObject so = new SerializedObject(tTrans[jjj].gameObject);
                    var soProperties = so.FindProperty("m_Component");
                    var components = tTrans[jjj].gameObject.GetComponents<Component>();
                    int propertyIndex = 0;
                    foreach (var c in components)
                    {
                        if (c == null)
                        {
                            soProperties.DeleteArrayElementAtIndex(propertyIndex);
                        }
                        ++propertyIndex;
                    }
                    so.ApplyModifiedProperties();
                }
                var tOldUserData = GetUserData(tPrefab);
                var tPath = AssetDatabase.GetAssetPath(tPrefab);
                //var tNewEmpty = PrefabUtility.CreateEmptyPrefab(tPath);
                //var gameNew = PrefabUtility.ReplacePrefab(tOldHierarchyPrefab, tNewEmpty, ReplacePrefabOptions.ConnectToPrefab);//过时
                var gameNew = PrefabUtility.SaveAsPrefabAsset(tOldHierarchyPrefab, tPath);                GameObject.DestroyImmediate(tOldHierarchyPrefab);
                SaveUserData(gameNew, tOldUserData);
            }
        }

        #region 批查看
        EditorGUILayout.BeginVertical("Box", GUILayout.Width(700));
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            if (mDicLookPrefabs.Count > 0)
            {
                GUILayout.Label("点击生成Hierarchy视图中", GUILayout.Height(16));
                foreach (var item in mDicLookPrefabs)
                {
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        if (GUILayout.Button(item.Key.name + "-->" + item.Value[i], GUILayout.Height(16)))
                        {
                            GenerateHerarchy(item.Key, item.Value[i],false);
                        }
                    }
                }
            }
            EditorGUILayout.EndScrollView();
        }
        EditorGUILayout.EndVertical();
        #endregion
    }
    public List<GameObject> GetPrefabs(string pPath, string pStr = "")
    {
        if (string.IsNullOrEmpty(pPath))
            pPath = "Assets/_Resource/UI/Prefabs";
        var guids = AssetDatabase.FindAssets("t:Prefab " + pStr, new string[] { pPath });
        List<GameObject> tAllPrefab = new List<GameObject>();
        foreach (var item in guids)
        {
            var go = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(item)) as GameObject;
            tAllPrefab.Add(go);
        }
        return tAllPrefab;
    }
    public void ShowMsg(string content)
    {
        ShowNotification(new GUIContent(content));
    }
        public string GetUserData(Object obj)
    {
        string pPath = AssetDatabase.GetAssetPath(obj);
        AssetImporter import = AssetImporter.GetAtPath(pPath);
        return import.userData;
    }

    public void SaveUserData(Object pObj, string pData)
    {
        string path = AssetDatabase.GetAssetPath(pObj);
        AssetImporter import = AssetImporter.GetAtPath(path);
        import.userData = pData;
        import.SaveAndReimport();
    }
}
    

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值