using UnityEngine;
using System.Collections;
using UnityEditor;
public class ApplyPrefabEditor : Editor {
[MenuItem("Ari/Batch Apply Prefab")]
public static void BatchApplyPrefab()
{
GameObject[] objs = Selection.gameObjects;
if (null == objs || objs.Length < 1)
{
Debug.LogError("没有选中prefab");
return;
}
for (int i = 0; i < objs.Length; i++)
{
ApplyPrefab(objs[i]);
}
}
public static void ApplyPrefab(GameObject obj)
{
if (null == obj)
{
Debug.LogError("选中的obj 是 null");
return;
}
PrefabType type = EditorUtility.GetPrefabType(obj);
if (type != PrefabType.PrefabInstance)
{
Debug.LogError("选中的obj " + obj.name + " 不是 PrefabInstance ");
return;
}
//这里必须获取到prefab实例的根节点,否则ReplacePrefab保存不了
GameObject prefabObj = GetPrefabInstanceParent(obj);
UnityEngine.Object prefabAsset = null;
if (prefabObj != null)
{
prefabAsset = PrefabUtility.GetPrefabParent(prefabObj);
if (prefabAsset != null)
{
PrefabUtility.ReplacePrefab(prefabObj, prefabAsset, ReplacePrefabOptions.ConnectToPrefab);
Debug.Log("PrefabInstance :" + prefabObj.name + " Apply 成功");
}
}
AssetDatabase.SaveAssets();
}
//遍历获取prefab节点所在的根prefab节点
static GameObject GetPrefabInstanceParent(GameObject obj)
{
if (obj == null)
{
return null;
}
PrefabType pType = EditorUtility.GetPrefabType(obj);
if (pType != PrefabType.PrefabInstance)
{
return null;
}
if (obj.transform.parent == null)
{
return obj;
}
pType = EditorUtility.GetPrefabType(obj.transform.parent.gameObject);
if (pType != PrefabType.PrefabInstance)
{
return obj;
}
return GetPrefabInstanceParent(obj.transform.parent.gameObject);
}
}
Unity 批量ApplyPrefab
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 今天遇到一个需求,需要批量修改粒子系统的startDelay属性值,Unity编辑器本来支持多选然后赋值,但是现在...
- Unity导出的安卓工程利用ant进行多渠道循环批量打包 一:设置JAVA环境变量 做android开发的配置这个...
- 使用方法 选中物体 按住Ctrl选中需要修改为的材质球 点击Tools/changeMaterial按钮