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. Mysql--学习笔记(==》简单查询三)

    -- 查看查询数据显示SELECT * FROM student; -- 显示一部分信息的查询SELECT sname 姓名,sscore 成绩,saddress 家庭住址 FROM student; ...

  2. Redis基础

    Redis是开源的,支持网络.可基于内存也可持久化的key-value数据库,支持主从复制,读写分离,支持5种基本数据类型,高并发性能 安装redis数据库 下载:http://redis.io/do ...

  3. VS2015使用技巧 打开代码片段C#部分

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

  4. Linq实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  5. 经典JSP数据库连接(ORACLE、SQL Server、MySQL)

    1.连接ORACLE8/8I/9I数据库(thin模式) <%@ page language="java" import="java.util.*" pa ...

  6. SY全局系统字段

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

  7. Redis数据导入工具优化过程总结

    Redis数据导入工具优化过程总结 背景 使用C++开发了一个Redis数据导入工具 从oracle中将所有表数据导入到redis中: 不是单纯的数据导入,每条oracle中的原有记录,需要经过业务逻 ...

  8. run a Freight robot (1)

    1. Freight robot The Fetch and Freight Research Edition Robots are indoor laboratory robots. Coordin ...

  9. Android中四种OnClick事件的写法

    package com.example.dailphone; import android.support.v7.app.ActionBarActivity; import android.suppo ...

  10. 如何在本机上将localhost改为www.dev.com

    windows上安装好服务器后,打开本地目录 C:\Windows\System32\drivers\etc\ ,会看到有个hosts文件,打开后里面的代码为: # Copyright (c) - M ...