Unity-批量修改Prefab上的属性

问题描述:今天发现工程中有些prefab上的脚本丢失了一些引用,本以为手动拖拽上去搞定,后来查看其它prefab,也有类似的问题,于是写了一个小工具,批量修改下。

 

上代码:

 1 [ExecuteInEditMode]
 2     [MenuItem("Tools/Add-HitData")]
 3     private static void AddHitData()
 4     {
 5         if (EditorUtility.DisplayDialog("功能确认",
 6             "此功能会遍历8_AssetBundle/Prefab_Hero/下的全部prefab,并将prefab上的ANIMATION_DATA/HIT赋值到LeaderAction脚本中的HIT_DATA上,确定要执行吗?",
 7             "确定"))
 8         {
 9             string genPath = Application.dataPath + "/8_AssetBundle/Prefab_Hero/";
10             string[] filesPath = Directory.GetFiles(genPath, "*.prefab", SearchOption.AllDirectories);
11 
12             for (int i = 0; i < filesPath.Length; i++)
13             {
14                 filesPath[i] = filesPath[i].Substring(filesPath[i].IndexOf("Assets"));
15 
16                 GameObject _prefab = AssetDatabase.LoadAssetAtPath(filesPath[i], typeof(GameObject)) as GameObject;
17                 GameObject prefabGameobject = PrefabUtility.InstantiatePrefab(_prefab) as GameObject;
18                 LeaderAction la = prefabGameobject.GetComponent<LeaderAction>();
19 
20 
21                 if (la != null)
22                 {
23                     if (la.HIT_DATA != null) continue;
24                     if (prefabGameobject.transform.FindChild("ANIMATION_DATA/HIT") == null) continue;
25                     la.HIT_DATA = prefabGameobject.transform.FindChild("ANIMATION_DATA/HIT").GetComponent<AnimationData>();
26                     PrefabUtility.ReplacePrefab(prefabGameobject, _prefab, ReplacePrefabOptions.Default);
27                     MonoBehaviour.DestroyImmediate(prefabGameobject);
28                 }
29                 else
30                     continue;
31 
32             }
33             AssetDatabase.SaveAssets();
34             EditorUtility.DisplayDialog("成功", "HIT_DATA 添加完成!", "确定");
35         }
36     }

 

 

由于之前老是有同事乱点击Tools中的工具导致一些不可预料的浪费时间,于是加上了二次提示框,仅供参考。

 

posted @ 2017-04-11 14:48  NIKEY_M  阅读(4498)  评论(0编辑  收藏  举报