I’m trying out the new workflow and like it very much when starting with a clean slate. However, now I’m trying to migrate existing content and couldn’t figure out how to do this: I have several regular simple prefabs which I would like to turn into variants of a new base prefab. Is this possible and if, how? The manual only describes how to create a variant from an existing base, but not how to connect existing prefabs. Thank you!
This is not possible, unfortunately. The way the Prefab system works, it’s not possible to do this without changing IDs of objects in the Prefab, which would in turn mean that any references to it would break.
The only way would be rather manual: To open the base Prefab and the intended derived Prefab and copy over GameObjects and components from the intended derived Prefab onto the base Prefab, then save the result as a new Prefab (choosing to make it a Prefab Variant when asked). And then replace the existing Prefab with this new one. This will break references to the Prefab as described, since you’re essentially replacing the Prefab with a new one, but if that’s acceptable it might be a way to go, and could possibly be automated with a little scripting.
Any chance this became possible in the last year (2019.3)? Dragging a prefab to another prefab or variant should convert it. Imagine if C# didn’t let you change the base class after a class is created…
I keep coming back to this problem ever since. Even in new projects. It’s simply not possible to think ahead like this and plan all content top-down, from base to more derived or variant versions. Like in programming, I start with a simple prefab, but then after months of development I realize that I need two very similar prefabs. But the new prefab cannot be a variant of the existing one, instead, the old one should become a variant of the not-yet existing new prefab or both should be derived from a common base prefab.
Edit: Ok, so it does work, (edit: for a single prefab) but not many people seem to know about it:
To turn an existing prefab into a variant of a newly create base prefab do the following steps:
- Open existing prefab “Cat”.
- Rename the GameObject root in prefab mode to “Animal”.
- Drag the root object “Animal” into the project window.
- A dialog comes up, confirm that you want to create a new base and turn the open prefab into a variant of the newly created one.
- Rename root gameobject “Animal” back to “Cat” in the open prefab.
- Save
You now have a base prefab “Animal” and a variant of it called “Cat” in the project.
Yes, that does work. However, in your original post you said you wanted to make multiple Prefabs into variants of the same base Prefab (at least that’s how I interpreted it) and you can’t do that with the approach described in the video and which you outlined above, since every time you follow the steps you get a new base Prefab.
Ah that’s true! Also, it only works in a very simple example as shown in the video, but I’ve now tried it in our actual project and quickly realized that I need a different solution. When creating a base from an existing prefab, the base will have all child GameObjects and components and I couldn’t find a way to remove some of them in the base but keep them as added overrides in the child.
So afterall, I decided to use more nested prefabs instead of variants. So, basically, instead of trying to create a base-variant relationship with overrides, I now have two entirely separate prefab root objects, which then both have a series of nested prefab childs, which some of them they can share and some of them they override. Maybe one could say “composition over inheritance”.
Great if that works for you. It’s virtually the same thing as well - the only difference is the root GameObject (if your nested Prefab is an immediate child). For all other GameObject’s there’s no difference between being part of a base Prefab or being part of a nested Prefab. The “composition versus inheritance” way of looking at it is not 100% representative since you can also make overrides on nested Prefabs (inside the outer Prefab) just like you can on a base Prefab inside a Variant.
I recently did something like this. We had our main character prefab, but wanted a simplified version that just shared the model, the animation stuff, and a simple script.
I did this:
- Created a copy (PrefabCopy) of the original prefab (OriginalPrefab)
- Turned the root of OriginalPrefab into a base prefab (BasePrefab) by dragging the root into the hierarchy.
- Opened BasePrefab, and deleted everything that was not supposed to be shared
- Copied everything that had been deleted from PrefabCopy to OriginalPrefab.
It wasn’t that much work, but it got annoying, and it’s a bit error-prone. This also only worker because the player prefab is spawned, and there’s no references to it.
If we could insert an empty prefab as a new base prefab, that would be a very acceptable workaround - then we could simply apply all the “modifications” we wanted the new base to have. I imagine that this could be done without breaking the ID’s? You’ve written something about how IDs for prefabs work before, but I can’t quite remember what it is - is it some kind of flag system?
If the base is a new Prefab, then yes this should be compatible with the way we use the IDs. However, it wouldn’t work to select an existing Prefab as the base (whether it’s just an empty GameObject or not) as far as I understand. And so it still wouldn’t be a way to refactor multiple existing Prefabs to share a common base.
Would it maybe be possible to select multiple existing prefabs and make them all variants of one, newly created, empty prefab?
I think that would be a feature with which most of these work flows could be realized. Maybe we could even hack this ourselves somehow?
No*
The issue is that there is a deterministic relationship between the IDs of objects in a Prefab Asset, and the IDs of objects in the instance of that Prefab, such as inside a Prefab Variant. So we can compute the IDs in a new Variant based on the IDs in the base, or “reverse compute” the IDs in a new base based on the IDs in the Prefab that’s being made into a Variant. But this doesn’t work for making multiple different objects share the same new base. Trying to “reverse compute” the IDs based on one of the Prefabs will give different IDs than if we “reverse compute” them based another of the Prefabs.
- When I say it’s not possible in this thread, I mean in a clean way that preserves object references. It should be possible if we change the IDs of the objects in the Prefabs that are meant to get a new common base, but that means that any references you have to those objects will become missing. Also, I’m not 100% all the details in my explanation here are entirely correct, but the basic gist of it should be.
So true, Same need today and it’s been several times I ended up to this thread.
Unity should make something about this !
Any progress on this? The need for a feature like this comes up a lot. Simply diffing the prefabs against each other and proceeding to remove the same parts on the soon-to-be-variant should do the trick, no?
I’m running into the same issue / feature request.
Question, would it be possible to create a script to automatize this?
+1. Seems like this should definitely be possible.
Rumors says runevision changed job because of this issue
This would be very useful, we have many cases where a few objects have been independent prefabs and would like to have a base prefab. We avoid making new objects and copying things because we always end up with enormous errors when making new objects with the same name as previous existing objects. Cursed asset management
in git, you can see that Unity 2022.2 will have the function:
PrefabUtility.ReplacePrefabAssetOfPrefabInstances
with several overloads
I wonder if this function could be used for this use case.
I also would really appreciate this feature. I am having to redo a bunch of rather complicated prefabs because of this.
This is working for me in unity 2022.2.
To reparent a prefab:
- double click on the prefab to open it in the hierarchy view.
- right click on the root object of the prefab and click on Prefab → Reconnect Prefab
- then select the prefab you want as a parent
- When asked, click on “use old name”
- Then click on the overrides dropdown and adjust as necessary.
I still have to do one at a time, I have a few hundred prefabs, so it is time-consuming, but this is so much better than having to rebuild them.
We have had to disable the ability to Convert the plain root GameObject for a Prefab to a Prefab instance as you are doing above as this was breaking all instances of the prefab in the Scene and other Prefabs: All instances was loosing their default overrides and references to its objects would break. We had already prevented doing this for Variant assets as it was also breaking, but we missed these safety check for plain GameObjects in Prefab Mode as well.
The change will land in Unity 2022.2.2f1.
We don’t currently have a way to refactor Prefabs Variant inheritance due to the way we calculate fileIDs. Changing this would be a breaking change.