Search Unity

Adding a parent to prefab hierarchy

Discussion in 'Prefabs' started by indywijnia, Dec 18, 2018.

  1. indywijnia

    indywijnia

    Joined:
    Dec 18, 2018
    Posts:
    1
    RDX Surge Hi there, love the new prefabs but I was wondering if there was some way to make a prefab inherit from a predefined base prefab. So let's say I already have a prefab with a component on, and another with that component plus an extra component, is there some way to create a link between these two prefabs without having to start making variants from the base again from scratch?
    http://vitalprogenixaustralia.com/rdx-surge/

    Thanks
     
    Last edited: Dec 22, 2018
    _TheFuture_ likes this.
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Unfortunately there is no way to do this. All objects in Unity have an ID, and you when reference that object, the reference is to that ID. We wouldn't be able to make an object inherit from another object without changing its ID, and this would mean all references to the Prefab or components on it would break.

    If the breakage is acceptable, it would be possible to implement some way that could do it (and break references), but it's not something we have implemented. For now you'll have to start with creating a Prefab Variant, as you say, and then copy over any components you want to have on that variant. If you have value changes on components that are also in the base, you can also copy those and choose "paste component values" on the new Prefab Variant on the equivalent components.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    As an additional tool for speeding this up, you could probably do a GUID swap. Create the variant as @runevision suggests, and copy over the values from the prefab. Then, open the guid files of both the prefab and the variant, and swap their GUID values. This will make everything that used to reference the prefab now reference the variant.

    There might be things about the new prefab system that breaks this, but probably not, as you're just referencing the root prefab. It's always worked for every other kind of asset that I've tried it on, and it's a kinda standard part of our toolbox. That being said, it's a hack, and not exactly documented, so don't actually do it unless you've got a pretty good source control system going.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    I'm not the best expert in this, but as far as I understand, this is not possible (and this is the whole problem).

    References use a combination of GUID (which file) and fileID (which object in the file) to reference objects. And for a Prefab Variant, you can't control what the fileID of a given object in it is since it is not stored in the file or meta file at all, but is generated deterministically from the fileID of the object in the base Prefab that it derives from. The exact reason why it has to be this way escapes me now, but it has to do with import determinism.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    The fileID issue will kick in if you have local modifications to the prefabs you're replacing, as the local modifications would be for fileID's that's not relevant anymore.

    ... which include the fileID of the transform. So if you did this, all of the prefab instances that lives in scenes you're replacing would be moved to 0, 0, 0. Yup, just checked, that happened.

    So to sum up, the GUID swap trick is only useful for prefabs if it's less work to restore their modifications than it's to manually replace them. That will be the case if it's a prefab that's only used by referencing and instantiating it.

    You could also make a framework for finding them in all scenes, storing their modifications by path (like animation clips does), and then re-applying those modifications. That seems like a month-long solution to a day-long problem, though.
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Well that, and any other reference to the Prefab. I.e. any object field in any scene or in other Prefabs or ScritableObjects which reference a GameObject or component in the Prefab will break (become missing) after doing the GUID swap. You'd have to patch up all references to the Prefab.