今天,突然,突然电脑死机,再次打开项目发现警告,有一些脚本丢失了,MissingScirpt!但是我忘记了,所以写个脚本自己删除吧。
先贴个代码文章源自大腿Plus-https://www.zhaoshijun.com/archives/453
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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.zhaoshijun.com/archives/453
- 我以为判断了组件是空,然后直接destroy掉就行,结果发现不起作用。后来查了一下,需要先序列化物体,然后得到它的序列化属性,将对应的属性根据索引删除掉。之后重新应用属性即可。最后刷新一下。
- 我想直接把场景里所有的物体都执行,所以对比了一下GameObject.FindObjectsOfType<GameObject>()和Resources.FindObjectsOfTypeAll<GameObject>(),发现后者会多出很多东西,包括但不限于预制体,内置的CUBE之类的。大家可以自己查一下。
好了,就这样。下班!文章源自大腿Plus-https://www.zhaoshijun.com/archives/453
此时国足1:0韩国 !文章源自大腿Plus-https://www.zhaoshijun.com/archives/453 文章源自大腿Plus-https://www.zhaoshijun.com/archives/453
我的微信
微信扫一扫

shijun_z
我的QQ
QQ扫一扫

846207670
评论