Search Unity

Ability to Reparent or Rebase a Prefab Variant

Discussion in 'Prefabs' started by amisner2k, Jan 16, 2019.

  1. amisner2k

    amisner2k

    Joined:
    Jan 9, 2017
    Posts:
    43
    Hi, I scoured the existing posts but didn't find one that mentioned this feature and I couldn't figure out how to do this if it actually somehow is already implemented.

    It would be very useful (and pretty much necessary in larger projects that have many variants) to be able to reparent or rebase a prefab variant.

    For example, say I have the following prefabs in my project:

    Part
    Panel (Variant of Part)
    Engine (Variant of Part)
    Turret (Variant of Part)
    Mini Gun (Variant of Part)
    Pulse Gun (Variant of Part)​

    Later in development, I realize that some of my Parts should really be further specialized into a new variant of Part called Attachment. Attachment is a common ancestor for Turret, Mini Gun, and Pulse Gun.

    Here's what I'd like to turn my new prefab hierarchy into:

    Part
    Panel (Variant of Part)
    Engine (Variant of Part)
    Attachment (Variant of Part)
    Turret (Variant of Attachment)
    Mini Gun (Variant of Attachment)
    Pulse Gun (Variant of Attachment)​

    I am now in a predicament as there is currently no way (as far as I know) to simply change the parent of Turret, Mini Gun, and Pulse Gun to be Attachment. I have to recreate each one individually by creating a new variant from Attachment instead of from Part.

    ---

    Hope this makes sense.
     
    ModLunar, AlejMC, leni8ec and 4 others like this.
  2. kbrizov

    kbrizov

    Joined:
    Sep 12, 2015
    Posts:
    2
    This absolutely makes sense. It'd be a cool feature. I used to play a lot with Unreal Engine. Re-basing a blueprint is super easy. I love prefab variants, but it's unfortunate that there seems to be no way to easily do it at this point in time.
     
    amisner2k likes this.
  3. amisner2k

    amisner2k

    Joined:
    Jan 9, 2017
    Posts:
    43
    Thank you krisitak, I'm glad you think so too. I also played around with Unreal blueprints and you're right, it is something their editor does quite nicely. I completely forgot that was a thing in Unreal. Anyway, hopefully the devs see this. Now that feedback is relegated to the forums, I wish I could see something that indicated that a Unity dev saw or acknowledged this request in some way. In the meantime, I'll just have to subtly ponder the meaning of the universe while I wait. :p
     
    AlejMC and kbrizov like this.
  4. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    219
    Sorry for the late reply. This refactor operation should be doable today by opening your variant in Prefab Mode, then dragging the root into the Project Browser presents you with this dialog:

    upload_2019-11-15_15-31-17.png

    If you select "Insert Base" you are inserting a variant into the inheritance chain.
     
  5. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    219
    To solve your example with multiple variants you would need to be able to choose which base to insert to fix up the other variants as well. So this might not be what you need anyways.
     
    amisner2k likes this.
  6. amisner2k

    amisner2k

    Joined:
    Jan 9, 2017
    Posts:
    43
    Hi @Mads-Nyholm, I suppose I will also apologize for a late reply. ;)

    Unfortunately for my situation, I need to be able to tell a Prefab Variant to use a different existing prefab as its new base.

    Ideally, you'd be able to just edit the Base property as shown in the Inspector:

    upload_2020-2-10_22-57-18.png

    I'm sure there's a reason why this field is disabled for editing, but if it can be made editable somehow, that would be amazing. I understand there may be issues with maintaining the integrity of the system and it's not a simple matter to just allow this field to be editable, but I feel it'd be the most intuitive way to expose this capability.

    Anyway, thanks for helping Mads! I appreciate it.
     
  7. Deleted User

    Deleted User

    Guest

    I would like to see full inheritance support like OOP languages do have em. You should be able to choose the base prefab, base off of it, insert a base, change the base, whatever... right now you have to know up front what your prefab chain needs to look like, otherwise you'll have to do all of this from scratch if you want to merge certain aspects of prefabs into one common base prefab.
     
    MrDizzle26 likes this.
  8. Deleted User

    Deleted User

    Guest

    @Mads-Nyholm is it possible to create a base and remove components from it that are keept by the prefab I created that base from? Usually base prefabs have less components on them and creating one is usually the reason for a new variant branch. So once I know that I want a new branch from a prefab bit with fewer components, how do I create the base, strip its components, but keep them on the prefab I created it from?
     
  9. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Right, we have discussed a "extract component" / "extract GameObject" feature which is basically the inverse of the apply action. It's not something that exists today though, or is currently being worked on.
     
    AlejMC likes this.
  10. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    Glad this is being taken a look at. For the time being, is there a way to do this via text editor for now? say, creating a new base (a variant of the most basic prefab base) and make the other variants point to that new base? Tried not extensively but failed as I might not know what I'm doing.

    Figured to give that a try since the prefabs and variants are a series of GUIDs references and overrides. For the most part, a rebased variant will tend to have at least all of the objects (and if not, it will have 'remove' overrides for those) of the new base.

    Personal use case:
    For 99% of the cases I have found using variants for overriding basic behaviour/look/icons. The bottom-most base (or is it topmost?) has already most of the scripts, visuals, animations and functionality embedded... the variants add very few things when needed and override some others. (This might not be the most efficient way, don't know yet).
    And then there are those few that are composed prefabs with tons of children prefab (and maybe a variant of this composed one at some point) but that's usually not an issue... since the need to 'refactor' tents to fall on the 'work unit' prefabs (like the attachment part in the first post).
     
    Vivraan likes this.
  11. Vivraan

    Vivraan

    Joined:
    Feb 2, 2018
    Posts:
    26
    Bumping the thread for visibility of @Alejandro-Martinez-Chacin's query since introducing changes to text asset files using an editor is usually trivial in engines such as Godot.
     
    TheHeftyCoder and AlejMC like this.
  12. mmalley

    mmalley

    Joined:
    Apr 25, 2019
    Posts:
    2
    Howdy :). Sorry for necromancy, but I have a solution to at least one of the problems discussed... though it may not be what people are really looking for.

    My problem:
    • My team had a base object which all other objects are variants of.
    • I have made lots of things out of this base object.
    • We made some new tech that changed how our camera works - and visually broke several 'types' of assets but not others.
    • To fix this issue, I needed to adjust the base object, but doing so would actually break every other object that was not one of these 'types'.

    Solution:
    • We duplicated the original base object and named it something else (v2).
    • We opened each ".prefab" of all the variants which were the visually broken 'types' in a text editor, as well as both the original base object and the v2.
    • We did a search & replace in each ".prefab" for the GUID from the original and changed them into the v2.
    • Then we were able to adjust the v2 into the hierarchy we wanted without breaking all the other assets.
    • I was able to do this with Unity running, and it updated it real time no problem.
    I hope that helps some folks :).

    Cross posting to https://forum.unity.com/threads/change-base-of-prefab-variant.635119/ for visibility.
     
    amisner2k likes this.
  13. amisner2k

    amisner2k

    Joined:
    Jan 9, 2017
    Posts:
    43
    Nice, that's a good revelation. Sounds like something that could possibly be automated with an Editor script. But even better would be for the Unity Editor itself to be able to do this for us. If it really is that simple as updating GUIDs this should hopefully be rather straightforward to add in a future Unity release. Fingers crossed.
     
  14. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    137
    It's not as simple as just updating the GUID.
    It might come one day, in a few year. I'll bet Unity 2025.
     
  15. Sneirox

    Sneirox

    Joined:
    Jun 25, 2013
    Posts:
    18
    I'll bet Unity 3025.
     
    Sarai likes this.
  16. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    I am actually amazed that this post, and other on the same topic, are not getting more attention. To me this seems like something every single team should be struggling with every single day. It’s either get this working or live with a redundancy mess. The ability to refactor prefab hierarchies is an absolute must have imo. Imagine not being able to refactor inheritance chains in C#. The horror!

    I've read the posts above and in other threads and there are some really good ideas. However, I have a bit of a different take on what could be a solution, at least for me. And an unqualified guess is that implementing it should be really simple for the Unity team.

    To me it seems the by far most common scenario when one wants to rebase a prefab variant is when you have a subset of prefab variant siblings (variants with the same base prefab) and you want to “categorize” these to avoid redundancy among them. The categorization would be manifested by be inserting a common base prefab as the “category” and put the common/currently redundant stuff there.

    Here is a somewhat simplified scenario to make it less abstract.

    We start with 'Empty' a prefab which is not a variant of anything and is simply an empty game object. This is the root prefab from which every other prefab must “inherit” directly or indfirectly. Kind of like System.Object in C#. Now, as development progresses, we start creating random variants of this Empty. We end up with something like:

    * Sword
    * Snake
    * Coin
    * Bush
    * Axe
    * Crab
    * Javelin

    … and many others.

    It sooner or later becomes clear that the components/child objects we add to each of these are in many cases the same, so the work we are doing is starting to feel a bit redundant. We wish that we had created a 'Weapon' prefab variant from 'Empty' from which 'Sword' and 'Axe' and 'Javelin' could be variants.

    Today, the only thing we can do is to pick any of these and create a new base. But that helps no one. We could also create a base for 'Empty', but again, pointless.

    What we would like to do is to create a variant of 'Empty', call it 'Weapon' and then make 'Sword', 'Axe' and 'Javelin' use it as their base prefab. Now, afaik, this should be 100% viable, and pretty simple, since 'Weapon', at this point, is identical to 'Empty'. I.e. there are no structural changes needed to either of the explicit weapons, except that they need to update some fileIDs and guids to point to 'Weapon' instead of 'Empty'.

    After this has been done. We can now start removing the components/game objects that are redundantly added to each of these weapons and put them in the 'Weapon' base prefab variant instead.

    This procedure is of course possible for other categories too, like ‘Enemy’ for ‘Snake’ and ‘Crab’ or as a new level of categorization for the weapons. For the 'Sword', 'Axe' and 'Javelin' prefab variants, we can now say that we want a 'Melee Weapon' prefab variant as base for 'Sword' and 'Axe' and 'Projectile Weapon' prefab variant for 'Javelin' (and others). This would be done in the same way as before but this time by creating variants of 'Weapon' instead of 'Empty' and rebasing the explicit weapons to those.

    I am of course aware that in a real project, someone hopefully realized ahead of time that an obvious category like 'Weapon' is likely needed and did create it before creating the actual weapons. But at least in my case I constantly find myself wanting new sub categories among siblings which I did not think of in advance.

    Since creating a prefab variant of any existing prefab variant can easily be done today. The only added functionality we would need is the ability to change the base prefab to a direct unchanged variant of current base prefab. I do realize that the feature of arbitrarily changing a base prefab for any variant to any other variant in general might be a hornets nest, but for a variant of the existing prefab with no changes, which could be a requirement, it should be pretty simple?

    I'm believe this can be done by manually editing the .prefab file, like some of you suggested, but I would prefer to be able to do it with the PrefabUtility API.
     
  17. a52

    a52

    Joined:
    Mar 15, 2018
    Posts:
    18
    This might not work for all situations, but there is a way to do this in the current version (I'm on 2022.3 -- it may work in earlier versions as well).

    1. Right click your child object in the hierarchy. **Prefab > Unpack completely**
    2. **Reconnect Prefab** to the object you want as the new parent.

    Note that this creates it as a new prefab, so you'll have to replace any usages of the previous child prefab. But with the new quick-replace prefab features, that's not too bad in certain cases.
     
  18. PandaArcade

    PandaArcade

    Joined:
    Jan 2, 2017
    Posts:
    128
    Just adding my vote for this... Unity please add this feature!