惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
S
Securelist
T
Tor Project blog
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 热门话题
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tenable Blog
博客园_首页
TaoSecurity Blog
TaoSecurity Blog
Attack and Defense Labs
Attack and Defense Labs
Project Zero
Project Zero
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
F
Full Disclosure
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
GbyAI
GbyAI
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
博客园 - 叶小钗
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - 鱼丸粗面

《Unity預計算即時GI》笔记:三、Clusters和总结 《Unity預計算即時GI》笔记:二、光照图 - 鱼丸粗面 - 博客园 《Unity預計算即時GI》笔记:一、基本概念与一些设置 - 鱼丸粗面 - 博客园 我所了解的法线贴图 - 鱼丸粗面 - 博客园 游戏设计模式读书笔记:命令模式 - 鱼丸粗面 - 博客园 游戏设计模式读书笔记:架构、性能、游戏 一个美术需求引发的Custom Inspector 代码生成AnimatorController unity生成的WP8.1工程的Title本地化实现 unity生成WP工程后ExtendedSplashImage显示不正确的问题 Unity5.x在WP8.1中无法使用Reflection API的解决方法 使用uGUI制作游戏内2D动画 3D touch在Unity3D中的使用 Unity中的协程是什么? Unity3D脚本调用Objective C代码实现游戏内购买 WindowsPhone8拍照功能实现简介 WindowsPhone App如何扩展能够使用的内存 prism关键概念: objective-c:继承
Unity3D Editor模式下批量修改prefab
鱼丸粗面 · 2015-11-03 · via 博客园 - 鱼丸粗面

最经遇到一个需要批量修改已经做好的prefab的问题,查了一些资料最终实现了但是还是不够完美,通过学习也发现unity的编辑器功能还是非常强大的。废话不多说直接上代码:

 1 [ExecuteInEditMode]
 2     [MenuItem("Tools/RecordPoint Add Flame")]
 3     private static void RecordPointAddFlame()
 4     {
 5         GameObject twoSphere = AssetDatabase.LoadAssetAtPath("Assets/Resources/Prefabs/TwoSphere.prefab", typeof(GameObject)) as GameObject;
 6 
 7         string[] ids = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Resources/Prefabs" });
 8         for (int i = 0; i < ids.Length; i++)
 9         {
10             string path = AssetDatabase.GUIDToAssetPath(ids[i]);
11             Debug.Log(path);
12             if (!path.Contains("TwoCube"))
13             {
14                 continue;
15             }
16             GameObject originTwoCube = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
17             GameObject twoCube = PrefabUtility.InstantiatePrefab(originTwoCube) as GameObject;
18 
19             foreach (Transform item in twoCube.transform)
20             {
21                 if (item.FindChild("TwoSphere") == null)
22                 {
23                     GameObject ts = PrefabUtility.InstantiatePrefab(twoSphere) as GameObject;
24                     ts.transform.parent = item;
25                 }
26             }
27 
28             var newprefab = PrefabUtility.CreateEmptyPrefab("Assets/Resources/Prefabs/TwoCube.prefab");
29             PrefabUtility.ReplacePrefab(twoCube, newprefab, ReplacePrefabOptions.Default);
30         }
31 
32         AssetDatabase.SaveAssets();
33         Debug.Log("Done");
34     }

 这段代码的功能是在TwoCube这个prefab的两个子对象cube上挂一个名为TwoSphere的prefab。如图

最终结果如下:

代码中为什么要使用PrefabUtility.InstantiatePrefab和PrefabUtility.ReplacePrefab,这是因为上述例子有一点比较特殊的地方,就是是一个prefab中嵌入另一个prefab。如果单纯的只是操作一个prefab是没有必要这样做的。