Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2018.3 How to open a Prefab for editing programmatically?

Discussion in 'Prefabs' started by Jean-Fabre, Oct 3, 2018.

  1. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi Guys.

    I am wondering where is the API to open a prefab asset for editing?

    I can see the internal static method, but maybe there is a public method for this too?

    internal static PrefabStage OpenPrefab (string prefabAssetPath)
    {
    return PrefabStageUtility.OpenPrefab (prefabAssetPath, null);
    }


    Thanks :)

    Jean
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
  3. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Ah! I completely overlooked that call :) thanks!

    Bye,

    Jean
     
  4. Alex-Chouls

    Alex-Chouls

    Joined:
    Mar 24, 2009
    Posts:
    2,652
    Is there a public method to open the prefab in the editor the same way the Open Prefab button works in the Inspector?
    LoadPrefabContents seems to make a disconnected preview scene not visible to the user...?
     
  5. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    For now use the AssetDatabase API:

    Code (CSharp):
    1. AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath(yourPrefabAssetPath));
     
  6. Alex-Chouls

    Alex-Chouls

    Joined:
    Mar 24, 2009
    Posts:
    2,652
  7. Alex-Chouls

    Alex-Chouls

    Joined:
    Mar 24, 2009
    Posts:
    2,652
    How would I go about finding the corresponding component from an instance on the prefab stage?

    I have a component on a prefab instance.
    I can find the corresponding component on the prefab parent:
    var prefabParent = PrefabUtility.GetCorrespondingObjectFromSource(component)
    But how do I find the corresponding component in the preview stage?

    I tried using GetCorrespondingObjectFromSource on preview stage objects to match against prefabParent, but that seems to always return null.
     
    garage5 and wowo like this.
  8. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    How to close it? It makes problem when you have opened and go to Playmode, showing warrnings...
     
  9. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    Use
    StageUtility.GoToMainStage()
     
    daoth-gear and winxalex like this.
  10. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    Is there are way to refresh Inspector of Opened Prefab Window. I'm opened the Prefab Stage and I'm adding components thru script(AddComponent<T>) to prefabContentRoot which is selected, but I need to reselect to see new Components
     
  11. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    217
    I'm curious why the Inspector is not picking this up when rebuilding after opening the Prefab. Can you add your script to a bug report? Then I can investigate what happens.