项目进行到中后期,资源脚本不断变动,有些脚本不需要了已经移除掉,可能忘记更新脚本所在的prefab,或者更新不完全有遗漏等。。。工程运行起来时可能会出现这种情况:一大串警告“The referenced script on this Behaviour is missing!”。
这种情况需要把prefab上丢失不用的脚本移除掉,如果有很多个prefab丢失脚本,手动一个个移除工作量是非常大的。
能写个工具批量处理将节省大量工作量,经过研究发现需要把丢失脚本的prefab序列化移除掉不用的记录,prefab可以用记事本打开,发现里面有个“m_Component”,就是这个属性记录挂载的脚本,所以读取该属性移除掉丢失脚本的记录,替换掉相应的prefab即可。。
[MenuItem("ZYKJ/RemoveMissComponent")]
static public void RemoveMissComponent()
{
UnityEngine.Object[] obj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
GameObject targetObj = null;
GameObject prefabObj = null;
Debug.LogError(obj.Length);
for (int i = 0, j = obj.Length; i < j; i++)
{
EditorUtility.DisplayProgressBar("移除进度", string.Format("{0}/{1}", i + 1, obj.Length), (i + 1f) / obj.Length);
if (null == obj[i]) continue;
PrefabType pType = PrefabUtility.GetPrefabType(obj[i]);
if (pType != PrefabType.Prefab) continue;
//targetObj = (GameObject)obj[i];
//if (null != targetObj)
//prefabObj = GameObject.Instantiate(targetObj) as GameObject;
prefabObj = (GameObject)AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(obj[i]), typeof(UnityEngine.Object));
if (null != prefabObj)
{
CheckMissComponent(prefabObj, targetObj);
}
//if (null != prefabObj)
// UnityEngine.Object.DestroyImmediate(prefabObj);
}
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog("替换结果", "完成替换", "确定");
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
}
static void CheckMissComponent(GameObject prefabObj, GameObject targetObj)
{
if (null == prefabObj /*|| null == targetObj*/) return;
Transform root = prefabObj.GetComponent();
Transform[] trans = prefabObj.GetComponentsInChildren(true);
ListtList = new List(trans);
tList.Add(root);
for (int i = 0, j = tList.Count; i < j; i++)
{
SerializedObject seri = new SerializedObject(tList[i].gameObject);
SerializedProperty serPro = seri.FindProperty("m_Component");
Component[] comps = tList[i].gameObject.GetComponents();
for(int a = 0,b = comps.Length;a < b;a++)
{
if(comps[a] == null)
serPro.DeleteArrayElementAtIndex(a);
}
seri.ApplyModifiedProperties();
}
//PrefabUtility.ReplacePrefab(prefabObj, targetObj);
}
不同unity版本处理方法不一样:
using UnityEngine;
using System.Collections;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
public class SmallTools
{
[MenuItem("Tools/SmallToos/RemoveMissingComponent")]
static void RemoveMissingComponent()
{
string[] allAssets = AssetDatabase.GetAllAssetPaths();
float face = 1f / allAssets.Length;
float process = face;
for (int i = 0, j = allAssets.Length; i < j; i++)
{
EditorUtility.DisplayProgressBar("RemoveMissingComponent", string.Format("正在查找{0}%", process * 100), process);
process += face;
if (!allAssets[i].Contains("____noPublic"))
continue;
if (".prefab" != Path.GetExtension(allAssets[i]))
continue;
RemoveMissingComponent(allAssets[i]);
}
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
}
static void RemoveMissingComponent(string path)
{
GameObject go1 = AssetDatabase.LoadAssetAtPath(path,typeof(GameObject)) as GameObject;
if (null == go1)
return;
Transform[] trans = go1.GetComponentsInChildren<Transform>(true);
bool isModified = false;
for (int i = 0, j = trans.Length; i < j; i++)
{
Component[] comps = trans[i].gameObject.GetComponents<Component>();
bool isRemove = RemoveMissingComponent(comps, trans[i].gameObject);
if (isRemove)
isModified = isRemove;
}
GameObject go2 = GameObject.Instantiate(go1) as GameObject;
if (isModified)
{
PrefabUtility.CreatePrefab(path, go2,ReplacePrefabOptions.ConnectToPrefab);
}
GameObject.DestroyImmediate(go2);
}
static bool RemoveMissingComponent(Component[] comps,GameObject go)
{
SerializedObject sObj = new SerializedObject(go);
SerializedProperty sPro = sObj.FindProperty("m_Component");
if (null == sPro) return false;
bool isModified = false;
for (int i = 0, j = comps.Length; i < j; i++)
{
if (null == comps[i])
{
isModified = true;
Debug.LogError("================>>1" + go.name + " // " + sPro.arraySize);
sPro.DeleteArrayElementAtIndex(i);
Debug.LogError("================>>2" + go.name + " // " + sPro.arraySize);
}
}
if (isModified)
{
bool isApply = sObj.ApplyModifiedProperties();
//sObj.Update();
Debug.LogError("==============>>isApply " + isApply);
}
return isModified;
}
}
网友评论