http://www.xuanyusong.com/archives/3727

感谢楼下的牛逼回复更正一下,我表示我也是才知道。。

其实不需要实例化也能查找,你依然直接用GetComponentsInChildren<>(true),对传true即可。。。这样搞还很麻烦。。。唯一关注是能否把Missing的脚本序列化找出来??

使用 GetComponentsInChildren<>(true) 可以直接把Project视图里的子对象找出来!!!!

return;

代码是这样的

 
 
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
43
44
45
46
47
48
49
50
51
52
53
[MenuItem("Assets/Delete")]
static void delete ()
{
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameObject.prefab");
 
//删除MeshCollider
MeshCollider [] meshColliders = prefab.GetComponentsInChildren<MeshCollider>(true);
foreach(MeshCollider meshCollider in meshColliders){
 
GameObject.DestroyImmediate(meshCollider,true);
}
 
//删除空的Animation组件
Animation [] animations = prefab.GetComponentsInChildren<Animation>(true);
foreach(Animation animation in animations){
if( animation.clip == null){
GameObject.DestroyImmediate(animation,true);
}
 
}
 
//删除missing的脚本组件
MonoBehaviour [] monoBehaviours = prefab.GetComponentsInChildren<MonoBehaviour>(true);
foreach(MonoBehaviour monoBehaviour in monoBehaviours){
 
 
if(monoBehaviour == null){
Debug.Log("有个missing的脚本");
//GameObject.DestroyImmediate(monoBehaviour,true);
 
}
}
 
//遍历Transform的名子, 并且给某个游戏对象添加一个脚本
Transform [] transforms = prefab.GetComponentsInChildren<Transform>(true);
foreach(Transform transfomr in transforms){
if(transfomr.name == "GameObject (1)"){
Debug.Log(transfomr.parent.name);
transfomr.gameObject.AddComponent<BoxCollider>();
return;
}
 
}
//遍历Transform的名子, 删除某个GameObject节点
foreach(Transform transfomr in transforms){
if(transfomr.name == "GameObject (2)"){
GameObject.DestroyImmediate(transfomr.gameObject,true);
return;
}
 
}
                EditorUtility.SetDirty(prefab);
}

今天有朋友说不能删除missing的脚本, 我试了一下确实不行。 随后查了一下, 可以用这个方法来删除,

http://answers.unity3d.com/questions/15225/how-do-i-remove-null-components-ie-missingmono-scr.html

 
 
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
[MenuItem("Edit/Cleanup Missing Scripts")]
static void CleanupMissingScripts ()
{
     for(int i = 0; i < Selection.gameObjects.Length; i++)
     {
         var gameObject = Selection.gameObjects[i];
        
         // We must use the GetComponents array to actually detect missing components
         var components = gameObject.GetComponents<Component>();
        
         // Create a serialized object so that we can edit the component list
         var serializedObject = new SerializedObject(gameObject);
         // Find the component list property
         var prop = serializedObject.FindProperty("m_Component");
        
         // Track how many components we've removed
         int r = 0;
        
         // Iterate over all components
         for(int j = 0; j < components.Length; j++)
         {
             // Check if the ref is null
             if(components[j] == null)
             {
                 // If so, remove from the serialized component array
                 prop.DeleteArrayElementAtIndex(j-r);
                 // Increment removed count
                 r++;
             }
         }
        
         // Apply our changes to the game object
         serializedObject.ApplyModifiedProperties();
         //这一行一定要加!!!
         EditorUtility.SetDirty(gameObject);
     }
}

昨天晚上睡觉的时候脑洞打开。因为做项目的时候我们可能要在编辑器上做很多检查工具一类的东西。 这里我说几个典型的例子,比如空的Animation组件、丢失的脚本、没用的meshCollider组件。这些东西我们是不需要的,但是美术可能不会不小心加到prefab里。

以前的做法是 先要把Prefab 实例化 Instance以后  然后  GetComponentsInChildren  把所有的组件都取出来。 在进行遍历删除。 然后还要DestroyImmediate 它。 。那么如果prefab数量比较多的话,那么检查一次时间是很漫长的。

如果你只是想找组件 空脚本 一类的。用如下代码就可以不实例化并且找出来。

如果你想不实例化并且修改数据的话,那么可以考虑用下面的方法。

1.先把prefab 序列化的方式改成text 用File就可以把prefab的文本信息读出来。

 
 
1
File.ReadAllText("xxx/xxx.prefab")

2.prefab文本序列化的结构,如下图所示,看到!u!111了吗  111 是一组id .它是有意义的(它表示Animation),标着着这个组件是个啥东西。 具体是什么含义大家可以去这里查 http://docs.unity3d.com/Manual/ClassIDReference.html

3.自定义脚本

