[Unity]批量编辑Prefab

2019/10 19 20:10

作用:批量修改Prefab中的内容,比如Prefab中添加了许多EditorOnly的物件,现在用工具批量删除

方法一:断开所有连接,重新组装Prefab,不推荐
var prefabObj = AssetDatabase.LoadAssetAtPath<GameObject>(prefabFilename);
var newInstance = GameObject.Instantiate(prefabObj);
    // Edit: UnityEngine.Object.DestroyImmediate();
    // Edit: GameObject.DestroyImmediate(obj);
PrefabUtility.SaveAsPrefabAssetAndConnect(newInstance, newPath, InteractionMode.AutomatedAction);
GameObject.DestroyImmediate(newInstance);

方法二:保持Prefab内的嵌套
var prefabObj = PrefabUtility.LoadPrefabContents(prefabFilename);
    // Edit: UnityEngine.Object.DestroyImmediate();
    // Edit: GameObject.DestroyImmediate(obj);
PrefabUtility.SaveAsPrefabAsset(prefabObj, newPath);
PrefabUtility.UnloadPrefabContents(prefabObj); // 注意调用此API,否则UnityCrash!

--转载请注明: http://blog.coolcoding.cn/?p=446

发表回复

欢迎回来 (打开)

(必填)