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();
}
}