Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to get prefab asset Object of a Nested Prefab Variant?

Discussion in 'Prefabs' started by ProgrammerSi, Jul 26, 2023.

  1. ProgrammerSi

    ProgrammerSi

    Joined:
    Mar 1, 2016
    Posts:
    4
    I find PrefabUtility to be quite a confusing API, but understand that nested prefabs and prefab variants have made it so. However i can't seem to find a single method in PrefabUtility that gets me what i want here.

    I have a prefab (let's call it A) that contains a nested prefab, this nested prefab is itself a prefab variant (let's call it BVariant).

    So we have A, we have B and BVariant.

    What i want to get is the BVariant asset from the BVariant nested prefab instance in A.

    GetCorrespondingObjectFromSource returns BVariant (object in prefab A) as stated in the docs.

    GetCorrespondingObjectFromOriginalSource gets me prefab asset B, not BVariant.

    The only way i have found to get what i want so far is to call GetCorrespondingObjectFromSource twice if the prefab is not the IsOutermostPrefabInstanceRoot, which just doesn't seem right.

    Is there a suggested way of doing this? Or am i missing something?

    Unity version: 2021.3.16

    Thanks.
     
    Last edited: Jul 31, 2023
  2. Katunator

    Katunator

    Joined:
    Oct 23, 2018
    Posts:
    5
  3. ProgrammerSi

    ProgrammerSi

    Joined:
    Mar 1, 2016
    Posts:
    4
    Thank you for your response! I was meaning to follow up that we discovered this in the end.

    The strange thing is that if you look at what this does, it calls a method GetOriginalSourceOrVariantRoot which is exactly what we want, but is not exposed to us, it is internal only.

    I wonder why that is, maybe Unity will expose it in a future version.

    But yes you are right, using GetPrefabAssetPathOfNearestInstanceRoot and then loading the asset manually gets me what i needed. Thank you.
     
    Katunator likes this.
  4. Katunator

    Katunator

    Joined:
    Oct 23, 2018
    Posts:
    5
    It's hard to look at method you can't call...
    Thank you too cause I was searching for a method that will return original prefab from prefab variant instance :p