Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五)
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获取删除更新组件(十五)的更多相关文章
- Unity3D编辑器之不实例化Prefab获取删除更新组件
原文链接:http://www.xuanyusong.com/archives/3727
- Unity3D研究院编辑器之脚本获取资源内存和硬盘大小
内存 使用Profiler可以查看某个资源的内存占用情况,但是必须启动游戏,并且待查看的资源已经载入游戏中.我希望的是不启动游戏,也能看到它的内存好做统计. 硬盘 由于unity中的资源压缩格式记录在 ...
- Unity3D研究院编辑器之Editor的GUI的事件拦截
OnGUI是Unity上一个时代的UI系统,而现在运行时的UI系统已经被UGUI取代,但是Editor的UI还是在用老的这一套GUI系统.比如unity编辑器里的所有窗口,布局,按钮,拖动条.滚动等等 ...
- [Unity] Unity3D研究院编辑器之自定义默认资源的Inspector面板
比如编辑模式下对场景或者特定文件夹有一些操作可以在这个面板里来完成.. 代码如下. using UnityEngine; using System.Collections; using UnityEd ...
- Unity3D研究院编辑器之脚本设置ToolBar
Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...
- Unity3D研究院编辑器之脚本设置ToolBar及脚本设置顶视图
Unity版本5.3.2 如下图所示,ToolBar就是Unity顶部的那一横条.这里的所有按钮一般情况下都得我们手动的用鼠标去点击.这篇文章我们说说如果自动操作它们 1.自动点击左边四个按钮 (拖动 ...
- [Unity] Unity3D研究院编辑器之独立Inspector属性
本文转自: http://www.xuanyusong.com/archives/3680雨松MOMO Unity提供了强大的Editor功能, 我们可以很轻易的在EditorGUI中绘制任意的属性. ...
- Unity3D研究院编辑器之不影响原有布局拓展Inspector
今天无意间发现了一篇好文章,也让我解决了一个很久都没解决的难题.问题是这样的,假如我想去拓展Unity自带的inspector但是并不想影响原有布局. 比如下面这段代码: 1 2 3 4 5 ...
- Unity3D研究院编辑器之重写Hierarchy的右键菜单
Hierarchy视图中选择一个游戏对象以后通过右键可以打开一个unity默认菜单,一般情况下都可以满足我们,但是我想真对某些特殊的游戏对象而展开特殊的菜单.如下图所示,比如这样: 代码: using ...
随机推荐
- LTMP手动编译安装以及全自动化部署实践(附详细代码)
大家使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL /PHP),LNMPA(N ...
- [Effective Java]第九章 异常
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- So easy Webservice 8.spring整合CXF 发布WS
1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件 2.web.xml中配置CXFServlet,过滤WS服务的地址 <!-- 配置CXFServlet ...
- Redis 在新浪微博中的应用
Redis 在新浪微博中的应用 Redis简介 1. 支持5种数据结构 支持strings, hashes, lists, sets, sorted setsstring是很好的存储方式,用来做计数存 ...
- 未能加载文件或程序集xxx或它的某一个依赖项 试图加载格式不正确的程序
解决方案:IIS——应用程序池——高级设置——启用32位应用程序 :true.
- 模仿$.Callbacks实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- Nginx简介及配置实用
Nginx简介 Nginx是一个高性能的HTTP和反向代理服务器: 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...
- thinkphp 初始配置
他喵的,去做了个其他的模板,一段时间不碰tp,居然配置了好久 记录留备用 一.把下好的ThinkPHP放到根目录的文件夹下 ,例如www文件夹下 在www目录下新建文件夹admin和home 新建入口 ...
- 访问远程mysql数据库
使用mysql命令窗口模式/工具,比如需要给'10.2.9.239' 的用户分配mantis123,mantis123访问,则使用如下格式: GRANT ALL PRIVILEGES ON *.* T ...