如果我想查一下看看prefab有没有绑定我自己写的脚本怎么办呢?如下图所 ,guid这一栏 就写的是你的脚本的guid了。

然后在脚本对应的mate文件里就记录这这个脚本的guid ,如果这两个id匹配,那么就说明这个prefab里挂着这个脚本了。

最后就交给正则表达式做第一步的匹配吧。 这样的话第一步就可以筛选掉一大批prefab了。 如果还需要进行验证在进一步的Instance来检查吧。。

Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)的更多相关文章

  1. Unity3D编辑器之不实例化Prefab获取删除更新组件

    原文链接:http://www.xuanyusong.com/archives/3727

  2. Unity3D研究院编辑器之脚本获取资源内存和硬盘大小

    内存 使用Profiler可以查看某个资源的内存占用情况,但是必须启动游戏,并且待查看的资源已经载入游戏中.我希望的是不启动游戏,也能看到它的内存好做统计. 硬盘 由于unity中的资源压缩格式记录在 ...

  3. Unity3D研究院编辑器之Editor的GUI的事件拦截

    OnGUI是Unity上一个时代的UI系统,而现在运行时的UI系统已经被UGUI取代,但是Editor的UI还是在用老的这一套GUI系统.比如unity编辑器里的所有窗口,布局,按钮,拖动条.滚动等等 ...

  4. [Unity] Unity3D研究院编辑器之自定义默认资源的Inspector面板

    比如编辑模式下对场景或者特定文件夹有一些操作可以在这个面板里来完成.. 代码如下. using UnityEngine; using System.Collections; using UnityEd ...

  5. Unity3D研究院编辑器之脚本设置ToolBar

    Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...

  6. Unity3D研究院编辑器之脚本设置ToolBar及脚本设置顶视图

    Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...

  7. [Unity] Unity3D研究院编辑器之独立Inspector属性

    本文转自: http://www.xuanyusong.com/archives/3680雨松MOMO Unity提供了强大的Editor功能, 我们可以很轻易的在EditorGUI中绘制任意的属性. ...

  8. Unity3D研究院编辑器之不影响原有布局拓展Inspector

    今天无意间发现了一篇好文章,也让我解决了一个很久都没解决的难题.问题是这样的,假如我想去拓展Unity自带的inspector但是并不想影响原有布局. 比如下面这段代码:     1 2 3 4 5 ...

  9. Unity3D研究院编辑器之重写Hierarchy的右键菜单

    Hierarchy视图中选择一个游戏对象以后通过右键可以打开一个unity默认菜单,一般情况下都可以满足我们,但是我想真对某些特殊的游戏对象而展开特殊的菜单.如下图所示,比如这样: 代码: using ...

随机推荐

  1. jQueryEasyUI Messager基本使用

    二.jQueryEasyUI Messager基本使用 1.$.messager.alert(title, msg, icon, fn)1>.基本用法 代码: 1 2 3 4 5 6 7 8 9 ...

  2. 2016年11月9日 星期三 --出埃及记 Exodus 19:25

    2016年11月9日 星期三 --出埃及记 Exodus 19:25 So Moses went down to the people and told them.于是摩西下到百姓那里告诉他们.

  3. C#中另类自定义公式计算 字符串转换为计算公式,并得出计算结果

    [csharp] view plain copy print? //方法一 利用DataTable中的Compute方法 例如:1*2-(4/1)+2*4=6 , , , ); DataTable d ...

  4. RabbitMQ术语

    工作队列:Working Queue 分配:多个客户端接收同一个Queue,如何做负载均衡(分配).     Round-robin分配:多个接收端接收同一个Queue时,采用了Round-robin ...

  5. 复旦大学2013--2014学年第一学期(13级)高等代数I期末考试第七大题解答

    七.(本题10分)设 \(A\) 为数域 \(K\) 上的 \(n\) 阶非异阵, 证明: 对任意的对角阵 \(B\in M_n(K)\),  \(A^{-1}BA\) 均为对角阵的充分必要条件是 \ ...

  6. Python3基础 print 中字符串乘以数字,重复输出多次

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  7. 关于stl string find 容易犯的一个错误

    有时候经常会判断一个字符串a中是否有子字符串b,那么有人会调用 string::find这个函数  这个函数返回子字符串首次出现的位置,那么有人会这样写 string str1 = "&qu ...

  8. jquery之 animate()方法详解

    jQuery.animate() 函数详解 animate()函数用于执行一个基于css属性的自定义动画. 你可以为匹配的元素设置css样式,animate()函数将会执行一个从当前样式到指定的css ...

  9. [Effective Java]第四章 类和接口

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. CUBRID学习笔记 38 net调用java的函数过程

    首先要设置java_stored_procedure 为yes  该配置项在cubrid.conf中 书写并编译java代码 public class SpCubrid{      public st ...