PrefabLightmapping icon indicating copy to clipboard operation
PrefabLightmapping copied to clipboard

Not working correctly on Prefab Variants

Open Plaximo opened this issue 4 years ago • 2 comments

First of all very cool script. I have a problem using this with prefab variants, if I bake the light on a prefab variant he is copying all meshes to the parent prefab.

I made a quickfix but I'm not sure about it. I commented out everything and only left the stuff in the else branch, it seems to work.

if (targetPrefab != null) {
                GameObject root = PrefabUtility.GetOutermostPrefabInstanceRoot(instance.gameObject); // 根结点
                //如果当前预制体是是某个嵌套预制体的一部分(IsPartOfPrefabInstance)
                if (root != null) {
                    GameObject rootPrefab = PrefabUtility.GetCorrespondingObjectFromSource(instance.gameObject);
                    string rootPath = AssetDatabase.GetAssetPath(rootPrefab);
                    //打开根部预制体
                    PrefabUtility.UnpackPrefabInstanceAndReturnNewOutermostRoots(root, PrefabUnpackMode.OutermostRoot);
                    try {
                        //Apply各个子预制体的改变
                        PrefabUtility.ApplyPrefabInstance(instance.gameObject, InteractionMode.AutomatedAction);
                    } catch { } finally {
                        //重新更新根预制体
                        PrefabUtility.SaveAsPrefabAssetAndConnect(root, rootPath, InteractionMode.AutomatedAction);
                    }
                } else {
                    PrefabUtility.ApplyPrefabInstance(instance.gameObject, InteractionMode.AutomatedAction);
                }
            }

Plaximo avatar Oct 02 '20 16:10 Plaximo

Oh interesting, I haven't tested with prefab variants, thank you for the snippet. I will check it to make sure when I have some time

Ayfel avatar Oct 05 '20 15:10 Ayfel

Can you tell more about you fix ? I'm not able to make the script work...

Entretoize avatar Apr 06 '22 11:04 Entretoize